@@ -27,6 +27,16 @@ const runCommand = (command, args) => {
27
27
} ) ;
28
28
} ;
29
29
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
+
30
40
module . exports = function promptForInstallation ( packages , ...args ) {
31
41
const nameOfPackage = "@webpack-cli/" + packages ;
32
42
let packageIsInstalled = false ;
@@ -59,9 +69,18 @@ module.exports = function promptForInstallation(packages, ...args) {
59
69
options [ 0 ] = "add" ;
60
70
}
61
71
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
+
62
81
const commandToBeRun = `${ packageManager } ${ options . join ( " " ) } ` ;
63
82
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) : ` ;
65
84
66
85
console . error ( `The command moved into a separate package: ${ nameOfPackage } ` ) ;
67
86
const questionInterface = readLine . createInterface ( {
@@ -77,6 +96,22 @@ module.exports = function promptForInstallation(packages, ...args) {
77
96
78
97
runCommand ( packageManager , options )
79
98
. 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
+
80
115
pathForCmd = path . resolve ( process . cwd ( ) , "node_modules" , "@webpack-cli" , packages ) ;
81
116
if ( packages === "serve" ) {
82
117
return require ( pathForCmd ) . default . serve ( ) ;
0 commit comments