|
1 | 1 | #!/usr/bin/env node |
| 2 | +function runCommand(command, options) { |
| 3 | + const cp = require("child_process"); |
| 4 | + return new Promise((resolve, reject) => { |
| 5 | + const executedCommand = cp.spawn(command, options, { |
| 6 | + stdio: "inherit" |
| 7 | + }); |
| 8 | + |
| 9 | + executedCommand.on("error", error => { |
| 10 | + reject(error); |
| 11 | + }); |
| 12 | + |
| 13 | + executedCommand.on("exit", code => { |
| 14 | + if (code === 0) { |
| 15 | + resolve(true); |
| 16 | + } else { |
| 17 | + reject(); |
| 18 | + } |
| 19 | + }); |
| 20 | + }); |
| 21 | +} |
2 | 22 |
|
3 | 23 | let webpackCliInstalled = false; |
4 | 24 | try { |
5 | 25 | require.resolve("webpack-cli"); |
6 | 26 | webpackCliInstalled = true; |
7 | | -} catch (e) { |
| 27 | +} catch (err) { |
8 | 28 | webpackCliInstalled = false; |
9 | 29 | } |
10 | 30 |
|
11 | | -if (webpackCliInstalled) { |
12 | | - require("webpack-cli"); // eslint-disable-line node/no-missing-require, node/no-extraneous-require, node/no-unpublished-require |
| 31 | +if (!webpackCliInstalled) { |
| 32 | + const path = require("path"); |
| 33 | + const fs = require("fs"); |
| 34 | + const readLine = require("readline"); |
| 35 | + const isYarn = fs.existsSync(path.resolve(process.cwd(), "yarn.lock")); |
| 36 | + |
| 37 | + const packageManager = isYarn ? "yarn" : "npm"; |
| 38 | + const options = ["install", "-D", "webpack-cli"]; |
| 39 | + |
| 40 | + if (isYarn) { |
| 41 | + options[0] = "add"; |
| 42 | + } |
| 43 | + |
| 44 | + const commandToBeRun = `${packageManager} ${options.join(" ")}`; |
| 45 | + |
| 46 | + const question = `Would you like to install webpack-cli? (That will run ${ |
| 47 | + commandToBeRun |
| 48 | + }) `; |
| 49 | + |
| 50 | + console.error("The CLI moved into a separate package: webpack-cli"); |
| 51 | + const questionInterface = readLine.createInterface({ |
| 52 | + input: process.stdin, |
| 53 | + output: process.stdout |
| 54 | + }); |
| 55 | + questionInterface.question(question, answer => { |
| 56 | + questionInterface.close(); |
| 57 | + switch (answer.toLowerCase()) { |
| 58 | + case "y": |
| 59 | + case "yes": |
| 60 | + case "1": { |
| 61 | + runCommand(packageManager, options) |
| 62 | + .then(result => { |
| 63 | + return require("webpack-cli"); //eslint-disable-line |
| 64 | + }) |
| 65 | + .catch(error => { |
| 66 | + console.error(error); |
| 67 | + process.exitCode = 1; |
| 68 | + }); |
| 69 | + break; |
| 70 | + } |
| 71 | + default: { |
| 72 | + console.error( |
| 73 | + "It needs to be installed alongside webpack to use the CLI" |
| 74 | + ); |
| 75 | + process.exitCode = 1; |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + }); |
13 | 80 | } else { |
14 | | - console.error("The CLI moved into a separate package: webpack-cli."); |
15 | | - console.error( |
16 | | - "Please install 'webpack-cli' in addition to webpack itself to use the CLI." |
17 | | - ); |
18 | | - console.error("-> When using npm: npm install webpack-cli -D"); |
19 | | - console.error("-> When using yarn: yarn add webpack-cli -D"); |
20 | | - process.exitCode = 1; |
| 81 | + require("webpack-cli"); // eslint-disable-line |
21 | 82 | } |
0 commit comments