Skip to content

Commit 2e2e577

Browse files
committed
Require directly, don't spawn
1 parent 132f8fa commit 2e2e577

File tree

5 files changed

+79
-114
lines changed

5 files changed

+79
-114
lines changed

cli.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,3 @@
55
'use strict';
66

77
module.exports = require('./local-cli/cli.js');
8-
var spawn = require('child_process').spawn;
9-
var path = require('path');
10-
11-
function printUsage() {
12-
console.log([
13-
'Usage: react-native <command>',
14-
'',
15-
'Commands:',
16-
' start: starts the webserver',
17-
].join('\n'));
18-
process.exit(1);
19-
}
20-
21-
function run() {
22-
var args = process.argv.slice(2);
23-
if (args.length === 0) {
24-
printUsage();
25-
}
26-
27-
switch (args[0]) {
28-
case 'start':
29-
spawn('sh', [
30-
path.resolve(__dirname, 'packager', 'packager.sh'),
31-
'--projectRoots',
32-
process.cwd(),
33-
], {stdio: 'inherit'});
34-
break;
35-
default:
36-
console.error('Command `%s` unrecognized', args[0]);
37-
printUsage();
38-
}
39-
// Here goes any cli commands we need to
40-
}
41-
42-
module.exports = {
43-
run: run
44-
};

init.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict';
2+
3+
var path = require('path');
4+
var fs = require('fs');
5+
var file = require('file');
6+
7+
function init(projectDir, appName) {
8+
console.log('Setting up new React Native app in ' + projectDir);
9+
var source = path.resolve(__dirname, 'Examples/SampleApp');
10+
11+
walk(source).forEach(function(f) {
12+
f = f.replace(source + '/', ''); // Strip off absolute path
13+
if(f === 'project.xcworkspace' || f === 'xcuserdata') { return; }
14+
15+
var replacements = {
16+
'Examples/SampleApp/': '',
17+
'../../Libraries/': 'node_modules/react-native/Libraries/',
18+
'../../React/': 'node_modules/react-native/React/',
19+
'SampleApp': appName
20+
};
21+
22+
var dest = f.replace(new RegExp('SampleApp', 'g'), appName);
23+
copyAndReplace(
24+
path.resolve(source, f),
25+
path.resolve(projectDir, dest),
26+
replacements
27+
);
28+
});
29+
}
30+
31+
function copyAndReplace(src, dest, replacements) {
32+
if (fs.lstatSync(src).isDirectory()) {
33+
if (!fs.existsSync(dest)) {
34+
fs.mkdirSync(dest);
35+
}
36+
}
37+
else {
38+
var content = fs.readFileSync(src, 'utf8');
39+
Object.keys(replacements).forEach(function(regex) {
40+
content = content.replace(new RegExp(regex, 'g'), replacements[regex]);
41+
});
42+
fs.writeFileSync(dest, content);
43+
}
44+
}
45+
46+
function copyAndReplace2(src, dest, appName) {
47+
if (fs.lstatSync(src).isDirectory()) {
48+
if (!fs.existsSync(dest)) {
49+
fs.mkdirSync(dest);
50+
}
51+
}
52+
else {
53+
var content = fs.readFileSync(src, 'utf8')
54+
.replace(new RegExp('SampleApp', 'g'), appName)
55+
.replace(new RegExp('Examples/' + appName + '/', 'g'), '')
56+
.replace(new RegExp('../../Libraries/', 'g'), 'node_modules/react-native/Libraries/')
57+
.replace(new RegExp('../../React/', 'g'), 'node_modules/react-native/React/');
58+
fs.writeFileSync(dest, content);
59+
}
60+
}
61+
62+
function walk(current) {
63+
if(fs.lstatSync(current).isDirectory()) {
64+
var files = fs.readdirSync(current).map(function(child) {
65+
child = path.join(current, child);
66+
return walk(child);
67+
});
68+
return [].concat.apply([current], files);
69+
} else {
70+
return [current];
71+
}
72+
}
73+
74+
module.exports = init;

init.sh

Lines changed: 0 additions & 50 deletions
This file was deleted.

package.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@
4343
"react-native-start": "packager/packager.sh"
4444
},
4545
"dependencies": {
46-
"connect": "2.8.3",
47-
"file": "^0.2.2",
48-
"jstransform": "10.0.1",
49-
"react-timer-mixin": "^0.13.1",
50-
"react-tools": "0.13.0-rc2",
51-
"rebound": "^0.0.12",
52-
"source-map": "0.1.31",
53-
"stacktrace-parser": "0.1.1",
5446
"absolute-path": "0.0.0",
5547
"bluebird": "^2.9.21",
5648
"chalk": "^1.0.0",

react-native-cli/index.js

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,31 +85,17 @@ function init(name) {
8585
start: 'node_modules/react-native/packager/packager.sh'
8686
}
8787
};
88-
fs.writeFileSync(
89-
path.join(root, 'package.json'),
90-
JSON.stringify(packageJson, null, 2)
91-
);
88+
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify(packageJson));
9289
process.chdir(root);
9390

94-
var initCmd = path.resolve(__dirname, '..', 'init.sh') + ' ' + projectName;
95-
run(initCmd, function(e) {
91+
run('npm install --save react-native', function(e) {
9692
if (e) {
97-
console.error('initialization failed');
93+
console.error('`npm install --save react-native` failed');
9894
process.exit(1);
9995
}
10096

101-
run('npm install --save react-native', function(e) {
102-
if (e) {
103-
console.error('`npm install --save react-native` failed');
104-
process.exit(1);
105-
}
106-
107-
console.log('Next steps:');
108-
console.log('');
109-
console.log(' Open ' + path.join(root, projectName) + '.xcodeproj in Xcode');
110-
console.log(' Hit Run button');
111-
console.log('');
112-
});
97+
var cli = require(CLI_MODULE_PATH());
98+
cli.init(root, projectName);
11399
});
114100
}
115101

0 commit comments

Comments
 (0)