Skip to content

Commit

Permalink
Replace requirer with node readline
Browse files Browse the repository at this point in the history
Removing inquirer and updating package.json
  • Loading branch information
matheus1lva authored and sokra committed Mar 9, 2018
1 parent f7af10a commit 1abef28
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 82 deletions.
93 changes: 41 additions & 52 deletions bin/webpack.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
#!/usr/bin/env node
const { exec, execSync } = require("child_process");
const inquirer = require("inquirer");

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) => {
executedCommand.on("error", error => {
reject(error);
});

executedCommand.on("exit", (code) => {
if(code === 0) {
executedCommand.on("exit", code => {
if (code === 0) {
resolve(true);
} else {
reject();
Expand All @@ -23,69 +21,60 @@ function runCommand(command, options) {
}

let webpackCliInstalled = false;
// try {
// const blah = require("webpack-cli"); // eslint-disable-line node/no-missing-require, node/no-extraneous-require, node/no-unpublished-require
// webpackCliInstalled = true;
// } catch(e) {
// console.log("error", e);
// webpackCliInstalled = false;
// }

try {
execSync("node -e require.resolve('webpack-cli')", { stdio: "ignore" });
require.resolve("webpack-cli");
webpackCliInstalled = true;
} catch (err) {
webpackCliInstalled = false;
}


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

let packageManager;
let options = [];
if(isYarn) {
packageManager = "yarn";
options = ["add", "-D", "webpack-cli"];
} else {
packageManager = "npm";
options = ["install", "--save-dev", "webpack-cli"];
const packageManager = isYarn ? "yarn" : "npm";
const options = ["install", "-D", "webpack-cli"];

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

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

const question = {
type: "confirm",
name: "shouldInstall",
message: `Would you like to install webpack-cli? (That will run ${commandToBeRun})`,
default: true
};
const question = `Would you like to install webpack-cli? (That will run ${commandToBeRun}) `;

if(isYarn) {
command = "yarn add webpack-cli -D";
} else {
command = "npm install --save-dev webpack-cli";
}

console.error("The CLI moved into a separate package: webpack-cli.\n");
inquirer.prompt(question).then((answer) => {
if(answer) {
console.error("Installing webpack-cli");
runCommand(packageManager, options).then((result) => {
require("webpack-cli"); // eslint-disable-line
}).catch((error) => {
console.error(error);
});
} else {
process.exitCode(1);
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 => {
switch (answer.toLowerCase()) {
case "y":
case "yes":
case "1": {
runCommand(packageManager, options)
.then(result => {
questionInterface.close();
return require("webpack-cli"); //eslint-disable-line
})
.catch(error => {
questionInterface.close();
console.error(error);
});
break;
}
default: {
console.error("The CLI moved into a separate package: webpack-cli");
console.error("It needs to be installed alongside webpack to use the CLI");
process.exitCode = 1;
questionInterface.close();
break;
}
}
});
} else {
require("webpack-cli"); // eslint-disable-line
}

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"chrome-trace-event": "^0.1.1",
"enhanced-resolve": "^4.0.0",
"eslint-scope": "^3.7.1",
"inquirer": "5.1.0",
"loader-runner": "^2.3.0",
"loader-utils": "^1.1.0",
"memory-fs": "~0.4.1",
Expand Down
30 changes: 1 addition & 29 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,7 @@ extend@~3.0.0, extend@~3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.1.tgz#a755ea7bc1adfcc5a31ce7e762dbaadc5e636444"

external-editor@^2.0.4, external-editor@^2.1.0:
external-editor@^2.0.4:
version "2.1.0"
resolved "https://registry.yarnpkg.com/external-editor/-/external-editor-2.1.0.tgz#3d026a21b7f95b5726387d4200ac160d372c3b48"
dependencies:
Expand Down Expand Up @@ -2012,24 +2012,6 @@ ini@~1.3.0:
version "1.3.4"
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.4.tgz#0537cb79daf59b59a1a517dff706c86ec039162e"

[email protected]:
version "5.1.0"
resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-5.1.0.tgz#19da508931892328abbbdd4c477f1efc65abfd67"
dependencies:
ansi-escapes "^3.0.0"
chalk "^2.0.0"
cli-cursor "^2.1.0"
cli-width "^2.0.0"
external-editor "^2.1.0"
figures "^2.0.0"
lodash "^4.3.0"
mute-stream "0.0.7"
run-async "^2.2.0"
rxjs "^5.5.2"
string-width "^2.1.0"
strip-ansi "^4.0.0"
through "^2.3.6"

inquirer@^3.0.6:
version "3.3.0"
resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-3.3.0.tgz#9dd2f2ad765dcab1ff0443b491442a20ba227dc9"
Expand Down Expand Up @@ -3849,12 +3831,6 @@ rx-lite@*, rx-lite@^4.0.8:
version "4.0.8"
resolved "https://registry.yarnpkg.com/rx-lite/-/rx-lite-4.0.8.tgz#0b1e11af8bc44836f04a6407e92da42467b79444"

rxjs@^5.5.2:
version "5.5.6"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-5.5.6.tgz#e31fb96d6fd2ff1fd84bcea8ae9c02d007179c02"
dependencies:
symbol-observable "1.0.1"

safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853"
Expand Down Expand Up @@ -4320,10 +4296,6 @@ svgo@^0.7.0:
sax "~1.2.1"
whet.extend "~0.9.9"

[email protected]:
version "1.0.1"
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.0.1.tgz#8340fc4702c3122df5d22288f88283f513d3fdd4"

table@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/table/-/table-4.0.2.tgz#a33447375391e766ad34d3486e6e2aedc84d2e36"
Expand Down

0 comments on commit 1abef28

Please sign in to comment.