Merge pull request #178 from frantic/no-npm-init-in-cli

Allow react-native init <project-or-dir-name>
This commit is contained in:
Alexander Kotliarskyi 2015-03-22 14:09:51 -07:00
commit ca47be3d38
3 changed files with 48 additions and 28 deletions

4
cli.js
View File

@ -38,8 +38,8 @@ function run() {
// Here goes any cli commands we need to // Here goes any cli commands we need to
} }
function init() { function init(root, projectName) {
spawn('sh', [path.resolve(__dirname, 'init.sh')], {stdio:'inherit'}); spawn(path.resolve(__dirname, 'init.sh'), [projectName], {stdio:'inherit'});
} }
module.exports = { module.exports = {

View File

@ -14,7 +14,7 @@ def cp(src, dest, app_name)
end end
def main(dest, app_name) def main(dest, app_name)
source = File.expand_path("../../Examples/SampleApp", __FILE__) source = File.expand_path("../Examples/SampleApp", __FILE__)
files = Dir.chdir(source) { Dir["**/*"] } files = Dir.chdir(source) { Dir["**/*"] }
.reject { |file| file["project.xcworkspace"] || file["xcuserdata"] } .reject { |file| file["project.xcworkspace"] || file["xcuserdata"] }
.each { |file| .each { |file|

View File

@ -4,19 +4,22 @@
* Copyright 2004-present Facebook. All Rights Reserved. * Copyright 2004-present Facebook. All Rights Reserved.
*/ */
var spawn = require('child_process').spawn; var fs = require('fs');
var path = require('path'); var path = require('path');
var spawn = require('child_process').spawn;
var CLI_MODULE_PATH = path.resolve( var CLI_MODULE_PATH = function() {
process.cwd(), return path.resolve(
'node_modules', process.cwd(),
'react-native', 'node_modules',
'cli' 'react-native',
); 'cli'
);
};
var cli; var cli;
try { try {
cli = require(CLI_MODULE_PATH); cli = require(CLI_MODULE_PATH());
} catch(e) {} } catch(e) {}
if (cli) { if (cli) {
@ -25,13 +28,20 @@ if (cli) {
var args = process.argv.slice(2); var args = process.argv.slice(2);
if (args.length === 0) { if (args.length === 0) {
console.error( console.error(
'You did not pass any commands, did you mean to run init?' 'You did not pass any commands, did you mean to run `react-native init`?'
); );
process.exit(1); process.exit(1);
} }
if (args[0] === 'init') { if (args[0] === 'init') {
init(); if (args[1]) {
init(args[1]);
} else {
console.error(
'Usage: react-native init <ProjectName>'
);
process.exit(1);
}
} else { } else {
console.error( console.error(
'Command `%s` unrecognized.' + 'Command `%s` unrecognized.' +
@ -42,28 +52,38 @@ if (cli) {
} }
} }
function init() { function init(name) {
var root = path.resolve(name);
var projectName = path.basename(root);
console.log( console.log(
'This will walk you through creating a new react-native project', 'This will walk you through creating a new React Native project in',
'in the current directory' root
); );
console.log('Running npm init'); if (!fs.existsSync(root)) {
run('npm init', function(e) { fs.mkdirSync(root);
}
var packageJson = {
name: projectName,
version: '0.0.1',
private: true,
scripts: {
start: "react-native start"
}
};
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify(packageJson));
process.chdir(root);
run('npm install --save react-native', function(e) {
if (e) { if (e) {
console.error('npm init 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);
}
var cli = require(CLI_MODULE_PATH);
cli.init();
});
}); });
} }