11#!/usr/bin/env node
2- const { exec, execSync } = require ( "child_process" ) ;
3- const inquirer = require ( "inquirer" ) ;
4-
52function runCommand ( command , options ) {
3+ const cp = require ( "child_process" ) ;
64 return new Promise ( ( resolve , reject ) => {
75 const executedCommand = cp . spawn ( command , options , {
86 stdio : "inherit"
97 } ) ;
108
11- executedCommand . on ( "error" , ( error ) => {
9+ executedCommand . on ( "error" , error => {
1210 reject ( error ) ;
1311 } ) ;
1412
15- executedCommand . on ( "exit" , ( code ) => {
16- if ( code === 0 ) {
13+ executedCommand . on ( "exit" , code => {
14+ if ( code === 0 ) {
1715 resolve ( true ) ;
1816 } else {
1917 reject ( ) ;
@@ -23,69 +21,60 @@ function runCommand(command, options) {
2321}
2422
2523let webpackCliInstalled = false ;
26- // try {
27- // const blah = require("webpack-cli"); // eslint-disable-line node/no-missing-require, node/no-extraneous-require, node/no-unpublished-require
28- // webpackCliInstalled = true;
29- // } catch(e) {
30- // console.log("error", e);
31- // webpackCliInstalled = false;
32- // }
33-
3424try {
35- execSync ( "node -e require.resolve(' webpack-cli')" , { stdio : "ignore" } ) ;
25+ require . resolve ( " webpack-cli" ) ;
3626 webpackCliInstalled = true ;
3727} catch ( err ) {
3828 webpackCliInstalled = false ;
3929}
4030
41-
42- if ( webpackCliInstalled ) {
43- require ( "webpack-cli" ) ; // eslint-disable-line node/no-missing-require, node/no-extraneous-require, node/no-unpublished-require
44- } else {
31+ if ( ! webpackCliInstalled ) {
4532 const path = require ( "path" ) ;
4633 const fs = require ( "fs" ) ;
34+ const readLine = require ( "readline" ) ;
4735 const isYarn = fs . existsSync ( path . resolve ( process . cwd ( ) , "yarn.lock" ) ) ;
48- let command ;
4936
50- let packageManager ;
51- let options = [ ] ;
52- if ( isYarn ) {
53- packageManager = "yarn" ;
54- options = [ "add" , "-D" , "webpack-cli" ] ;
55- } else {
56- packageManager = "npm" ;
57- options = [ "install" , "--save-dev" , "webpack-cli" ] ;
37+ const packageManager = isYarn ? "yarn" : "npm" ;
38+ const options = [ "install" , "-D" , "webpack-cli" ] ;
39+
40+ if ( isYarn ) {
41+ options [ 0 ] = "add" ;
5842 }
5943
6044 const commandToBeRun = `${ packageManager } ${ options . join ( " " ) } ` ;
6145
62- const question = {
63- type : "confirm" ,
64- name : "shouldInstall" ,
65- message : `Would you like to install webpack-cli? (That will run ${ commandToBeRun } )` ,
66- default : true
67- } ;
46+ const question = `Would you like to install webpack-cli? (That will run ${ commandToBeRun } ) ` ;
6847
69- if ( isYarn ) {
70- command = "yarn add webpack-cli -D" ;
71- } else {
72- command = "npm install --save-dev webpack-cli" ;
73- }
74-
75- console . error ( "The CLI moved into a separate package: webpack-cli.\n" ) ;
76- inquirer . prompt ( question ) . then ( ( answer ) => {
77- if ( answer ) {
78- console . error ( "Installing webpack-cli" ) ;
79- runCommand ( packageManager , options ) . then ( ( result ) => {
80- require ( "webpack-cli" ) ; // eslint-disable-line
81- } ) . catch ( ( error ) => {
82- console . error ( error ) ;
83- } ) ;
84- } else {
85- process . exitCode ( 1 ) ;
48+ console . error ( "The CLI moved into a separate package: webpack-cli" ) ;
49+ const questionInterface = readLine . createInterface ( {
50+ input : process . stdin ,
51+ output : process . stdout
52+ } ) ;
53+ questionInterface . question ( question , answer => {
54+ switch ( answer . toLowerCase ( ) ) {
55+ case "y" :
56+ case "yes" :
57+ case "1" : {
58+ runCommand ( packageManager , options )
59+ . then ( result => {
60+ questionInterface . close ( ) ;
61+ return require ( "webpack-cli" ) ; //eslint-disable-line
62+ } )
63+ . catch ( error => {
64+ questionInterface . close ( ) ;
65+ console . error ( error ) ;
66+ } ) ;
67+ break ;
68+ }
69+ default : {
70+ console . error ( "The CLI moved into a separate package: webpack-cli" ) ;
71+ console . error ( "It needs to be installed alongside webpack to use the CLI" ) ;
72+ process . exitCode = 1 ;
73+ questionInterface . close ( ) ;
74+ break ;
75+ }
8676 }
8777 } ) ;
8878} else {
8979 require ( "webpack-cli" ) ; // eslint-disable-line
9080}
91-
0 commit comments