Skip to content

Commit

Permalink
cli(prompt): initial comment for prompt file
Browse files Browse the repository at this point in the history
  • Loading branch information
evenstensberg committed May 14, 2018
1 parent df8287d commit f8a71c0
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
4 changes: 1 addition & 3 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@
});

if (NON_COMPILATION_CMD) {
// TODO: prompt if package isn't installed, run installed package otherwise.
//return require(`./commands/${NON_COMPILATION_CMD}`)(...process.argv);
return;
return require('./prompt-command')(NON_COMPILATION_CMD, ...process.argv);
}

const yargs = require("yargs").usage(`webpack-cli ${
Expand Down
82 changes: 81 additions & 1 deletion bin/prompt-command.js
Original file line number Diff line number Diff line change
@@ -1 +1,81 @@
module.exports = {};
// based on https://github.com/webpack/webpack/blob/master/bin/webpack.js
module.exports = function promptForInstallation(command, options) {
const cp = require("child_process");
return new Promise((resolve, reject) => {
const executedCommand = cp.spawn(command, options, {
stdio: "inherit",
shell: true
});

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

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

let packageIsInstalled = false;
try {
require.resolve(package);
packageIsInstalled = true;
} catch (err) {
packageIsInstalled = false;
}

if (!packageIsInstalled) {
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", package];

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

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

const question = `Would you like to install ${packageIsInstalled}? (That will run ${commandToBeRun}) (yes/NO)`;

console.error(`The CLI moved into a separate package: @webpack-cli/${package}`);
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/${package}`); //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(package); // eslint-disable-line
}

0 comments on commit f8a71c0

Please sign in to comment.