Skip to content

Commit

Permalink
fix(init): support global installation
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh3112 committed Mar 9, 2019
1 parent 48b3b23 commit 1cb0166
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion bin/prompt-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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({
Expand All @@ -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();
Expand Down

0 comments on commit 1cb0166

Please sign in to comment.