|
3 | 3 | const cp = require("child_process"); |
4 | 4 | const inquirer = require("inquirer"); |
5 | 5 |
|
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 | + }); |
15 | 23 | }); |
16 | 24 | } |
17 | 25 |
|
18 | 26 | let webpackCliInstalled = false; |
19 | 27 | try { |
20 | 28 | require.resolve("webpack-cli"); |
21 | 29 | webpackCliInstalled = true; |
22 | | -} catch(err) { |
| 30 | +} catch (err) { |
23 | 31 | webpackCliInstalled = false; |
24 | 32 | } |
25 | 33 |
|
26 | | -if(!webpackCliInstalled) { |
| 34 | +if (!webpackCliInstalled) { |
27 | 35 | const path = require("path"); |
28 | 36 | const fs = require("fs"); |
29 | 37 | const isYarn = fs.existsSync(path.resolve(process.cwd(), "yarn.lock")); |
30 | 38 |
|
| 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 | + |
31 | 51 | const question = { |
32 | 52 | type: "confirm", |
33 | 53 | name: "shouldInstall", |
34 | | - message: "Would you like to install webpack-cli?", |
| 54 | + message: `Would you like to install webpack-cli? (That will run ${commandToBeRun})`, |
35 | 55 | default: true |
36 | 56 | }; |
37 | 57 |
|
38 | 58 | 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) { |
41 | 61 | 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); |
53 | 67 | } |
54 | 68 | }); |
55 | 69 | } 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 |
57 | 71 | } |
0 commit comments