Skip to content

Commit

Permalink
Merge pull request #6467 from PlayMa256/prompt_install_cli
Browse files Browse the repository at this point in the history
Prompt install cli
  • Loading branch information
sokra committed Mar 29, 2018
2 parents b30de38 + e500384 commit 3f6b78f
Showing 1 changed file with 71 additions and 10 deletions.
81 changes: 71 additions & 10 deletions bin/webpack.js 100644 → 100755
@@ -1,21 +1,82 @@
#!/usr/bin/env node
function runCommand(command, options) {
const cp = require("child_process");
return new Promise((resolve, reject) => {
const executedCommand = cp.spawn(command, options, {
stdio: "inherit"
});

executedCommand.on("error", error => {
reject(error);
});

executedCommand.on("exit", code => {
if (code === 0) {
resolve(true);
} else {
reject();
}
});
});
}

let webpackCliInstalled = false;
try {
require.resolve("webpack-cli");
webpackCliInstalled = true;
} catch (e) {
} catch (err) {
webpackCliInstalled = false;
}

if (webpackCliInstalled) {
require("webpack-cli"); // eslint-disable-line node/no-missing-require, node/no-extraneous-require, node/no-unpublished-require
if (!webpackCliInstalled) {
const path = require("path");
const fs = require("fs");
const readLine = require("readline");
const isYarn = fs.existsSync(path.resolve(process.cwd(), "yarn.lock"));

const packageManager = isYarn ? "yarn" : "npm";
const options = ["install", "-D", "webpack-cli"];

if (isYarn) {
options[0] = "add";
}

const commandToBeRun = `${packageManager} ${options.join(" ")}`;

const question = `Would you like to install webpack-cli? (That will run ${
commandToBeRun
}) `;

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;
}
}
});
} else {
console.error("The CLI moved into a separate package: webpack-cli.");
console.error(
"Please install 'webpack-cli' in addition to webpack itself to use the CLI."
);
console.error("-> When using npm: npm install webpack-cli -D");
console.error("-> When using yarn: yarn add webpack-cli -D");
process.exitCode = 1;
require("webpack-cli"); // eslint-disable-line
}

0 comments on commit 3f6b78f

Please sign in to comment.