2012-03-10 20:11:23 +08:00
|
|
|
#!/usr/bin/env node
|
2018-02-26 06:17:27 +08:00
|
|
|
function runCommand(command, options) {
|
2018-03-07 04:07:54 +08:00
|
|
|
const cp = require("child_process");
|
2018-02-26 06:17:27 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const executedCommand = cp.spawn(command, options, {
|
2018-03-29 23:06:10 +08:00
|
|
|
stdio: "inherit",
|
|
|
|
shell: true
|
2018-02-26 06:17:27 +08:00
|
|
|
});
|
|
|
|
|
2018-03-07 04:07:54 +08:00
|
|
|
executedCommand.on("error", error => {
|
2018-02-26 06:17:27 +08:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
|
2018-03-07 04:07:54 +08:00
|
|
|
executedCommand.on("exit", code => {
|
|
|
|
if (code === 0) {
|
2018-02-26 06:17:27 +08:00
|
|
|
resolve(true);
|
|
|
|
} else {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
});
|
2018-02-08 08:28:38 +08:00
|
|
|
});
|
|
|
|
}
|
2012-03-10 20:11:23 +08:00
|
|
|
|
2017-12-13 18:30:15 +08:00
|
|
|
let webpackCliInstalled = false;
|
|
|
|
try {
|
|
|
|
require.resolve("webpack-cli");
|
|
|
|
webpackCliInstalled = true;
|
2018-02-26 06:17:27 +08:00
|
|
|
} catch (err) {
|
2017-12-13 18:30:15 +08:00
|
|
|
webpackCliInstalled = false;
|
|
|
|
}
|
|
|
|
|
2018-03-07 04:07:54 +08:00
|
|
|
if (!webpackCliInstalled) {
|
2018-02-09 00:01:23 +08:00
|
|
|
const path = require("path");
|
|
|
|
const fs = require("fs");
|
2018-03-07 04:07:54 +08:00
|
|
|
const readLine = require("readline");
|
2018-02-09 00:01:23 +08:00
|
|
|
const isYarn = fs.existsSync(path.resolve(process.cwd(), "yarn.lock"));
|
|
|
|
|
2018-03-07 04:07:54 +08:00
|
|
|
const packageManager = isYarn ? "yarn" : "npm";
|
|
|
|
const options = ["install", "-D", "webpack-cli"];
|
|
|
|
|
|
|
|
if (isYarn) {
|
|
|
|
options[0] = "add";
|
2018-02-26 06:17:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const commandToBeRun = `${packageManager} ${options.join(" ")}`;
|
|
|
|
|
2018-03-29 23:06:10 +08:00
|
|
|
const question = `Would you like to install webpack-cli? (That will run ${commandToBeRun}) (yes/NO)`;
|
2018-02-08 08:28:38 +08:00
|
|
|
|
2018-03-07 04:07:54 +08:00
|
|
|
console.error("The CLI moved into a separate package: webpack-cli");
|
|
|
|
const questionInterface = readLine.createInterface({
|
|
|
|
input: process.stdin,
|
|
|
|
output: process.stdout
|
|
|
|
});
|
|
|
|
questionInterface.question(question, answer => {
|
2018-03-13 16:36:10 +08:00
|
|
|
questionInterface.close();
|
2018-03-07 04:07:54 +08:00
|
|
|
switch (answer.toLowerCase()) {
|
|
|
|
case "y":
|
|
|
|
case "yes":
|
|
|
|
case "1": {
|
|
|
|
runCommand(packageManager, options)
|
|
|
|
.then(result => {
|
|
|
|
return require("webpack-cli"); //eslint-disable-line
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
2018-03-13 16:34:57 +08:00
|
|
|
process.exitCode = 1;
|
2018-03-07 04:07:54 +08:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2018-03-09 16:17:39 +08:00
|
|
|
console.error(
|
|
|
|
"It needs to be installed alongside webpack to use the CLI"
|
|
|
|
);
|
2018-03-07 04:07:54 +08:00
|
|
|
process.exitCode = 1;
|
|
|
|
break;
|
|
|
|
}
|
2018-02-08 08:28:38 +08:00
|
|
|
}
|
2018-02-09 00:01:23 +08:00
|
|
|
});
|
2017-12-13 18:30:15 +08:00
|
|
|
} else {
|
2018-02-26 06:17:27 +08:00
|
|
|
require("webpack-cli"); // eslint-disable-line
|
2017-12-13 18:30:15 +08:00
|
|
|
}
|