mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-06-18 09:23:16 +08:00
Merge rnpm cli into react-native
Summary: This is an initial step of rewriting the CLI interface to use `rnpm` one (commander, plugins etc.). It's scope is to move all existing commands to use rnpm CLI interface, so that we get plugins, flags and our existing ecosystem working out of the box. <s>This is still WIP and some of the commands are left commented out.</s> For the `config` of `rnpm` (functions get info about project and dependency), <s>I am thinking we can merge them with</s> we decided to merge it with [`default.config.js`](e57683e420/local-cli/default.config.js (L33)), so they are available on the `new Config()` [instance](e57683e420/local-cli/cliEntry.js (L59)) (which means we don't have to change anything and current plugins, like runIOS and runAndroid can just start using it [w/o depending on any extra argument](https://github.com/grabbou/react-native/blob/e57683e420210749a5a6b802b4e Closes https://github.com/facebook/react-native/pull/7899 Differential Revision: D3613193 Pulled By: bestander fbshipit-source-id: 09a072f3b21e5239dfcd8da88a205bd28dc5d037
This commit is contained in:
@@ -12,63 +12,36 @@ const chalk = require('chalk');
|
||||
const child_process = require('child_process');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const parseCommandLine = require('../util/parseCommandLine');
|
||||
const isPackagerRunning = require('../util/isPackagerRunning');
|
||||
const Promise = require('promise');
|
||||
const adb = require('./adb');
|
||||
|
||||
// Verifies this is an Android project
|
||||
function checkAndroid(root) {
|
||||
return fs.existsSync(path.join(root, 'android/gradlew'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the app on a connected Android emulator or device.
|
||||
*/
|
||||
function runAndroid(argv, config) {
|
||||
return new Promise((resolve, reject) => {
|
||||
_runAndroid(argv, config, resolve, reject);
|
||||
});
|
||||
}
|
||||
|
||||
function _runAndroid(argv, config, resolve, reject) {
|
||||
const args = parseCommandLine([{
|
||||
command: 'install-debug',
|
||||
type: 'string',
|
||||
required: false,
|
||||
}, {
|
||||
command: 'root',
|
||||
type: 'string',
|
||||
description: 'Override the root directory for the android build (which contains the android directory)',
|
||||
}, {
|
||||
command: 'flavor',
|
||||
type: 'string',
|
||||
required: false,
|
||||
}, {
|
||||
command: 'variant',
|
||||
type: 'string',
|
||||
required: false,
|
||||
}], argv);
|
||||
|
||||
args.root = args.root || '';
|
||||
|
||||
if (!checkAndroid(args)) {
|
||||
function runAndroid(argv, config, args) {
|
||||
if (!checkAndroid(args.root)) {
|
||||
console.log(chalk.red('Android project not found. Maybe run react-native android first?'));
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(isPackagerRunning().then(result => {
|
||||
return isPackagerRunning().then(result => {
|
||||
if (result === 'running') {
|
||||
console.log(chalk.bold(`JS server already running.`));
|
||||
console.log(chalk.bold('JS server already running.'));
|
||||
} else if (result === 'unrecognized') {
|
||||
console.warn(chalk.yellow(`JS server not recognized, continuing with build...`));
|
||||
console.warn(chalk.yellow('JS server not recognized, continuing with build...'));
|
||||
} else {
|
||||
// result == 'not_running'
|
||||
console.log(chalk.bold(`Starting JS server...`));
|
||||
console.log(chalk.bold('Starting JS server...'));
|
||||
startServerInNewWindow();
|
||||
}
|
||||
run(args, reject);
|
||||
}));
|
||||
}
|
||||
|
||||
// Verifies this is an Android project
|
||||
function checkAndroid(args) {
|
||||
return fs.existsSync(path.join(args.root, 'android/gradlew'));
|
||||
return buildAndRun(args);
|
||||
});
|
||||
}
|
||||
|
||||
function getAdbPath() {
|
||||
@@ -98,7 +71,7 @@ function tryRunAdbReverse() {
|
||||
}
|
||||
|
||||
// Builds the app and runs it on a connected emulator / device.
|
||||
function run(args, reject) {
|
||||
function buildAndRun(args) {
|
||||
process.chdir(path.join(args.root, 'android'));
|
||||
try {
|
||||
tryRunAdbReverse();
|
||||
@@ -108,23 +81,23 @@ function run(args, reject) {
|
||||
: './gradlew';
|
||||
|
||||
const gradleArgs = [];
|
||||
if (args['variant']) {
|
||||
gradleArgs.push('install' +
|
||||
args['variant'][0].toUpperCase() + args['variant'].slice(1)
|
||||
);
|
||||
} else if (args['flavor']) {
|
||||
console.warn(chalk.yellow(
|
||||
`--flavor has been deprecated. Use --variant instead`
|
||||
));
|
||||
gradleArgs.push('install' +
|
||||
args['flavor'][0].toUpperCase() + args['flavor'].slice(1)
|
||||
);
|
||||
if (args.variant) {
|
||||
gradleArgs.push('install' +
|
||||
args.variant[0].toUpperCase() + args.variant.slice(1)
|
||||
);
|
||||
} else if (args.flavor) {
|
||||
console.warn(chalk.yellow(
|
||||
'--flavor has been deprecated. Use --variant instead'
|
||||
));
|
||||
gradleArgs.push('install' +
|
||||
args.flavor[0].toUpperCase() + args.flavor.slice(1)
|
||||
);
|
||||
} else {
|
||||
gradleArgs.push('installDebug');
|
||||
gradleArgs.push('installDebug');
|
||||
}
|
||||
|
||||
if (args['install-debug']) {
|
||||
gradleArgs.push(args['install-debug']);
|
||||
if (args.installDebug) {
|
||||
gradleArgs.push(args.installDebug);
|
||||
}
|
||||
|
||||
console.log(chalk.bold(
|
||||
@@ -144,8 +117,7 @@ function run(args, reject) {
|
||||
// stderr is automatically piped from the gradle process, so the user
|
||||
// should see the error already, there is no need to do
|
||||
// `console.log(e.stderr)`
|
||||
reject();
|
||||
return;
|
||||
return Promise.reject();
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -183,13 +155,12 @@ function run(args, reject) {
|
||||
|
||||
} catch (e) {
|
||||
console.log(chalk.red(
|
||||
`adb invocation failed. Do you have adb in your PATH?`
|
||||
'adb invocation failed. Do you have adb in your PATH?'
|
||||
));
|
||||
// stderr is automatically piped from the gradle process, so the user
|
||||
// should see the error already, there is no need to do
|
||||
// `console.log(e.stderr)`
|
||||
reject();
|
||||
return;
|
||||
return Promise.reject();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,4 +195,20 @@ function startServerInNewWindow() {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = runAndroid;
|
||||
module.exports = {
|
||||
name: 'run-android',
|
||||
description: 'builds your app and starts it on a connected Android emulator or device',
|
||||
func: runAndroid,
|
||||
options: [{
|
||||
command: '--install-debug',
|
||||
}, {
|
||||
command: '--root [string]',
|
||||
description: 'Override the root directory for the android build (which contains the android directory)',
|
||||
default: '',
|
||||
}, {
|
||||
command: '--flavor [string]',
|
||||
description: '--flavor has been deprecated. Use --variant instead',
|
||||
}, {
|
||||
command: '--variant [string]',
|
||||
}],
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user