From 5230740f6ffe9ed03dd59e0826cecb170bb9089b Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Wed, 9 May 2018 15:00:15 +0300 Subject: [PATCH] feat(cli): support `webpack-command` --- bin/webpack.js | 115 ++++++++++++++++++++++++++++++++-------------- declarations.d.ts | 1 + 2 files changed, 81 insertions(+), 35 deletions(-) diff --git a/bin/webpack.js b/bin/webpack.js index 9d83f9bef..4c6f3a5bd 100755 --- a/bin/webpack.js +++ b/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) - .then(result => { - return require("webpack-cli"); //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; - } + + 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(normalizedAnswer); //eslint-disable-line + }) + .catch(error => { + console.error(error); + process.exitCode = 1; + }); }); } 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 } diff --git a/declarations.d.ts b/declarations.d.ts index fe84d642b..5d161e7c5 100644 --- a/declarations.d.ts +++ b/declarations.d.ts @@ -1,5 +1,6 @@ declare module "*.json"; declare module "webpack-cli"; +declare module "webpack-command"; // Deprecated NodeJS API usages in Webpack declare namespace NodeJS {