@@ -40,19 +40,6 @@ if (commands.length === 0) {
40
40
41
41
createApp ( commands [ 0 ] , ! ! argv . verbose , argv [ 'scripts-version' ] ) . then ( ( ) => { } ) ;
42
42
43
- // use yarn if it's available, otherwise use npm
44
- function shouldUseYarn ( ) {
45
- try {
46
- const result = spawn . sync ( 'yarnpkg' , [ '--version' ] , { stdio : 'ignore' } ) ;
47
- if ( result . error || result . status !== 0 ) {
48
- return false ;
49
- }
50
- return true ;
51
- } catch ( e ) {
52
- return false ;
53
- }
54
- }
55
-
56
43
async function createApp ( name : string , verbose : boolean , version : ?string ) : Promise < void > {
57
44
const root = path . resolve ( name ) ;
58
45
const appName = path . basename ( root ) ;
@@ -91,37 +78,45 @@ function install(
91
78
verbose : boolean ,
92
79
callback : ( code : number , command : string , args : Array < string > ) => Promise < void >
93
80
) : void {
94
- const useYarn = shouldUseYarn ( ) ;
95
- let args , cmd, result ;
96
-
97
- if ( useYarn ) {
98
- cmd = 'yarnpkg' ;
99
- args = [ 'add' ] ;
81
+ let args = [ 'add' , '--dev' , '--exact' , '--ignore-optional' , packageToInstall ] ;
82
+ const proc = spawn ( 'yarnpkg' , args , { stdio : 'inherit' } ) ;
100
83
101
- if ( verbose ) {
102
- args . push ( '--verbose' ) ;
84
+ let yarnExists = true ;
85
+ proc . on ( 'error' , function ( err ) {
86
+ if ( err . code === 'ENOENT' ) {
87
+ yarnExists = false ;
103
88
}
89
+ } ) ;
104
90
105
- args = args . concat ( [ '--dev' , '--exact' , '--ignore-optional' , packageToInstall ] ) ;
106
- result = spawn . sync ( cmd , args , { stdio : 'inherit' } ) ;
107
- } else {
91
+ proc . on ( 'close' , function ( code ) {
92
+ if ( yarnExists ) {
93
+ callback ( code , 'yarnpkg' , args ) . then (
94
+ ( ) => { } ,
95
+ e => {
96
+ throw e ;
97
+ }
98
+ ) ;
99
+ return ;
100
+ }
101
+ // No Yarn installed, continuing with npm.
108
102
args = [ 'install' ] ;
109
103
110
104
if ( verbose ) {
111
105
args . push ( '--verbose' ) ;
112
106
}
113
- cmd = 'npm ';
114
- args = args . concat ( [ '--save-dev' , '--save-exact' , packageToInstall ] ) ;
115
107
116
- result = spawn . sync ( cmd , args , { stdio : 'inherit' } ) ;
117
- }
108
+ args = args . concat ( [ '--save-dev' , '--save-exact' , packageToInstall ] ) ;
118
109
119
- callback ( result . status , cmd , args ) . then (
120
- ( ) => { } ,
121
- e => {
122
- throw e ;
123
- }
124
- ) ;
110
+ const npmProc = spawn ( 'npm' , args , { stdio : 'inherit' } ) ;
111
+ npmProc . on ( 'close' , function ( code ) {
112
+ callback ( code , 'npm' , args ) . then (
113
+ ( ) => { } ,
114
+ e => {
115
+ throw e ;
116
+ }
117
+ ) ;
118
+ } ) ;
119
+ } ) ;
125
120
}
126
121
127
122
async function run (
0 commit comments