Skip to content

Commit 80d835a

Browse files
committed
1-1 port of init script to Nodejs
This will remove the dependency on Ruby.
1 parent 3c43410 commit 80d835a

5 files changed

Lines changed: 69 additions & 55 deletions

File tree

cli.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ function run() {
3838
// Here goes any cli commands we need to
3939
}
4040

41-
function init(root, projectName) {
42-
spawn(path.resolve(__dirname, 'init.sh'), [projectName], {stdio:'inherit'});
43-
}
44-
4541
module.exports = {
46-
run: run,
47-
init: init,
42+
run: run
4843
};

init.sh

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

react-native-cli/index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,28 @@ function init(name) {
8282
start: 'node_modules/react-native/packager/packager.sh'
8383
}
8484
};
85-
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify(packageJson));
85+
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify(packageJson, null, 2));
8686
process.chdir(root);
8787

88-
run('npm install --save react-native', function(e) {
88+
var initCmd = path.resolve(__dirname, 'init.sh') + ' ' + projectName;
89+
run(initCmd, function(e) {
8990
if (e) {
90-
console.error('`npm install --save react-native` failed');
91+
console.error('initialization failed');
9192
process.exit(1);
9293
}
9394

94-
var cli = require(CLI_MODULE_PATH());
95-
cli.init(root, projectName);
95+
run('npm install --save react-native', function(e) {
96+
if (e) {
97+
console.error('`npm install --save react-native` failed');
98+
process.exit(1);
99+
}
100+
101+
console.log('Next steps:');
102+
console.log('');
103+
console.log(' Open ' + path.join(root, projectName) + '.xcodeproj in Xcode');
104+
console.log(' Hit Run button');
105+
console.log('');
106+
});
96107
});
97108
}
98109

react-native-cli/init.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env node
2+
3+
var path = require('path');
4+
var fs = require('fs');
5+
var file = require('file');
6+
7+
if (process.argv.length === 0) {
8+
console.log('Usage: ' + path.basename(__filename) + ' <ProjectNameInCamelCase>');
9+
console.log('');
10+
console.log('This script will bootstrap new React Native app in current folder');
11+
process.exit(1);
12+
}
13+
14+
var appName = process.argv[2];
15+
var dest = process.cwd();
16+
console.log('Setting up new React Native app in ' + dest);
17+
console.log('');
18+
19+
main(dest, appName);
20+
21+
function cp(src, dest, appName) {
22+
if(fs.lstatSync(src).isDirectory()) {
23+
if(!fs.existsSync(dest)) {
24+
fs.mkdirSync(dest);
25+
}
26+
}
27+
else {
28+
var content = fs.readFileSync(src, 'utf8')
29+
.replace(new RegExp('SampleApp', 'g'), appName)
30+
.replace(new RegExp('Examples/' + appName + '/', 'g'), '')
31+
.replace(new RegExp('../../Libraries/', 'g'), 'node_modules/react-native/Libraries/')
32+
.replace(new RegExp('../../React/', 'g'), 'node_modules/react-native/React/');
33+
fs.writeFileSync(dest, content);
34+
}
35+
}
36+
37+
function main(dest, appName) {
38+
var source = path.resolve(__dirname, '../Examples/SampleApp');
39+
file.walk(source, function(error, _, dirs, files) {
40+
if (error) { throw error; }
41+
42+
dirs.concat(files).forEach(function(f) {
43+
f = f.replace(source + '/', ''); // Strip off absolute path
44+
if (f == 'project.xcworkspace' || f == 'xcuserdata') { return; }
45+
var newFile = f.replace(new RegExp('SampleApp', 'g'), appName);
46+
cp(path.resolve(source, f), path.resolve(dest, newFile), appName);
47+
});
48+
});
49+
}

react-native-cli/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
"main": "index.js",
66
"bin": {
77
"react-native": "index.js"
8+
},
9+
"dependencies": {
10+
"file": "^0.2.2"
811
}
912
}

0 commit comments

Comments
 (0)