react-native/local-cli/init.js

64 lines
1.7 KiB
JavaScript
Raw Normal View History

2015-05-14 14:18:44 +00:00
'use strict';
var path = require('path');
var fs = require('fs');
function init(projectDir, appName) {
console.log('Setting up new React Native app in ' + projectDir);
2015-05-14 17:08:56 +00:00
var source = path.resolve(__dirname, '..', 'Examples/SampleApp');
2015-05-14 14:18:44 +00:00
walk(source).forEach(function(f) {
f = f.replace(source + '/', ''); // Strip off absolute path
2015-05-14 19:05:32 +00:00
if (f === 'project.xcworkspace' || f === 'xcuserdata') {
return;
}
2015-05-14 14:18:44 +00:00
var replacements = {
'Examples/SampleApp/': '',
'../../Libraries/': 'node_modules/react-native/Libraries/',
'../../React/': 'node_modules/react-native/React/',
'SampleApp': appName
};
2015-05-14 19:05:32 +00:00
var dest = f.replace(/SampleApp/g, appName).replace(/^_/, '.');
2015-05-14 14:18:44 +00:00
copyAndReplace(
path.resolve(source, f),
path.resolve(projectDir, dest),
replacements
);
});
2015-05-14 14:35:09 +00:00
console.log('Next Steps:');
console.log(' Open ' + path.resolve(projectDir, appName) + '.xcodeproj in Xcode');
console.log(' Hit Run button');
console.log('');
2015-05-14 14:18:44 +00:00
}
function copyAndReplace(src, dest, replacements) {
if (fs.lstatSync(src).isDirectory()) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}
2015-05-14 19:05:32 +00:00
} else {
2015-05-14 14:18:44 +00:00
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 walk(current) {
2015-05-14 19:05:32 +00:00
if (!fs.lstatSync(current).isDirectory()) {
2015-05-14 14:18:44 +00:00
return [current];
}
2015-05-14 19:05:32 +00:00
var files = fs.readdirSync(current).map(function(child) {
child = path.join(current, child);
return walk(child);
});
return [].concat.apply([current], files);
2015-05-14 14:18:44 +00:00
}
module.exports = init;