Skip to content

Commit 1cb0166

Browse files
committedMar 9, 2019
fix(init): support global installation
1 parent 48b3b23 commit 1cb0166

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed
 

‎bin/prompt-command.js

+36-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ const runCommand = (command, args) => {
2727
});
2828
};
2929

30+
const npmGlobalRoot = () => {
31+
const cp = require("child_process");
32+
return new Promise((resolve, reject) => {
33+
const command = cp.spawn("npm", ["root", "-g"]);
34+
command.on('error', (error) => reject(error));
35+
command.stdout.on('data', (data) => resolve(data.toString()));
36+
command.stderr.on('data', (data) => reject(data));
37+
})
38+
}
39+
3040
module.exports = function promptForInstallation(packages, ...args) {
3141
const nameOfPackage = "@webpack-cli/" + packages;
3242
let packageIsInstalled = false;
@@ -59,9 +69,18 @@ module.exports = function promptForInstallation(packages, ...args) {
5969
options[0] = "add";
6070
}
6171

72+
if (packages == 'init') {
73+
if (isYarn) {
74+
options.splice(1, 1); // remove '-D'
75+
options.splice(0, 0, "global");
76+
} else {
77+
options[1] = "-g";
78+
}
79+
}
80+
6281
const commandToBeRun = `${packageManager} ${options.join(" ")}`;
6382

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

6685
console.error(`The command moved into a separate package: ${nameOfPackage}`);
6786
const questionInterface = readLine.createInterface({
@@ -77,6 +96,22 @@ module.exports = function promptForInstallation(packages, ...args) {
7796

7897
runCommand(packageManager, options)
7998
.then(_=> {
99+
if (packages == "init") {
100+
npmGlobalRoot()
101+
.then((root) => {
102+
pathtoInit = path.resolve(root.trim(), "@webpack-cli", "init");
103+
return pathtoInit;
104+
})
105+
.then((pathForInit) => {
106+
return require(pathForInit).default(...args);
107+
})
108+
.catch((error) => {
109+
console.error(error);
110+
process.exitCode = 1;
111+
})
112+
return;
113+
}
114+
80115
pathForCmd = path.resolve(process.cwd(), "node_modules", "@webpack-cli", packages);
81116
if (packages === "serve") {
82117
return require(pathForCmd).default.serve();

0 commit comments

Comments
 (0)
Please sign in to comment.