GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #6467 from PlayMa256/prompt_install_cli · webpack/webpack@3f6b78f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • bin
    • webpack.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎bin/webpack.js‎

Copy file name to clipboard
100644100755
+71-10Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,82 @@
11
#!/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+
}
222

323
let webpackCliInstalled = false;
424
try {
525
require.resolve("webpack-cli");
626
webpackCliInstalled = true;
7-
} catch (e) {
27+
} catch (err) {
828
webpackCliInstalled = false;
929
}
1030

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+
});
1380
} 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
2182
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.