2016-05-21 13:53:32 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2016-07-30 15:59:16 +00:00
|
|
|
*
|
|
|
|
* @flow
|
2016-05-21 13:53:32 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
const commander = require('commander');
|
|
|
|
|
2016-05-21 13:53:32 +00:00
|
|
|
const Config = require('./util/Config');
|
2016-07-30 15:59:16 +00:00
|
|
|
const childProcess = require('child_process');
|
2016-05-21 13:53:32 +00:00
|
|
|
const Promise = require('promise');
|
2016-07-30 15:59:16 +00:00
|
|
|
const chalk = require('chalk');
|
|
|
|
const path = require('path');
|
2016-05-21 13:53:32 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const gracefulFs = require('graceful-fs');
|
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
const init = require('./init/init');
|
|
|
|
const commands = require('./commands');
|
|
|
|
const assertRequiredOptions = require('./util/assertRequiredOptions');
|
|
|
|
const pkg = require('../package.json');
|
|
|
|
const defaultConfig = require('./default.config');
|
|
|
|
|
|
|
|
import type { Command } from './commands';
|
2016-06-03 16:05:14 +00:00
|
|
|
|
2016-05-21 13:53:32 +00:00
|
|
|
// graceful-fs helps on getting an error when we run out of file
|
|
|
|
// descriptors. When that happens it will enqueue the operation and retry it.
|
|
|
|
gracefulFs.gracefulify(fs);
|
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
commander.version(pkg.version);
|
2016-05-21 13:53:32 +00:00
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
const defaultOptParser = (val) => val;
|
2016-05-21 13:53:32 +00:00
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
const handleError = (err) => {
|
|
|
|
console.error();
|
|
|
|
console.error(err.message || err);
|
|
|
|
console.error();
|
|
|
|
process.exit(1);
|
2016-05-21 13:53:32 +00:00
|
|
|
};
|
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
// Custom printHelpInformation command inspired by internal Commander.js
|
|
|
|
// one modified to suit our needs
|
|
|
|
function printHelpInformation() {
|
|
|
|
let cmdName = this._name;
|
|
|
|
if (this._alias) {
|
|
|
|
cmdName = cmdName + '|' + this._alias;
|
2016-05-21 13:53:32 +00:00
|
|
|
}
|
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
let output = [
|
|
|
|
'',
|
2016-08-03 15:18:26 +00:00
|
|
|
chalk.bold(chalk.cyan((` react-native ${cmdName} ${this.usage()}`))),
|
2016-07-30 15:59:16 +00:00
|
|
|
` ${this._description}`,
|
|
|
|
'',
|
|
|
|
` ${chalk.bold('Options:')}`,
|
|
|
|
'',
|
|
|
|
this.optionHelp().replace(/^/gm, ' '),
|
|
|
|
'',
|
|
|
|
];
|
2016-05-21 13:53:32 +00:00
|
|
|
|
2016-08-03 15:18:26 +00:00
|
|
|
if (this.examples && this.examples.length > 0) {
|
|
|
|
const formattedUsage = this.examples.map(
|
2016-07-30 15:59:16 +00:00
|
|
|
example => ` ${example.desc}: \n ${chalk.cyan(example.cmd)}`,
|
|
|
|
).join('\n\n');
|
|
|
|
|
|
|
|
output = output.concat([
|
|
|
|
chalk.bold(' Example usage:'),
|
|
|
|
'',
|
|
|
|
formattedUsage,
|
|
|
|
]);
|
|
|
|
}
|
2016-05-21 13:53:32 +00:00
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
return output.concat([
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
]).join('\n');
|
2016-05-21 13:53:32 +00:00
|
|
|
}
|
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
function printUnknownCommand(cmdName) {
|
2016-05-21 13:53:32 +00:00
|
|
|
console.log([
|
|
|
|
'',
|
2016-07-30 15:59:16 +00:00
|
|
|
cmdName
|
|
|
|
? chalk.red(` Unrecognized command '${cmdName}'`)
|
|
|
|
: chalk.red(' You didn\'t pass any command'),
|
|
|
|
` Run ${chalk.cyan('react-native --help')} to see list of all available commands`,
|
|
|
|
'',
|
|
|
|
].join('\n'));
|
2016-05-21 13:53:32 +00:00
|
|
|
}
|
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
const addCommand = (command: Command, config: Config) => {
|
|
|
|
const options = command.options || [];
|
|
|
|
|
|
|
|
const cmd = commander
|
|
|
|
.command(command.name, undefined, {
|
|
|
|
noHelp: !command.description,
|
|
|
|
})
|
|
|
|
.description(command.description)
|
|
|
|
.action(function runAction() {
|
|
|
|
const passedOptions = this.opts();
|
|
|
|
const argv: Array<string> = Array.from(arguments).slice(0, -1);
|
|
|
|
|
|
|
|
Promise.resolve()
|
|
|
|
.then(() => {
|
|
|
|
assertRequiredOptions(options, passedOptions);
|
|
|
|
return command.func(argv, config, passedOptions);
|
|
|
|
})
|
|
|
|
.catch(handleError);
|
|
|
|
});
|
|
|
|
|
|
|
|
cmd.helpInformation = printHelpInformation.bind(cmd);
|
2016-08-03 15:18:26 +00:00
|
|
|
cmd.examples = command.examples;
|
2016-07-30 15:59:16 +00:00
|
|
|
|
|
|
|
options
|
|
|
|
.forEach(opt => cmd.option(
|
|
|
|
opt.command,
|
|
|
|
opt.description,
|
|
|
|
opt.parse || defaultOptParser,
|
|
|
|
typeof opt.default === 'function' ? opt.default(config) : opt.default,
|
|
|
|
));
|
|
|
|
};
|
2016-05-21 13:53:32 +00:00
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
function run() {
|
|
|
|
const config = Config.get(__dirname, defaultConfig);
|
|
|
|
const setupEnvScript = /^win/.test(process.platform)
|
|
|
|
? 'setup_env.bat'
|
|
|
|
: 'setup_env.sh';
|
|
|
|
|
|
|
|
childProcess.execFileSync(path.join(__dirname, setupEnvScript));
|
|
|
|
|
|
|
|
commands.forEach(cmd => addCommand(cmd, config));
|
|
|
|
|
|
|
|
commander.parse(process.argv);
|
|
|
|
|
2016-08-03 07:53:05 +00:00
|
|
|
const isValidCommand = commands.find(cmd => cmd.name.split(' ')[0] === process.argv[2]);
|
2016-07-30 15:59:16 +00:00
|
|
|
|
|
|
|
if (!isValidCommand) {
|
|
|
|
printUnknownCommand(process.argv[2]);
|
|
|
|
return;
|
2016-05-21 13:53:32 +00:00
|
|
|
}
|
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
if (!commander.args.length) {
|
|
|
|
commander.help();
|
|
|
|
}
|
2016-05-21 13:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
run: run,
|
|
|
|
init: init,
|
|
|
|
};
|