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
}
function init() {
spawn('sh', [path.resolve(__dirname, 'init.sh')], {stdio:'inherit'});
function init(root, projectName) {
spawn(path.resolve(__dirname, 'init.sh'), [projectName], {stdio:'inherit'});
}
module.exports = {

View File

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

View File

@ -4,19 +4,22 @@
* Copyright 2004-present Facebook. All Rights Reserved.
*/
var spawn = require('child_process').spawn;
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;
var CLI_MODULE_PATH = path.resolve(
process.cwd(),
'node_modules',
'react-native',
'cli'
);
var CLI_MODULE_PATH = function() {
return path.resolve(
process.cwd(),
'node_modules',
'react-native',
'cli'
);
};
var cli;
try {
cli = require(CLI_MODULE_PATH);
cli = require(CLI_MODULE_PATH());
} catch(e) {}
if (cli) {
@ -25,13 +28,20 @@ if (cli) {
var args = process.argv.slice(2);
if (args.length === 0) {
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);
}
if (args[0] === 'init') {
init();
if (args[1]) {
init(args[1]);
} else {
console.error(
'Usage: react-native init <ProjectName>'
);
process.exit(1);
}
} else {
console.error(
'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(
'This will walk you through creating a new react-native project',
'in the current directory'
'This will walk you through creating a new React Native project in',
root
);
console.log('Running npm init');
run('npm init', function(e) {
if (!fs.existsSync(root)) {
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) {
console.error('npm init 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);
}
var cli = require(CLI_MODULE_PATH);
cli.init();
});
var cli = require(CLI_MODULE_PATH());
cli.init(root, projectName);
});
}