forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
145 lines (133 loc) · 3.47 KB
/
Copy pathsetup.js
File metadata and controls
145 lines (133 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"use strict";
const fs = require("fs");
const path = require("path");
const root = process.cwd();
const nodeModulesFolder = path.resolve(root, "node_modules");
const webpackDependencyFolder = path.resolve(root, "node_modules/webpack");
/**
* @returns {Promise<void>} result
*/
function setup() {
return checkSymlinkExistsAsync()
.then(async (hasSymlink) => {
if (!hasSymlink) {
await ensureYarnInstalledAsync();
await runSetupSymlinkAsync();
if (!(await checkSymlinkExistsAsync())) {
throw new Error("windows symlink was not successfully created");
}
}
})
.then(() => {
process.exitCode = 0;
})
.catch((err) => {
console.error(err);
process.exitCode = 1;
});
}
/**
* @returns {Promise<void>} result
*/
async function runSetupSymlinkAsync() {
await exec("yarn", ["install"], "Install dependencies");
await exec("yarn", ["link"], "Create webpack symlink");
await exec("yarn", ["link", "webpack"], "Link webpack into itself");
}
/**
* @returns {Promise<boolean>} result
*/
function checkSymlinkExistsAsync() {
return new Promise((resolve) => {
try {
if (
fs.existsSync(nodeModulesFolder) &&
fs.existsSync(webpackDependencyFolder) &&
fs.lstatSync(webpackDependencyFolder).isSymbolicLink()
) {
resolve(true);
} else {
resolve(false);
}
} catch {
resolve(false);
}
});
}
/**
* @returns {Promise<void>} result
*/
async function ensureYarnInstalledAsync() {
const semverPattern =
/^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|\d*[a-z-][0-9a-z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-z-][0-9a-z-]*))*)?(?:\+[0-9a-z-]+(?:\.[0-9a-z-]+)*)?$/i;
let hasYarn = false;
try {
const stdout = await execGetOutput("yarn", ["-v"], "Check yarn version");
hasYarn = semverPattern.test(stdout);
} catch (_err) {
hasYarn = false;
}
if (!hasYarn) await installYarnAsync();
}
/**
* @returns {Promise<void>} result
*/
function installYarnAsync() {
return exec("npm", ["install", "-g", "yarn"], "Install yarn");
}
/**
* @param {string} command command
* @param {string[]} args args
* @param {string} description description
* @returns {Promise<void>} result
*/
function exec(command, args, description) {
console.log(`Setup: ${description}`);
return new Promise((resolve, reject) => {
const cp = require("child_process").spawn(command, args, {
cwd: root,
stdio: "inherit",
shell: true
});
cp.on("error", (error) => {
reject(new Error(`${description} failed with ${error}`));
});
cp.on("exit", (exitCode) => {
if (exitCode) {
reject(new Error(`${description} failed with exit code ${exitCode}`));
} else {
resolve();
}
});
});
}
/**
* @param {string} command command
* @param {string[]} args args
* @param {string} description description
* @returns {Promise<string>} result
*/
function execGetOutput(command, args, description) {
console.log(`Setup: ${description}`);
return new Promise((resolve, reject) => {
const cp = require("child_process").spawn(command, args, {
cwd: root,
stdio: [process.stdin, "pipe", process.stderr],
shell: true
});
cp.on("error", (error) => {
reject(new Error(`${description} failed with ${error}`));
});
cp.on("exit", (exitCode) => {
if (exitCode) {
reject(new Error(`${description} failed with exit code ${exitCode}`));
} else {
resolve(Buffer.concat(buffers).toString("utf8").trim());
}
});
/** @type {Buffer[]} */
const buffers = [];
cp.stdout.on("data", (data) => buffers.push(data));
});
}
setup();