Skip to content

Commit

Permalink
Require directly, don't spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeStanton committed May 14, 2015
1 parent 132f8fa commit 2f43e1d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 70 deletions.
4 changes: 3 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
module.exports = require('./local-cli/cli.js');
var spawn = require('child_process').spawn;
var path = require('path');
var init = require('./init');

function printUsage() {
console.log([
Expand Down Expand Up @@ -40,5 +41,6 @@ function run() {
}

module.exports = {
run: run
run: run,
init: init
};
74 changes: 74 additions & 0 deletions init.js
Original file line number Diff line number Diff line change
@@ -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 changes: 0 additions & 50 deletions init.sh

This file was deleted.

24 changes: 5 additions & 19 deletions react-native-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,31 +85,17 @@ function init(name) {
start: 'node_modules/react-native/packager/packager.sh'
}
};
fs.writeFileSync(
path.join(root, 'package.json'),
JSON.stringify(packageJson, null, 2)
);
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify(packageJson));
process.chdir(root);

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

run('npm install --save react-native', function(e) {
if (e) {
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('');
});
var cli = require(CLI_MODULE_PATH());
cli.init(root, projectName);
});
}

Expand Down

0 comments on commit 2f43e1d

Please sign in to comment.