Require directly, don't spawn

This commit is contained in:
Joe Stanton 2015-05-14 15:18:44 +01:00
parent 132f8fa3ce
commit 2e2e577a09
5 changed files with 79 additions and 114 deletions

37
cli.js
View File

@ -5,40 +5,3 @@
'use strict'; 'use strict';
module.exports = require('./local-cli/cli.js'); module.exports = require('./local-cli/cli.js');
var spawn = require('child_process').spawn;
var path = require('path');
function printUsage() {
console.log([
'Usage: react-native <command>',
'',
'Commands:',
' start: starts the webserver',
].join('\n'));
process.exit(1);
}
function run() {
var args = process.argv.slice(2);
if (args.length === 0) {
printUsage();
}
switch (args[0]) {
case 'start':
spawn('sh', [
path.resolve(__dirname, 'packager', 'packager.sh'),
'--projectRoots',
process.cwd(),
], {stdio: 'inherit'});
break;
default:
console.error('Command `%s` unrecognized', args[0]);
printUsage();
}
// Here goes any cli commands we need to
}
module.exports = {
run: run
};

74
init.js Executable file
View File

@ -0,0 +1,74 @@
'use strict';
var path = require('path');
var fs = require('fs');
var file = require('file');
function init(projectDir, appName) {
console.log('Setting up new React Native app in ' + projectDir);
var source = path.resolve(__dirname, 'Examples/SampleApp');
walk(source).forEach(function(f) {
f = f.replace(source + '/', ''); // Strip off absolute path
if(f === 'project.xcworkspace' || f === 'xcuserdata') { return; }
var replacements = {
'Examples/SampleApp/': '',
'../../Libraries/': 'node_modules/react-native/Libraries/',
'../../React/': 'node_modules/react-native/React/',
'SampleApp': appName
};
var dest = f.replace(new RegExp('SampleApp', 'g'), appName);
copyAndReplace(
path.resolve(source, f),
path.resolve(projectDir, dest),
replacements
);
});
}
function copyAndReplace(src, dest, replacements) {
if (fs.lstatSync(src).isDirectory()) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}
}
else {
var content = fs.readFileSync(src, 'utf8');
Object.keys(replacements).forEach(function(regex) {
content = content.replace(new RegExp(regex, 'g'), replacements[regex]);
});
fs.writeFileSync(dest, content);
}
}
function copyAndReplace2(src, dest, appName) {
if (fs.lstatSync(src).isDirectory()) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}
}
else {
var content = fs.readFileSync(src, 'utf8')
.replace(new RegExp('SampleApp', 'g'), appName)
.replace(new RegExp('Examples/' + appName + '/', 'g'), '')
.replace(new RegExp('../../Libraries/', 'g'), 'node_modules/react-native/Libraries/')
.replace(new RegExp('../../React/', 'g'), 'node_modules/react-native/React/');
fs.writeFileSync(dest, content);
}
}
function walk(current) {
if(fs.lstatSync(current).isDirectory()) {
var files = fs.readdirSync(current).map(function(child) {
child = path.join(current, child);
return walk(child);
});
return [].concat.apply([current], files);
} else {
return [current];
}
}
module.exports = init;

50
init.sh
View File

@ -1,50 +0,0 @@
#!/usr/bin/env node
'use strict';
var path = require('path');
var fs = require('fs');
var file = require('file');
if (process.argv.length === 0) {
console.log('Usage: ' + path.basename(__filename) + ' <ProjectNameInCamelCase>');
console.log('');
console.log('This script will bootstrap new React Native app in current folder');
process.exit(1);
}
var appName = process.argv[2];
var dest = process.cwd();
console.log('Setting up new React Native app in ' + dest);
console.log('');
main(dest, appName);
function cp(src, dest, appName) {
if (fs.lstatSync(src).isDirectory()) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}
}
else {
var content = fs.readFileSync(src, 'utf8')
.replace(new RegExp('SampleApp', 'g'), appName)
.replace(new RegExp('Examples/' + appName + '/', 'g'), '')
.replace(new RegExp('../../Libraries/', 'g'), 'node_modules/react-native/Libraries/')
.replace(new RegExp('../../React/', 'g'), 'node_modules/react-native/React/');
fs.writeFileSync(dest, content);
}
}
function main(dest, appName) {
var source = path.resolve(__dirname, 'Examples/SampleApp');
file.walk(source, function(error, _, dirs, files) {
if (error) { throw error; }
dirs.concat(files).forEach(function(f) {
f = f.replace(source + '/', ''); // Strip off absolute path
if (f === 'project.xcworkspace' || f === 'xcuserdata') { return; }
var newFile = f.replace(new RegExp('SampleApp', 'g'), appName);
cp(path.resolve(source, f), path.resolve(dest, newFile), appName);
});
});
}

View File

@ -43,14 +43,6 @@
"react-native-start": "packager/packager.sh" "react-native-start": "packager/packager.sh"
}, },
"dependencies": { "dependencies": {
"connect": "2.8.3",
"file": "^0.2.2",
"jstransform": "10.0.1",
"react-timer-mixin": "^0.13.1",
"react-tools": "0.13.0-rc2",
"rebound": "^0.0.12",
"source-map": "0.1.31",
"stacktrace-parser": "0.1.1",
"absolute-path": "0.0.0", "absolute-path": "0.0.0",
"bluebird": "^2.9.21", "bluebird": "^2.9.21",
"chalk": "^1.0.0", "chalk": "^1.0.0",

View File

@ -85,31 +85,17 @@ function init(name) {
start: 'node_modules/react-native/packager/packager.sh' start: 'node_modules/react-native/packager/packager.sh'
} }
}; };
fs.writeFileSync( fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify(packageJson));
path.join(root, 'package.json'),
JSON.stringify(packageJson, null, 2)
);
process.chdir(root); process.chdir(root);
var initCmd = path.resolve(__dirname, '..', 'init.sh') + ' ' + projectName; run('npm install --save react-native', function(e) {
run(initCmd, function(e) {
if (e) { if (e) {
console.error('initialization failed'); console.error('`npm install --save react-native` failed');
process.exit(1); process.exit(1);
} }
run('npm install --save react-native', function(e) { var cli = require(CLI_MODULE_PATH());
if (e) { cli.init(root, projectName);
console.error('`npm install --save react-native` failed');
process.exit(1);
}
console.log('Next steps:');
console.log('');
console.log(' Open ' + path.join(root, projectName) + '.xcodeproj in Xcode');
console.log(' Hit Run button');
console.log('');
});
}); });
} }