Skip to content

Commit 180e2b7

Browse files
matheus1lvasokra
authored andcommitted
* Pipe stdout and stderr from child_process to main process
* code refactoring and using promises
1 parent 58b6724 commit 180e2b7

1 file changed

Lines changed: 40 additions & 26 deletions

File tree

bin/webpack.js

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,69 @@
33
const cp = require("child_process");
44
const inquirer = require("inquirer");
55

6-
function runCommand(command) {
7-
cp.exec(command, (error, stdout, stderr) => {
8-
if(!error) {
9-
console.log("webpack-cli installed successfully");
10-
return true;
11-
}
12-
console.log("failed to install webpack-cli");
13-
console.error(stderr);
14-
return false;
6+
function runCommand(command, options) {
7+
return new Promise((resolve, reject) => {
8+
const executedCommand = cp.spawn(command, options, {
9+
stdio: "inherit"
10+
});
11+
12+
executedCommand.on("error", error => {
13+
reject(error);
14+
});
15+
16+
executedCommand.on("exit", code => {
17+
if (code === 0) {
18+
resolve(true);
19+
} else {
20+
reject();
21+
}
22+
});
1523
});
1624
}
1725

1826
let webpackCliInstalled = false;
1927
try {
2028
require.resolve("webpack-cli");
2129
webpackCliInstalled = true;
22-
} catch(err) {
30+
} catch (err) {
2331
webpackCliInstalled = false;
2432
}
2533

26-
if(!webpackCliInstalled) {
34+
if (!webpackCliInstalled) {
2735
const path = require("path");
2836
const fs = require("fs");
2937
const isYarn = fs.existsSync(path.resolve(process.cwd(), "yarn.lock"));
3038

39+
let packageManager;
40+
let options = [];
41+
if (isYarn) {
42+
packageManager = "yarn";
43+
options = ["add", "-D", "webpack-cli"];
44+
} else {
45+
packageManager = "npm";
46+
options = ["install", "--save-dev", "webpack-cli"];
47+
}
48+
49+
const commandToBeRun = `${packageManager} ${options.join(" ")}`;
50+
3151
const question = {
3252
type: "confirm",
3353
name: "shouldInstall",
34-
message: "Would you like to install webpack-cli?",
54+
message: `Would you like to install webpack-cli? (That will run ${commandToBeRun})`,
3555
default: true
3656
};
3757

3858
console.error("The CLI moved into a separate package: webpack-cli");
39-
inquirer.prompt(question).then((answer) => {
40-
if(answer) {
59+
inquirer.prompt(question).then(answer => {
60+
if (answer) {
4161
console.error("Installing webpack-cli");
42-
43-
let command;
44-
if(isYarn) {
45-
command = "yarn add -D webpack-cli";
46-
} else {
47-
command = "npm install --save-dev webpack-cli";
48-
}
49-
50-
if(runCommand(command)) {
51-
require("webpack-cli"); // eslint-disable-line node/no-missing-require, node/no-extraneous-require, node/no-unpublished-require
52-
}
62+
runCommand(packageManager, options)
63+
.then(result => require("webpack-cli")) // eslint-disable-line
64+
.catch(error => console.error(error));
65+
} else {
66+
process.exitCode(1);
5367
}
5468
});
5569
} else {
56-
require("webpack-cli"); // eslint-disable-line node/no-missing-require, node/no-extraneous-require, node/no-unpublished-require
70+
require("webpack-cli"); // eslint-disable-line
5771
}

0 commit comments

Comments
 (0)