@@ -59,6 +59,7 @@ var path = require('path');
59
59
var execSync = require ( 'child_process' ) . execSync ;
60
60
var spawn = require ( 'cross-spawn' ) ;
61
61
var semver = require ( 'semver' ) ;
62
+ var dns = require ( 'dns' ) ;
62
63
63
64
var projectName ;
64
65
@@ -154,25 +155,44 @@ function shouldUseYarn() {
154
155
}
155
156
}
156
157
157
- function install ( dependencies , verbose , callback ) {
158
- var command ;
159
- var args ;
160
- if ( shouldUseYarn ( ) ) {
161
- command = 'yarnpkg' ;
162
- args = [ 'add' , '--exact' ] . concat ( dependencies ) ;
163
- } else {
164
- checkNpmVersion ( ) ;
165
- command = 'npm' ;
166
- args = [ 'install' , '--save' , '--save-exact' ] . concat ( dependencies ) ;
167
- }
158
+ function install ( useYarn , dependencies , verbose , isOnline ) {
159
+ return new Promise ( function ( resolve , reject ) {
160
+ var command ;
161
+ var args ;
162
+ if ( useYarn ) {
163
+ command = 'yarnpkg' ;
164
+ args = [
165
+ 'add' ,
166
+ '--exact' ,
167
+ isOnline === false && '--offline'
168
+ ] . concat ( dependencies ) ;
169
+
170
+ if ( ! isOnline ) {
171
+ console . log ( chalk . yellow ( 'You appear to be offline.' ) ) ;
172
+ console . log ( chalk . yellow ( 'Falling back to the local Yarn cache.' ) ) ;
173
+ console . log ( ) ;
174
+ }
168
175
169
- if ( verbose ) {
170
- args . push ( '--verbose' ) ;
171
- }
176
+ } else {
177
+ checkNpmVersion ( ) ;
178
+ command = 'npm' ;
179
+ args = [ 'install' , '--save' , '--save-exact' ] . concat ( dependencies ) ;
180
+ }
172
181
173
- var child = spawn ( command , args , { stdio : 'inherit' } ) ;
174
- child . on ( 'close' , function ( code ) {
175
- callback ( code , command , args ) ;
182
+ if ( verbose ) {
183
+ args . push ( '--verbose' ) ;
184
+ }
185
+
186
+ var child = spawn ( command , args , { stdio : 'inherit' } ) ;
187
+ child . on ( 'close' , function ( code ) {
188
+ if ( code !== 0 ) {
189
+ reject ( {
190
+ command : command + ' ' + args . join ( ' ' )
191
+ } ) ;
192
+ return ;
193
+ }
194
+ resolve ( ) ;
195
+ } ) ;
176
196
} ) ;
177
197
}
178
198
@@ -188,11 +208,38 @@ function run(root, appName, version, verbose, originalDirectory, template) {
188
208
', and ' + chalk . cyan ( packageName ) + '...'
189
209
) ;
190
210
console . log ( ) ;
191
-
192
- install ( allDependencies , verbose , function ( code , command , args ) {
193
- if ( code !== 0 ) {
211
+
212
+ var useYarn = shouldUseYarn ( ) ;
213
+ checkIfOnline ( useYarn )
214
+ . then ( function ( isOnline ) {
215
+ return install ( useYarn , allDependencies , verbose , isOnline ) ;
216
+ } )
217
+ . then ( function ( ) {
218
+ checkNodeVersion ( packageName ) ;
219
+
220
+ // Since react-scripts has been installed with --save
221
+ // we need to move it into devDependencies and rewrite package.json
222
+ // also ensure react dependencies have caret version range
223
+ fixDependencies ( packageName ) ;
224
+
225
+ var scriptsPath = path . resolve (
226
+ process . cwd ( ) ,
227
+ 'node_modules' ,
228
+ packageName ,
229
+ 'scripts' ,
230
+ 'init.js'
231
+ ) ;
232
+ var init = require ( scriptsPath ) ;
233
+ init ( root , appName , verbose , originalDirectory , template ) ;
234
+ } )
235
+ . catch ( function ( reason ) {
194
236
console . log ( ) ;
195
- console . error ( 'Aborting installation.' , chalk . cyan ( command + ' ' + args . join ( ' ' ) ) , 'has failed.' ) ;
237
+ console . log ( 'Aborting installation.' ) ;
238
+ if ( reason . command ) {
239
+ console . log ( ' ' + chalk . cyan ( reason . command ) , 'has failed.' )
240
+ }
241
+ console . log ( ) ;
242
+
196
243
// On 'exit' we will delete these files from target directory.
197
244
var knownGeneratedFiles = [
198
245
'package.json' , 'npm-debug.log' , 'yarn-error.log' , 'yarn-debug.log' , 'node_modules'
@@ -217,25 +264,7 @@ function run(root, appName, version, verbose, originalDirectory, template) {
217
264
}
218
265
console . log ( 'Done.' ) ;
219
266
process . exit ( 1 ) ;
220
- }
221
-
222
- checkNodeVersion ( packageName ) ;
223
-
224
- // Since react-scripts has been installed with --save
225
- // we need to move it into devDependencies and rewrite package.json
226
- // also ensure react dependencies have caret version range
227
- fixDependencies ( packageName ) ;
228
-
229
- var scriptsPath = path . resolve (
230
- process . cwd ( ) ,
231
- 'node_modules' ,
232
- packageName ,
233
- 'scripts' ,
234
- 'init.js'
235
- ) ;
236
- var init = require ( scriptsPath ) ;
237
- init ( root , appName , verbose , originalDirectory , template ) ;
238
- } ) ;
267
+ } ) ;
239
268
}
240
269
241
270
function getInstallPackage ( version ) {
@@ -408,3 +437,17 @@ function isSafeToCreateProjectIn(root) {
408
437
return validFiles . indexOf ( file ) >= 0 ;
409
438
} ) ;
410
439
}
440
+
441
+ function checkIfOnline ( useYarn ) {
442
+ if ( ! useYarn ) {
443
+ // Don't ping the Yarn registry.
444
+ // We'll just assume the best case.
445
+ return Promise . resolve ( true ) ;
446
+ }
447
+
448
+ return new Promise ( function ( resolve ) {
449
+ dns . resolve ( 'registry.yarnpkg.com' , function ( err ) {
450
+ resolve ( err === null ) ;
451
+ } ) ;
452
+ } ) ;
453
+ }
0 commit comments