44 * Copyright 2004-present Facebook. All Rights Reserved.
55 */
66
7- var spawn = require ( 'child_process' ) . spawn ;
7+ var fs = require ( 'fs' ) ;
88var path = require ( 'path' ) ;
9+ var spawn = require ( 'child_process' ) . spawn ;
910
10- var CLI_MODULE_PATH = path . resolve (
11- process . cwd ( ) ,
12- 'node_modules' ,
13- 'react-native' ,
14- 'cli'
15- ) ;
11+ var CLI_MODULE_PATH = function ( ) {
12+ return path . resolve (
13+ process . cwd ( ) ,
14+ 'node_modules' ,
15+ 'react-native' ,
16+ 'cli'
17+ ) ;
18+ } ;
1619
1720var cli ;
1821try {
19- cli = require ( CLI_MODULE_PATH ) ;
22+ cli = require ( CLI_MODULE_PATH ( ) ) ;
2023} catch ( e ) { }
2124
2225if ( cli ) {
@@ -25,13 +28,20 @@ if (cli) {
2528 var args = process . argv . slice ( 2 ) ;
2629 if ( args . length === 0 ) {
2730 console . error (
28- 'You did not pass any commands, did you mean to run init?'
31+ 'You did not pass any commands, did you mean to run `react-native init` ?'
2932 ) ;
3033 process . exit ( 1 ) ;
3134 }
3235
3336 if ( args [ 0 ] === 'init' ) {
34- init ( ) ;
37+ if ( args [ 1 ] ) {
38+ init ( args [ 1 ] ) ;
39+ } else {
40+ console . error (
41+ 'Usage: react-native init <ProjectName>'
42+ ) ;
43+ process . exit ( 1 ) ;
44+ }
3545 } else {
3646 console . error (
3747 'Command `%s` unrecognized.' +
@@ -42,28 +52,38 @@ if (cli) {
4252 }
4353}
4454
45- function init ( ) {
55+ function init ( name ) {
56+ var root = path . resolve ( name ) ;
57+ var projectName = path . basename ( root ) ;
58+
4659 console . log (
47- 'This will walk you through creating a new react-native project' ,
48- 'in the current directory'
60+ 'This will walk you through creating a new React Native project in ' ,
61+ root
4962 ) ;
5063
51- console . log ( 'Running npm init' ) ;
52- run ( 'npm init' , function ( e ) {
64+ if ( ! fs . existsSync ( root ) ) {
65+ fs . mkdirSync ( root ) ;
66+ }
67+
68+ var packageJson = {
69+ name : projectName ,
70+ version : '0.0.1' ,
71+ private : true ,
72+ scripts : {
73+ start : "react-native start"
74+ }
75+ } ;
76+ fs . writeFileSync ( path . join ( root , 'package.json' ) , JSON . stringify ( packageJson ) ) ;
77+ process . chdir ( root ) ;
78+
79+ run ( 'npm install --save react-native' , function ( e ) {
5380 if ( e ) {
54- console . error ( 'npm init failed' ) ;
81+ console . error ( '` npm install --save react-native` failed' ) ;
5582 process . exit ( 1 ) ;
5683 }
5784
58- run ( 'npm install --save react-native' , function ( e ) {
59- if ( e ) {
60- console . error ( '`npm install --save react-native` failed' ) ;
61- process . exit ( 1 ) ;
62- }
63-
64- var cli = require ( CLI_MODULE_PATH ) ;
65- cli . init ( ) ;
66- } ) ;
85+ var cli = require ( CLI_MODULE_PATH ( ) ) ;
86+ cli . init ( root , projectName ) ;
6787 } ) ;
6888}
6989
0 commit comments