@@ -39,7 +39,6 @@ var fs = require('fs');
39
39
var path = require ( 'path' ) ;
40
40
var exec = require ( 'child_process' ) . exec ;
41
41
var execSync = require ( 'child_process' ) . execSync ;
42
- var spawn = require ( 'child_process' ) . spawn ;
43
42
var chalk = require ( 'chalk' ) ;
44
43
var prompt = require ( 'prompt' ) ;
45
44
var semver = require ( 'semver' ) ;
@@ -55,7 +54,9 @@ var semver = require('semver');
55
54
* - "https://registry.npmjs.org/react-native/-/react-native-0.20.0.tgz" - a .tgz archive from any npm repo
56
55
* - "/Users/home/react-native/react-native-0.22.0.tgz" - for package prepared with `npm pack`, useful for e2e tests
57
56
*/
58
- var argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) ) ;
57
+
58
+ var options = require ( 'minimist' ) ( process . argv . slice ( 2 ) ) ;
59
+ checkForVersionArgument ( options ) ;
59
60
60
61
var CLI_MODULE_PATH = function ( ) {
61
62
return path . resolve (
@@ -102,20 +103,17 @@ function getYarnVersionIfAvailable() {
102
103
}
103
104
}
104
105
105
- checkForVersionArgument ( ) ;
106
-
107
106
var cli ;
108
107
var cliPath = CLI_MODULE_PATH ( ) ;
109
108
if ( fs . existsSync ( cliPath ) ) {
110
109
cli = require ( cliPath ) ;
111
110
}
112
111
113
- // minimist api
114
- var commands = argv . _ ;
112
+ var commands = options . _ ;
115
113
if ( cli ) {
116
114
cli . run ( ) ;
117
115
} else {
118
- if ( argv . _ . length === 0 && ( argv . h || argv . help ) ) {
116
+ if ( options . _ . length === 0 && ( options . h || options . help ) ) {
119
117
console . log ( [
120
118
'' ,
121
119
' Usage: react-native [command] [options]' ,
@@ -149,8 +147,7 @@ if (cli) {
149
147
) ;
150
148
process . exit ( 1 ) ;
151
149
} else {
152
- const rnPackage = argv . version ;
153
- init ( commands [ 1 ] , argv . verbose , rnPackage , argv . npm ) ;
150
+ init ( commands [ 1 ] , options ) ;
154
151
}
155
152
break ;
156
153
default :
@@ -186,22 +183,22 @@ function validateProjectName(name) {
186
183
187
184
/**
188
185
* @param name Project name, e.g. 'AwesomeApp'.
189
- * @param verbose If true, will run 'npm install' in verbose mode (for debugging).
190
- * @param rnPackage Version of React Native to install, e.g. '0.38.0'.
191
- * @param forceNpmClient If true, always use the npm command line client,
186
+ * @param options. verbose If true, will run 'npm install' in verbose mode (for debugging).
187
+ * @param options.version Version of React Native to install, e.g. '0.38.0'.
188
+ * @param options.npm If true, always use the npm command line client,
192
189
* don't use yarn even if available.
193
190
*/
194
- function init ( name , verbose , rnPackage , forceNpmClient ) {
191
+ function init ( name , options ) {
195
192
validateProjectName ( name ) ;
196
193
197
194
if ( fs . existsSync ( name ) ) {
198
- createAfterConfirmation ( name , verbose , rnPackage , forceNpmClient ) ;
195
+ createAfterConfirmation ( name , options ) ;
199
196
} else {
200
- createProject ( name , verbose , rnPackage , forceNpmClient ) ;
197
+ createProject ( name , options ) ;
201
198
}
202
199
}
203
200
204
- function createAfterConfirmation ( name , verbose , rnPackage , forceNpmClient ) {
201
+ function createAfterConfirmation ( name , options ) {
205
202
prompt . start ( ) ;
206
203
207
204
var property = {
@@ -214,15 +211,15 @@ function createAfterConfirmation(name, verbose, rnPackage, forceNpmClient) {
214
211
215
212
prompt . get ( property , function ( err , result ) {
216
213
if ( result . yesno [ 0 ] === 'y' ) {
217
- createProject ( name , verbose , rnPackage , forceNpmClient ) ;
214
+ createProject ( name , options ) ;
218
215
} else {
219
216
console . log ( 'Project initialization canceled' ) ;
220
217
process . exit ( ) ;
221
218
}
222
219
} ) ;
223
220
}
224
221
225
- function createProject ( name , verbose , rnPackage , forceNpmClient ) {
222
+ function createProject ( name , options ) {
226
223
var root = path . resolve ( name ) ;
227
224
var projectName = path . basename ( root ) ;
228
225
@@ -246,11 +243,7 @@ function createProject(name, verbose, rnPackage, forceNpmClient) {
246
243
fs . writeFileSync ( path . join ( root , 'package.json' ) , JSON . stringify ( packageJson ) ) ;
247
244
process . chdir ( root ) ;
248
245
249
- if ( verbose ) {
250
- runVerbose ( root , projectName , rnPackage , forceNpmClient ) ;
251
- } else {
252
- run ( root , projectName , rnPackage , forceNpmClient ) ;
253
- }
246
+ run ( root , projectName , options ) ;
254
247
}
255
248
256
249
function getInstallPackage ( rnPackage ) {
@@ -265,46 +258,45 @@ function getInstallPackage(rnPackage) {
265
258
return packageToInstall ;
266
259
}
267
260
268
- function run ( root , projectName , rnPackage , forceNpmClient ) {
261
+ function run ( root , projectName , options ) {
262
+ // E.g. '0.38' or '/path/to/archive.tgz'
263
+ const rnPackage = options . version ;
264
+ const forceNpmClient = options . npm ;
269
265
const yarnVersion = ( ! forceNpmClient ) && getYarnVersionIfAvailable ( ) ;
270
266
var installCommand ;
271
- if ( yarnVersion ) {
272
- console . log ( 'Using yarn v' + yarnVersion ) ;
273
- console . log ( 'Installing ' + getInstallPackage ( rnPackage ) + '...' ) ;
274
- installCommand = 'yarn add ' + getInstallPackage ( rnPackage ) + ' --exact' ;
267
+ if ( options . installCommand ) {
268
+ // In CI environments it can be useful to provide a custom command,
269
+ // to set up and use an offline mirror for installing dependencies, for example.
270
+ installCommand = options . installCommand ;
275
271
} else {
276
- console . log ( 'Installing ' + getInstallPackage ( rnPackage ) + '...' ) ;
277
- if ( ! forceNpmClient ) {
278
- console . log ( 'Consider installing yarn to make this faster: https://yarnpkg.com' ) ;
272
+ if ( yarnVersion ) {
273
+ console . log ( 'Using yarn v' + yarnVersion ) ;
274
+ console . log ( 'Installing ' + getInstallPackage ( rnPackage ) + '...' ) ;
275
+ installCommand = 'yarn add ' + getInstallPackage ( rnPackage ) + ' --exact' ;
276
+ if ( options . verbose ) {
277
+ installCommand += ' --verbose' ;
278
+ }
279
+ } else {
280
+ console . log ( 'Installing ' + getInstallPackage ( rnPackage ) + '. This might take a while...' ) ;
281
+ if ( ! forceNpmClient ) {
282
+ console . log ( 'Consider installing yarn to make this faster: https://yarnpkg.com' ) ;
283
+ }
284
+ installCommand = 'npm install --save --save-exact ' + getInstallPackage ( rnPackage ) ;
285
+ if ( options . verbose ) {
286
+ installCommand += ' --verbose' ;
287
+ }
279
288
}
280
- installCommand = 'npm install --save --save-exact ' + getInstallPackage ( rnPackage ) ;
281
289
}
282
- exec ( installCommand , function ( err , stdout , stderr ) {
283
- if ( err ) {
284
- console . log ( stdout ) ;
285
- console . error ( stderr ) ;
286
- console . error ( 'Command `' + installCommand + '` failed.' ) ;
287
- process . exit ( 1 ) ;
288
- }
289
- checkNodeVersion ( ) ;
290
- cli = require ( CLI_MODULE_PATH ( ) ) ;
291
- cli . init ( root , projectName ) ;
292
- } ) ;
293
- }
294
-
295
- function runVerbose ( root , projectName , rnPackage , forceNpmClient ) {
296
- // Use npm client, yarn doesn't support --verbose yet
297
- console . log ( 'Installing ' + getInstallPackage ( rnPackage ) + ' from npm. This might take a while...' ) ;
298
- var proc = spawn ( / ^ w i n / . test ( process . platform ) ? 'npm.cmd' : 'npm' , [ 'install' , '--verbose' , '--save' , '--save-exact' , getInstallPackage ( rnPackage ) ] , { stdio : 'inherit' } ) ;
299
- proc . on ( 'close' , function ( code ) {
300
- if ( code !== 0 ) {
301
- console . error ( '`npm install --save --save-exact react-native` failed' ) ;
302
- return ;
303
- }
304
-
305
- cli = require ( CLI_MODULE_PATH ( ) ) ;
306
- cli . init ( root , projectName ) ;
307
- } ) ;
290
+ try {
291
+ execSync ( installCommand , { stdio : 'inherit' } ) ;
292
+ } catch ( err ) {
293
+ console . error ( err ) ;
294
+ console . error ( 'Command `' + installCommand + '` failed.' ) ;
295
+ process . exit ( 1 ) ;
296
+ }
297
+ checkNodeVersion ( ) ;
298
+ cli = require ( CLI_MODULE_PATH ( ) ) ;
299
+ cli . init ( root , projectName ) ;
308
300
}
309
301
310
302
function checkNodeVersion ( ) {
@@ -323,8 +315,8 @@ function checkNodeVersion() {
323
315
}
324
316
}
325
317
326
- function checkForVersionArgument ( ) {
327
- if ( argv . _ . length === 0 && ( argv . v || argv . version ) ) {
318
+ function checkForVersionArgument ( options ) {
319
+ if ( options . _ . length === 0 && ( options . v || options . version ) ) {
328
320
console . log ( 'react-native-cli: ' + require ( './package.json' ) . version ) ;
329
321
try {
330
322
console . log ( 'react-native: ' + require ( REACT_NATIVE_PACKAGE_JSON_PATH ( ) ) . version ) ;
0 commit comments