Skip to content

Commit

Permalink
chore(rebase): refactor stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
evenstensberg committed May 17, 2018
1 parent 2b3035c commit b02070d
Show file tree
Hide file tree
Showing 125 changed files with 19,295 additions and 6,988 deletions.
42 changes: 38 additions & 4 deletions bin/prompt-command.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
// based on https://github.com/webpack/webpack/blob/master/bin/webpack.js
module.exports = function promptForInstallation(packages, ...args) {

/**
* @param {string} command process to run
* @param {string[]} args commandline arguments
* @returns {Promise<void>} promise
*/
const runCommand = (command, args) => {
const cp = require("child_process");
return new Promise((resolve, reject) => {
resolve();
const executedCommand = cp.spawn(command, args, {
stdio: "inherit",
shell: true
});

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

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

module.exports = function promptForInstallation(packages, ...args) {
let packageIsInstalled = false;
try {
require.resolve(packages);
Expand All @@ -16,7 +44,8 @@ module.exports = function promptForInstallation(packages, ...args) {
const isYarn = fs.existsSync(path.resolve(process.cwd(), "yarn.lock"));

const packageManager = isYarn ? "yarn" : "npm";
const options = ["install", "-D", packages];
const nameOfPackage = "@webpack-cli/" + packages;
const options = ["install", "-D", nameOfPackage];

if (isYarn) {
options[0] = "add";
Expand All @@ -26,7 +55,9 @@ module.exports = function promptForInstallation(packages, ...args) {

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

console.error(`The command moved into a separate package: @webpack-cli/${packages}`);
console.error(
`The command moved into a separate package: ${nameOfPackage}`
);
const questionInterface = readLine.createInterface({
input: process.stdin,
output: process.stdout
Expand All @@ -40,6 +71,9 @@ module.exports = function promptForInstallation(packages, ...args) {
//eslint-disable-next-line
runCommand(packageManager, options)
.then(result => {
if (packages === "serve") {
return require(`@webpack-cli/${packages}`).serve();
}
return require(`@webpack-cli/${packages}`)(...args); //eslint-disable-line
})
.catch(error => {
Expand All @@ -50,7 +84,7 @@ module.exports = function promptForInstallation(packages, ...args) {
}
default: {
console.error(
"It needs to be installed alongside webpack CLI to use the command"
`${nameOfPackage} needs to be installed in order to run the command.`
);
process.exitCode = 1;
break;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
],
"scripts": {
"lint": "eslint ./bin/*.js ./packages/**/*.js ./test/**/*.js",
"format": "prettier-eslint ./utils/**/*.js ./cli.js ./test/**/*.js ./packages/**/*.js --write",
"format": "prettier-eslint ./bin/*.js ./test/**/*.js ./packages/**/*.js --write",
"lint:codeOnly": "eslint \"{utils}/**/!(__testfixtures__)/*.js\" \"{utils}/**.js\"",
"precommit": "lint-staged",
"pretest": "npm run lint",
Expand Down
Loading

0 comments on commit b02070d

Please sign in to comment.