Skip to content

Commit f7af10a

Browse files
matheus1lvasokra
authored andcommitted
* Pipe stdout and stderr from child_process to main process
* code refactoring and using promises
1 parent 18773b1 commit f7af10a

1 file changed

Lines changed: 44 additions & 21 deletions

File tree

‎bin/webpack.js‎

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
#!/usr/bin/env node
2+
const { exec, execSync } = require("child_process");
3+
const inquirer = require("inquirer");
4+
25
function runCommand(command, options) {
3-
const cp = require("child_process");
46
return new Promise((resolve, reject) => {
57
const executedCommand = cp.spawn(command, options, {
68
stdio: "inherit"
79
});
810

9-
executedCommand.on("error", error => {
11+
executedCommand.on("error", (error) => {
1012
reject(error);
1113
});
1214

13-
executedCommand.on("exit", code => {
14-
if (code === 0) {
15+
executedCommand.on("exit", (code) => {
16+
if(code === 0) {
1517
resolve(true);
1618
} else {
1719
reject();
@@ -21,48 +23,69 @@ function runCommand(command, options) {
2123
}
2224

2325
let webpackCliInstalled = false;
26+
// try {
27+
// const blah = require("webpack-cli"); // eslint-disable-line node/no-missing-require, node/no-extraneous-require, node/no-unpublished-require
28+
// webpackCliInstalled = true;
29+
// } catch(e) {
30+
// console.log("error", e);
31+
// webpackCliInstalled = false;
32+
// }
33+
2434
try {
25-
require.resolve("webpack-cli");
35+
execSync("node -e require.resolve('webpack-cli')", { stdio: "ignore" });
2636
webpackCliInstalled = true;
2737
} catch (err) {
2838
webpackCliInstalled = false;
2939
}
3040

31-
if (!webpackCliInstalled) {
41+
42+
if(webpackCliInstalled) {
43+
require("webpack-cli"); // eslint-disable-line node/no-missing-require, node/no-extraneous-require, node/no-unpublished-require
44+
} else {
3245
const path = require("path");
3346
const fs = require("fs");
34-
const inquirer = require("inquirer");
3547
const isYarn = fs.existsSync(path.resolve(process.cwd(), "yarn.lock"));
48+
let command;
3649

37-
const packageManager = isYarn ? "yarn" : "npm";
38-
const options = ["install", "-D", "webpack-cli"];
39-
40-
if (isYarn) {
41-
options[0] = "add";
50+
let packageManager;
51+
let options = [];
52+
if(isYarn) {
53+
packageManager = "yarn";
54+
options = ["add", "-D", "webpack-cli"];
55+
} else {
56+
packageManager = "npm";
57+
options = ["install", "--save-dev", "webpack-cli"];
4258
}
4359

4460
const commandToBeRun = `${packageManager} ${options.join(" ")}`;
4561

4662
const question = {
4763
type: "confirm",
4864
name: "shouldInstall",
49-
message: `Would you like to install webpack-cli? (That will run ${
50-
commandToBeRun
51-
})`,
65+
message: `Would you like to install webpack-cli? (That will run ${commandToBeRun})`,
5266
default: true
5367
};
5468

55-
console.error("The CLI moved into a separate package: webpack-cli");
56-
inquirer.prompt(question).then(answer => {
57-
if (answer) {
69+
if(isYarn) {
70+
command = "yarn add webpack-cli -D";
71+
} else {
72+
command = "npm install --save-dev webpack-cli";
73+
}
74+
75+
console.error("The CLI moved into a separate package: webpack-cli.\n");
76+
inquirer.prompt(question).then((answer) => {
77+
if(answer) {
5878
console.error("Installing webpack-cli");
59-
runCommand(packageManager, options)
60-
.then(result => require("webpack-cli")) // eslint-disable-line
61-
.catch(error => console.error(error));
79+
runCommand(packageManager, options).then((result) => {
80+
require("webpack-cli"); // eslint-disable-line
81+
}).catch((error) => {
82+
console.error(error);
83+
});
6284
} else {
6385
process.exitCode(1);
6486
}
6587
});
6688
} else {
6789
require("webpack-cli"); // eslint-disable-line
6890
}
91+

0 commit comments

Comments
 (0)