2016-05-21 13:53:32 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-07-30 15:59:16 +00:00
|
|
|
*
|
2018-05-11 19:43:49 +00:00
|
|
|
* @format
|
2016-07-30 15:59:16 +00:00
|
|
|
* @flow
|
2016-05-21 13:53:32 +00:00
|
|
|
*/
|
2018-05-11 19:43:49 +00:00
|
|
|
|
2016-05-21 13:53:32 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-07-25 12:44:40 +00:00
|
|
|
const {configPromise} = require('./core');
|
2016-10-14 18:11:35 +00:00
|
|
|
|
|
|
|
const assertRequiredOptions = require('./util/assertRequiredOptions');
|
2017-09-06 10:25:01 +00:00
|
|
|
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
|
|
|
|
* found when Flow v0.54 was deployed. To see the error delete this comment and
|
|
|
|
* run Flow. */
|
2016-07-30 15:59:16 +00:00
|
|
|
const chalk = require('chalk');
|
2016-10-14 18:11:35 +00:00
|
|
|
const childProcess = require('child_process');
|
2017-09-06 10:25:01 +00:00
|
|
|
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
|
|
|
|
* found when Flow v0.54 was deployed. To see the error delete this comment and
|
|
|
|
* run Flow. */
|
2016-10-14 18:11:35 +00:00
|
|
|
const commander = require('commander');
|
|
|
|
const commands = require('./commands');
|
|
|
|
const init = require('./init/init');
|
2016-07-30 15:59:16 +00:00
|
|
|
const path = require('path');
|
|
|
|
const pkg = require('../package.json');
|
|
|
|
|
Merge `rnpm config` with the `cli config`, step 1
Summary:
This is step one on merging the `rnpm` config with the `cli config`. The plan is to remove two sources of truth (rnpm package.json config and rn-cli Javascript config)
Rationale:
As of now, we have `rnpm` config, that used to live in the `local-cli/core/config/index.js` and the `rn-cli` config, living in `default.config.js` and `utils/Config.js`.
This PR moves all the things into one file, called `local-cli/core', simplifies things, enhances types (Config now has better types, added missing properties and fixed descriptions).
One notable addition is that users can now opt-in to override the commands that are loaded at the package level. Previously it was only possible to add a command by writing a plugin. Now, you can just tweak the `rn-cli.config.js`.
This is one of the several improvements I have on my roadmap, with better documentation for the CLI as well.
Closes https://github.com/facebook/react-native/pull/11564
Differential Revision: D4360095
fbshipit-source-id: feaec7c88df63e51cef1f9620c7eedeb738d3d00
2017-01-09 15:49:29 +00:00
|
|
|
import type {CommandT} from './commands';
|
2017-05-03 13:38:40 +00:00
|
|
|
import type {RNConfig} from './core';
|
2016-06-03 16:05:14 +00:00
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
commander.version(pkg.version);
|
2016-05-21 13:53:32 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
const defaultOptParser = val => val;
|
2016-05-21 13:53:32 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
const handleError = err => {
|
2016-07-30 15:59:16 +00:00
|
|
|
console.error();
|
|
|
|
console.error(err.message || err);
|
|
|
|
console.error();
|
2018-06-26 01:11:33 +00:00
|
|
|
if (err.stack) {
|
|
|
|
console.error(err.stack);
|
|
|
|
console.error();
|
|
|
|
}
|
2016-07-30 15:59:16 +00:00
|
|
|
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-08-29 15:30:52 +00:00
|
|
|
const sourceInformation = this.pkg
|
2018-05-11 19:43:49 +00:00
|
|
|
? [` ${chalk.bold('Source:')} ${this.pkg.name}@${this.pkg.version}`, '']
|
2016-08-29 15:30:52 +00:00
|
|
|
: [];
|
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
let output = [
|
|
|
|
'',
|
2018-05-11 19:43:49 +00:00
|
|
|
chalk.bold(chalk.cyan(` react-native ${cmdName} ${this.usage()}`)),
|
2016-07-30 15:59:16 +00:00
|
|
|
` ${this._description}`,
|
|
|
|
'',
|
2016-08-29 15:30:52 +00:00
|
|
|
...sourceInformation,
|
2016-07-30 15:59:16 +00:00
|
|
|
` ${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) {
|
2018-05-11 19:43:49 +00:00
|
|
|
const formattedUsage = this.examples
|
|
|
|
.map(example => ` ${example.desc}: \n ${chalk.cyan(example.cmd)}`)
|
|
|
|
.join('\n\n');
|
2016-07-30 15:59:16 +00:00
|
|
|
|
|
|
|
output = output.concat([
|
|
|
|
chalk.bold(' Example usage:'),
|
|
|
|
'',
|
|
|
|
formattedUsage,
|
|
|
|
]);
|
|
|
|
}
|
2016-05-21 13:53:32 +00:00
|
|
|
|
2018-05-11 19:43:49 +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) {
|
2018-05-11 19:43:49 +00:00
|
|
|
console.log(
|
|
|
|
[
|
|
|
|
'',
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-05-03 13:38:40 +00:00
|
|
|
const addCommand = (command: CommandT, cfg: RNConfig) => {
|
2016-07-30 15:59:16 +00:00
|
|
|
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);
|
Merge `rnpm config` with the `cli config`, step 1
Summary:
This is step one on merging the `rnpm` config with the `cli config`. The plan is to remove two sources of truth (rnpm package.json config and rn-cli Javascript config)
Rationale:
As of now, we have `rnpm` config, that used to live in the `local-cli/core/config/index.js` and the `rn-cli` config, living in `default.config.js` and `utils/Config.js`.
This PR moves all the things into one file, called `local-cli/core', simplifies things, enhances types (Config now has better types, added missing properties and fixed descriptions).
One notable addition is that users can now opt-in to override the commands that are loaded at the package level. Previously it was only possible to add a command by writing a plugin. Now, you can just tweak the `rn-cli.config.js`.
This is one of the several improvements I have on my roadmap, with better documentation for the CLI as well.
Closes https://github.com/facebook/react-native/pull/11564
Differential Revision: D4360095
fbshipit-source-id: feaec7c88df63e51cef1f9620c7eedeb738d3d00
2017-01-09 15:49:29 +00:00
|
|
|
return command.func(argv, cfg, passedOptions);
|
2016-07-30 15:59:16 +00:00
|
|
|
})
|
|
|
|
.catch(handleError);
|
|
|
|
});
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
cmd.helpInformation = printHelpInformation.bind(cmd);
|
|
|
|
cmd.examples = command.examples;
|
|
|
|
cmd.pkg = command.pkg;
|
2016-07-30 15:59:16 +00:00
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
options.forEach(opt =>
|
|
|
|
cmd.option(
|
2016-07-30 15:59:16 +00:00
|
|
|
opt.command,
|
|
|
|
opt.description,
|
|
|
|
opt.parse || defaultOptParser,
|
Merge `rnpm config` with the `cli config`, step 1
Summary:
This is step one on merging the `rnpm` config with the `cli config`. The plan is to remove two sources of truth (rnpm package.json config and rn-cli Javascript config)
Rationale:
As of now, we have `rnpm` config, that used to live in the `local-cli/core/config/index.js` and the `rn-cli` config, living in `default.config.js` and `utils/Config.js`.
This PR moves all the things into one file, called `local-cli/core', simplifies things, enhances types (Config now has better types, added missing properties and fixed descriptions).
One notable addition is that users can now opt-in to override the commands that are loaded at the package level. Previously it was only possible to add a command by writing a plugin. Now, you can just tweak the `rn-cli.config.js`.
This is one of the several improvements I have on my roadmap, with better documentation for the CLI as well.
Closes https://github.com/facebook/react-native/pull/11564
Differential Revision: D4360095
fbshipit-source-id: feaec7c88df63e51cef1f9620c7eedeb738d3d00
2017-01-09 15:49:29 +00:00
|
|
|
typeof opt.default === 'function' ? opt.default(cfg) : opt.default,
|
2018-05-11 19:43:49 +00:00
|
|
|
),
|
|
|
|
);
|
2016-08-12 18:46:04 +00:00
|
|
|
|
|
|
|
// Placeholder option for --config, which is parsed before any other option,
|
|
|
|
// but needs to be here to avoid "unknown option" errors when specified
|
|
|
|
cmd.option('--config [string]', 'Path to the CLI configuration file');
|
2016-07-30 15:59:16 +00:00
|
|
|
};
|
2016-05-21 13:53:32 +00:00
|
|
|
|
2018-07-25 12:44:40 +00:00
|
|
|
async function run() {
|
|
|
|
const config = await configPromise;
|
2016-07-30 15:59:16 +00:00
|
|
|
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);
|
|
|
|
|
2018-05-11 19:43:49 +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,
|
|
|
|
};
|