Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(init): fix init prompt installation #742

Merged
merged 6 commits into from
Mar 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions bin/prompt-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
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
Expand All @@ -28,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 @@ -71,7 +80,7 @@ module.exports = function promptForInstallation(packages, ...args) {

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 @@ -84,9 +93,25 @@ module.exports = function promptForInstallation(packages, ...args) {
case "y":
case "yes":
case "1": {
//eslint-disable-next-line

runCommand(packageManager, options)
.then(result => {
.then(_=> {
if (packages === "init") {
npmGlobalRoot()
.then((root) => {
const 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