Skip to content

Commit 5230740

Browse files
author
evilebottnawi
committed
feat(cli): support webpack-command
1 parent 586f9a2 commit 5230740

File tree

2 files changed

+81
-35
lines changed

2 files changed

+81
-35
lines changed

bin/webpack.js

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,61 +21,106 @@ function runCommand(command, options) {
2121
});
2222
}
2323

24-
let webpackCliInstalled = false;
25-
try {
26-
require.resolve("webpack-cli");
27-
webpackCliInstalled = true;
28-
} catch (err) {
29-
webpackCliInstalled = false;
24+
function isInstalled(packageName) {
25+
try {
26+
require.resolve(packageName);
27+
28+
return true;
29+
} catch (err) {
30+
return false;
31+
}
3032
}
3133

32-
if (!webpackCliInstalled) {
34+
const CLI = [
35+
{
36+
name: "webpack-cli",
37+
installed: isInstalled("webpack-cli"),
38+
URL: "https://github.com/webpack/webpack-cli",
39+
description: "The original webpack full-featured CLI from webpack@3."
40+
},
41+
{
42+
name: "webpack-command",
43+
installed: isInstalled("webpack-command"),
44+
URL: "https://github.com/webpack-contrib/webpack-command",
45+
description: "A lightweight, opinionated webpack CLI."
46+
}
47+
];
48+
49+
if (CLI.every(item => !item.installed)) {
3350
const path = require("path");
3451
const fs = require("fs");
3552
const readLine = require("readline");
53+
54+
let notify =
55+
"The CLI for webpack must be installed as a separate package, for which there are choices:\n";
56+
57+
CLI.forEach(item => {
58+
notify += ` ${item.name} (${item.URL}): ${item.description}\n`;
59+
});
60+
61+
console.error(notify);
62+
3663
const isYarn = fs.existsSync(path.resolve(process.cwd(), "yarn.lock"));
3764

3865
const packageManager = isYarn ? "yarn" : "npm";
39-
const options = ["install", "-D", "webpack-cli"];
66+
const installOptions = ["install", "-D"];
4067

4168
if (isYarn) {
42-
options[0] = "add";
69+
installOptions[0] = "add";
4370
}
4471

45-
const commandToBeRun = `${packageManager} ${options.join(" ")}`;
72+
let question = `Would you like to install (${CLI.map(item => item.name).join(
73+
"/"
74+
)}):\n`;
4675

47-
const question = `Would you like to install webpack-cli? (That will run ${commandToBeRun}) (yes/NO)`;
48-
49-
console.error("The CLI moved into a separate package: webpack-cli");
5076
const questionInterface = readLine.createInterface({
5177
input: process.stdin,
5278
output: process.stdout
5379
});
5480
questionInterface.question(question, answer => {
5581
questionInterface.close();
56-
switch (answer.toLowerCase()) {
57-
case "y":
58-
case "yes":
59-
case "1": {
60-
runCommand(packageManager, options)
61-
.then(result => {
62-
return require("webpack-cli"); //eslint-disable-line
63-
})
64-
.catch(error => {
65-
console.error(error);
66-
process.exitCode = 1;
67-
});
68-
break;
69-
}
70-
default: {
71-
console.error(
72-
"It needs to be installed alongside webpack to use the CLI"
73-
);
74-
process.exitCode = 1;
75-
break;
76-
}
82+
83+
const normalizedAnswer = answer.toLowerCase();
84+
const selectedPackage = CLI.find(item => item.name === normalizedAnswer);
85+
86+
if (!selectedPackage) {
87+
console.error(
88+
"It needs to be installed alongside webpack to use the CLI"
89+
);
90+
process.exitCode = 1;
91+
92+
return;
7793
}
94+
95+
installOptions.push(normalizedAnswer);
96+
97+
console.log(
98+
`Installing '${normalizedAnswer}' (running '${packageManager} ${installOptions.join(
99+
" "
100+
)}')...`
101+
);
102+
103+
runCommand(packageManager, installOptions)
104+
.then(result => {
105+
return require(normalizedAnswer); //eslint-disable-line
106+
})
107+
.catch(error => {
108+
console.error(error);
109+
process.exitCode = 1;
110+
});
78111
});
79112
} else {
80-
require("webpack-cli"); // eslint-disable-line
113+
const installedPackage = CLI.map(
114+
item => (item.installed ? item.name : "")
115+
).filter(v => v);
116+
117+
if (installedPackage.length > 1) {
118+
console.warn(
119+
`You have installed ${installedPackage.join(
120+
" and "
121+
)} together. To work with the webpack you need only one CLI package, please remove one of them`
122+
);
123+
}
124+
125+
require(installedPackage[0]); // eslint-disable-line
81126
}

declarations.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
declare module "*.json";
22
declare module "webpack-cli";
3+
declare module "webpack-command";
34

45
// Deprecated NodeJS API usages in Webpack
56
declare namespace NodeJS {

0 commit comments

Comments
 (0)