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