mirror of https://github.com/webpack/webpack.git
feat(cli): support `webpack-command`
This commit is contained in:
parent
586f9a2176
commit
5230740f6f
105
bin/webpack.js
105
bin/webpack.js
|
@ -21,61 +21,106 @@ function runCommand(command, options) {
|
|||
});
|
||||
}
|
||||
|
||||
let webpackCliInstalled = false;
|
||||
try {
|
||||
require.resolve("webpack-cli");
|
||||
webpackCliInstalled = true;
|
||||
} catch (err) {
|
||||
webpackCliInstalled = false;
|
||||
function isInstalled(packageName) {
|
||||
try {
|
||||
require.resolve(packageName);
|
||||
|
||||
return true;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!webpackCliInstalled) {
|
||||
const CLI = [
|
||||
{
|
||||
name: "webpack-cli",
|
||||
installed: isInstalled("webpack-cli"),
|
||||
URL: "https://github.com/webpack/webpack-cli",
|
||||
description: "The original webpack full-featured CLI from webpack@3."
|
||||
},
|
||||
{
|
||||
name: "webpack-command",
|
||||
installed: isInstalled("webpack-command"),
|
||||
URL: "https://github.com/webpack-contrib/webpack-command",
|
||||
description: "A lightweight, opinionated webpack CLI."
|
||||
}
|
||||
];
|
||||
|
||||
if (CLI.every(item => !item.installed)) {
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const readLine = require("readline");
|
||||
|
||||
let notify =
|
||||
"The CLI for webpack must be installed as a separate package, for which there are choices:\n";
|
||||
|
||||
CLI.forEach(item => {
|
||||
notify += ` ${item.name} (${item.URL}): ${item.description}\n`;
|
||||
});
|
||||
|
||||
console.error(notify);
|
||||
|
||||
const isYarn = fs.existsSync(path.resolve(process.cwd(), "yarn.lock"));
|
||||
|
||||
const packageManager = isYarn ? "yarn" : "npm";
|
||||
const options = ["install", "-D", "webpack-cli"];
|
||||
const installOptions = ["install", "-D"];
|
||||
|
||||
if (isYarn) {
|
||||
options[0] = "add";
|
||||
installOptions[0] = "add";
|
||||
}
|
||||
|
||||
const commandToBeRun = `${packageManager} ${options.join(" ")}`;
|
||||
let question = `Would you like to install (${CLI.map(item => item.name).join(
|
||||
"/"
|
||||
)}):\n`;
|
||||
|
||||
const question = `Would you like to install webpack-cli? (That will run ${commandToBeRun}) (yes/NO)`;
|
||||
|
||||
console.error("The CLI moved into a separate package: webpack-cli");
|
||||
const questionInterface = readLine.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
questionInterface.question(question, answer => {
|
||||
questionInterface.close();
|
||||
switch (answer.toLowerCase()) {
|
||||
case "y":
|
||||
case "yes":
|
||||
case "1": {
|
||||
runCommand(packageManager, options)
|
||||
|
||||
const normalizedAnswer = answer.toLowerCase();
|
||||
const selectedPackage = CLI.find(item => item.name === normalizedAnswer);
|
||||
|
||||
if (!selectedPackage) {
|
||||
console.error(
|
||||
"It needs to be installed alongside webpack to use the CLI"
|
||||
);
|
||||
process.exitCode = 1;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
installOptions.push(normalizedAnswer);
|
||||
|
||||
console.log(
|
||||
`Installing '${normalizedAnswer}' (running '${packageManager} ${installOptions.join(
|
||||
" "
|
||||
)}')...`
|
||||
);
|
||||
|
||||
runCommand(packageManager, installOptions)
|
||||
.then(result => {
|
||||
return require("webpack-cli"); //eslint-disable-line
|
||||
return require(normalizedAnswer); //eslint-disable-line
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
console.error(
|
||||
"It needs to be installed alongside webpack to use the CLI"
|
||||
);
|
||||
process.exitCode = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
require("webpack-cli"); // eslint-disable-line
|
||||
const installedPackage = CLI.map(
|
||||
item => (item.installed ? item.name : "")
|
||||
).filter(v => v);
|
||||
|
||||
if (installedPackage.length > 1) {
|
||||
console.warn(
|
||||
`You have installed ${installedPackage.join(
|
||||
" and "
|
||||
)} together. To work with the webpack you need only one CLI package, please remove one of them`
|
||||
);
|
||||
}
|
||||
|
||||
require(installedPackage[0]); // eslint-disable-line
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
declare module "*.json";
|
||||
declare module "webpack-cli";
|
||||
declare module "webpack-command";
|
||||
|
||||
// Deprecated NodeJS API usages in Webpack
|
||||
declare namespace NodeJS {
|
||||
|
|
Loading…
Reference in New Issue