diff --git a/bin/prompt-command.js b/bin/prompt-command.js index a657fa3ff46..2d810acbdf5 100644 --- a/bin/prompt-command.js +++ b/bin/prompt-command.js @@ -27,6 +27,16 @@ const runCommand = (command, args) => { }); }; +const npmGlobalRoot = () => { + const cp = require("child_process"); + return new Promise((resolve, reject) => { + const command = cp.spawn("npm", ["root", "-g"]); + command.on('error', (error) => reject(error)); + command.stdout.on('data', (data) => resolve(data.toString())); + command.stderr.on('data', (data) => reject(data)); + }) +} + module.exports = function promptForInstallation(packages, ...args) { const nameOfPackage = "@webpack-cli/" + packages; let packageIsInstalled = false; @@ -59,9 +69,18 @@ module.exports = function promptForInstallation(packages, ...args) { options[0] = "add"; } + if (packages == 'init') { + if (isYarn) { + options.splice(1, 1); // remove '-D' + options.splice(0, 0, "global"); + } else { + options[1] = "-g"; + } + } + const commandToBeRun = `${packageManager} ${options.join(" ")}`; - const question = `Would you like to install ${packages}? (That will run ${commandToBeRun}) (yes/NO)`; + const question = `Would you like to install ${packages}? (That will run ${commandToBeRun}) (yes/NO) : `; console.error(`The command moved into a separate package: ${nameOfPackage}`); const questionInterface = readLine.createInterface({ @@ -77,6 +96,22 @@ module.exports = function promptForInstallation(packages, ...args) { runCommand(packageManager, options) .then(_=> { + if (packages == "init") { + npmGlobalRoot() + .then((root) => { + pathtoInit = path.resolve(root.trim(), "@webpack-cli", "init"); + return pathtoInit; + }) + .then((pathForInit) => { + return require(pathForInit).default(...args); + }) + .catch((error) => { + console.error(error); + process.exitCode = 1; + }) + return; + } + pathForCmd = path.resolve(process.cwd(), "node_modules", "@webpack-cli", packages); if (packages === "serve") { return require(pathForCmd).default.serve();