2015-03-20 00:46:43 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
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-03-22 20:27:32 +00:00
|
|
|
var spawn = require('child_process').spawn;
|
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
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args[0] === '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-03-20 00:46:43 +00:00
|
|
|
} else {
|
|
|
|
console.error(
|
|
|
|
'Command `%s` unrecognized.' +
|
|
|
|
'Did you mean to run this inside a react-native project?',
|
|
|
|
args[0]
|
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-22 20:27:32 +00:00
|
|
|
function init(name) {
|
|
|
|
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 12:59:10 +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);
|
|
|
|
|
|
|
|
run('npm install --save react-native', function(e) {
|
2015-03-20 00:46:43 +00:00
|
|
|
if (e) {
|
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-03-22 21:04:55 +00:00
|
|
|
var 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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function run(command, cb) {
|
|
|
|
var parts = command.split(/\s+/);
|
|
|
|
var cmd = parts[0];
|
|
|
|
var args = parts.slice(1);
|
|
|
|
var proc = spawn(cmd, args, {stdio: 'inherit'});
|
|
|
|
proc.on('close', function(code) {
|
|
|
|
if (code !== 0) {
|
|
|
|
cb(new Error('Command exited with a non-zero status'));
|
|
|
|
} else {
|
|
|
|
cb(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|