Allow react-native init <project-or-dir-name>

This commit is contained in:
Alex Kotliarskyi 2015-03-22 13:27:32 -07:00
Родитель 616e29f723
Коммит df73a485cc
1 изменённых файлов: 33 добавлений и 18 удалений

51
react-native-cli/index.js поставляемый
Просмотреть файл

@ -4,8 +4,9 @@
* 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 = path.resolve(
process.cwd(), process.cwd(),
@ -25,13 +26,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 +50,35 @@ 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
};
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();
});
}); });
} }