mirror of https://github.com/webpack/webpack.git
* Pipe stdout and stderr from child_process to main process
* code refactoring and using promises
This commit is contained in:
parent
58b67248e8
commit
180e2b7dd2
|
@ -3,15 +3,23 @@
|
||||||
const cp = require("child_process");
|
const cp = require("child_process");
|
||||||
const inquirer = require("inquirer");
|
const inquirer = require("inquirer");
|
||||||
|
|
||||||
function runCommand(command) {
|
function runCommand(command, options) {
|
||||||
cp.exec(command, (error, stdout, stderr) => {
|
return new Promise((resolve, reject) => {
|
||||||
if(!error) {
|
const executedCommand = cp.spawn(command, options, {
|
||||||
console.log("webpack-cli installed successfully");
|
stdio: "inherit"
|
||||||
return true;
|
});
|
||||||
}
|
|
||||||
console.log("failed to install webpack-cli");
|
executedCommand.on("error", error => {
|
||||||
console.error(stderr);
|
reject(error);
|
||||||
return false;
|
});
|
||||||
|
|
||||||
|
executedCommand.on("exit", code => {
|
||||||
|
if (code === 0) {
|
||||||
|
resolve(true);
|
||||||
|
} else {
|
||||||
|
reject();
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,39 +27,45 @@ let webpackCliInstalled = false;
|
||||||
try {
|
try {
|
||||||
require.resolve("webpack-cli");
|
require.resolve("webpack-cli");
|
||||||
webpackCliInstalled = true;
|
webpackCliInstalled = true;
|
||||||
} catch(err) {
|
} catch (err) {
|
||||||
webpackCliInstalled = false;
|
webpackCliInstalled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!webpackCliInstalled) {
|
if (!webpackCliInstalled) {
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const isYarn = fs.existsSync(path.resolve(process.cwd(), "yarn.lock"));
|
const isYarn = fs.existsSync(path.resolve(process.cwd(), "yarn.lock"));
|
||||||
|
|
||||||
|
let packageManager;
|
||||||
|
let options = [];
|
||||||
|
if (isYarn) {
|
||||||
|
packageManager = "yarn";
|
||||||
|
options = ["add", "-D", "webpack-cli"];
|
||||||
|
} else {
|
||||||
|
packageManager = "npm";
|
||||||
|
options = ["install", "--save-dev", "webpack-cli"];
|
||||||
|
}
|
||||||
|
|
||||||
|
const commandToBeRun = `${packageManager} ${options.join(" ")}`;
|
||||||
|
|
||||||
const question = {
|
const question = {
|
||||||
type: "confirm",
|
type: "confirm",
|
||||||
name: "shouldInstall",
|
name: "shouldInstall",
|
||||||
message: "Would you like to install webpack-cli?",
|
message: `Would you like to install webpack-cli? (That will run ${commandToBeRun})`,
|
||||||
default: true
|
default: true
|
||||||
};
|
};
|
||||||
|
|
||||||
console.error("The CLI moved into a separate package: webpack-cli");
|
console.error("The CLI moved into a separate package: webpack-cli");
|
||||||
inquirer.prompt(question).then((answer) => {
|
inquirer.prompt(question).then(answer => {
|
||||||
if(answer) {
|
if (answer) {
|
||||||
console.error("Installing webpack-cli");
|
console.error("Installing webpack-cli");
|
||||||
|
runCommand(packageManager, options)
|
||||||
let command;
|
.then(result => require("webpack-cli")) // eslint-disable-line
|
||||||
if(isYarn) {
|
.catch(error => console.error(error));
|
||||||
command = "yarn add -D webpack-cli";
|
} else {
|
||||||
} else {
|
process.exitCode(1);
|
||||||
command = "npm install --save-dev webpack-cli";
|
|
||||||
}
|
|
||||||
|
|
||||||
if(runCommand(command)) {
|
|
||||||
require("webpack-cli"); // eslint-disable-line node/no-missing-require, node/no-extraneous-require, node/no-unpublished-require
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
require("webpack-cli"); // eslint-disable-line node/no-missing-require, node/no-extraneous-require, node/no-unpublished-require
|
require("webpack-cli"); // eslint-disable-line
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue