2015-03-20 00:46:43 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
2015-09-06 21:06:33 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-03-22 20:27:32 +00:00
|
|
|
var fs = require('fs');
|
2015-03-20 00:46:43 +00:00
|
|
|
var path = require('path');
|
2015-08-07 17:59:59 +00:00
|
|
|
var exec = require('child_process').exec;
|
2015-09-06 21:06:33 +00:00
|
|
|
var prompt = require('prompt');
|
2015-03-20 00:46:43 +00:00
|
|
|
|
2015-03-22 21:04:55 +00:00
|
|
|
var CLI_MODULE_PATH = function() {
|
|
|
|
return path.resolve(
|
|
|
|
process.cwd(),
|
|
|
|
'node_modules',
|
|
|
|
'react-native',
|
|
|
|
'cli'
|
|
|
|
);
|
|
|
|
};
|
2015-03-20 00:46:43 +00:00
|
|
|
|
2015-09-06 21:06:33 +00:00
|
|
|
checkForVersionArgument();
|
|
|
|
|
2015-03-20 00:46:43 +00:00
|
|
|
var cli;
|
|
|
|
try {
|
2015-03-22 21:04:55 +00:00
|
|
|
cli = require(CLI_MODULE_PATH());
|
2015-03-20 00:46:43 +00:00
|
|
|
} catch(e) {}
|
|
|
|
|
|
|
|
if (cli) {
|
|
|
|
cli.run();
|
|
|
|
} else {
|
|
|
|
var args = process.argv.slice(2);
|
|
|
|
if (args.length === 0) {
|
|
|
|
console.error(
|
2015-03-22 20:27:32 +00:00
|
|
|
'You did not pass any commands, did you mean to run `react-native init`?'
|
2015-03-20 00:46:43 +00:00
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2015-04-04 01:00:23 +00:00
|
|
|
switch (args[0]) {
|
|
|
|
case 'init':
|
2015-03-22 20:27:32 +00:00
|
|
|
if (args[1]) {
|
|
|
|
init(args[1]);
|
|
|
|
} else {
|
|
|
|
console.error(
|
|
|
|
'Usage: react-native init <ProjectName>'
|
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2015-04-04 01:00:23 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-03-20 00:46:43 +00:00
|
|
|
console.error(
|
2015-03-27 04:12:18 +00:00
|
|
|
'Command `%s` unrecognized. ' +
|
2015-03-20 00:46:43 +00:00
|
|
|
'Did you mean to run this inside a react-native project?',
|
|
|
|
args[0]
|
|
|
|
);
|
|
|
|
process.exit(1);
|
2015-04-04 01:00:23 +00:00
|
|
|
break;
|
2015-03-20 00:46:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 08:21:38 +00:00
|
|
|
function validatePackageName(name) {
|
|
|
|
if (!name.match(/^[$A-Z_][0-9A-Z_$]*$/i)) {
|
|
|
|
console.error(
|
|
|
|
'"%s" is not a valid name for a project. Please use a valid identifier ' +
|
|
|
|
'name (alphanumeric).',
|
|
|
|
name
|
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2015-05-23 20:28:04 +00:00
|
|
|
|
|
|
|
if (name === 'React') {
|
|
|
|
console.error(
|
2015-05-23 22:59:33 +00:00
|
|
|
'"%s" is not a valid name for a project. Please do not use the ' +
|
|
|
|
'reserved word "React".',
|
2015-05-23 20:28:04 +00:00
|
|
|
name
|
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2015-05-15 08:21:38 +00:00
|
|
|
}
|
|
|
|
|
2015-03-22 20:27:32 +00:00
|
|
|
function init(name) {
|
2015-05-15 08:21:38 +00:00
|
|
|
validatePackageName(name);
|
2015-03-26 18:19:18 +00:00
|
|
|
|
2015-05-15 07:51:55 +00:00
|
|
|
if (fs.existsSync(name)) {
|
2015-09-06 21:06:33 +00:00
|
|
|
createAfterConfirmation(name);
|
2015-05-15 07:51:55 +00:00
|
|
|
} else {
|
|
|
|
createProject(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createAfterConfirmation(name) {
|
2015-05-18 17:25:17 +00:00
|
|
|
prompt.start();
|
2015-05-15 07:51:55 +00:00
|
|
|
|
2015-05-18 17:25:17 +00:00
|
|
|
var property = {
|
|
|
|
name: 'yesno',
|
|
|
|
message: 'Directory ' + name + ' already exist. Continue?',
|
|
|
|
validator: /y[es]*|n[o]?/,
|
|
|
|
warning: 'Must respond yes or no',
|
|
|
|
default: 'no'
|
|
|
|
};
|
2015-05-15 07:51:55 +00:00
|
|
|
|
2015-05-18 17:25:17 +00:00
|
|
|
prompt.get(property, function (err, result) {
|
|
|
|
if (result.yesno[0] === 'y') {
|
|
|
|
createProject(name);
|
|
|
|
} else {
|
|
|
|
console.log('Project initialization canceled');
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
});
|
2015-05-15 07:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createProject(name) {
|
2015-03-22 20:27:32 +00:00
|
|
|
var root = path.resolve(name);
|
|
|
|
var projectName = path.basename(root);
|
|
|
|
|
2015-03-20 00:46:43 +00:00
|
|
|
console.log(
|
2015-03-22 20:27:32 +00:00
|
|
|
'This will walk you through creating a new React Native project in',
|
|
|
|
root
|
2015-03-20 00:46:43 +00:00
|
|
|
);
|
|
|
|
|
2015-03-22 20:27:32 +00:00
|
|
|
if (!fs.existsSync(root)) {
|
|
|
|
fs.mkdirSync(root);
|
|
|
|
}
|
|
|
|
|
|
|
|
var packageJson = {
|
|
|
|
name: projectName,
|
|
|
|
version: '0.0.1',
|
2015-03-22 21:04:55 +00:00
|
|
|
private: true,
|
|
|
|
scripts: {
|
2015-03-26 17:47:05 +00:00
|
|
|
start: 'node_modules/react-native/packager/packager.sh'
|
2015-03-22 21:04:55 +00:00
|
|
|
}
|
2015-03-22 20:27:32 +00:00
|
|
|
};
|
|
|
|
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify(packageJson));
|
|
|
|
process.chdir(root);
|
|
|
|
|
2015-08-07 17:59:59 +00:00
|
|
|
console.log('Installing react-native package from npm...');
|
|
|
|
exec('npm install --save react-native', function(e, stdout, stderr) {
|
2015-03-20 00:46:43 +00:00
|
|
|
if (e) {
|
2015-08-07 17:59:59 +00:00
|
|
|
console.log(stdout);
|
|
|
|
console.error(stderr);
|
2015-03-22 20:27:32 +00:00
|
|
|
console.error('`npm install --save react-native` failed');
|
2015-03-20 00:46:43 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2015-09-06 21:06:33 +00:00
|
|
|
cli = require(CLI_MODULE_PATH());
|
2015-03-22 20:27:32 +00:00
|
|
|
cli.init(root, projectName);
|
2015-03-20 00:46:43 +00:00
|
|
|
});
|
|
|
|
}
|
2015-09-06 21:06:33 +00:00
|
|
|
|
|
|
|
function checkForVersionArgument() {
|
|
|
|
if (process.argv.indexOf('-v') >= 0 || process.argv.indexOf('--version') >= 0) {
|
|
|
|
var pjson = require('./package.json');
|
|
|
|
console.log(pjson.version);
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
}
|