mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-02-11 09:11:14 +08:00
Move run-android to private-cli
Reviewed By: mkonicek Differential Revision: D2544567 fb-gh-sync-id: 3c62f6c30b5be7f480d8f44a48fb551fc30d477e
This commit is contained in:
committed by
facebook-github-bot-4
parent
93488a7898
commit
02e39719c2
145
private-cli/src/runAndroid/runAndroid.js
Normal file
145
private-cli/src/runAndroid/runAndroid.js
Normal file
@@ -0,0 +1,145 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
const chalk = require('chalk');
|
||||
const child_process = require('child_process');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const parseCommandLine = require('../../../packager/parseCommandLine');
|
||||
const isPackagerRunning = require('../util/isPackagerRunning');
|
||||
const Promise = require('promise');
|
||||
|
||||
/**
|
||||
* Starts the app on the Android simulator.
|
||||
*/
|
||||
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,
|
||||
}], argv);
|
||||
|
||||
if (!checkAndroid()) {
|
||||
console.log(chalk.red('Android project not found. Maybe run react-native android first?'));
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(isPackagerRunning().then(result => {
|
||||
if (result === 'running') {
|
||||
console.log(chalk.bold('JS server already running.'));
|
||||
} else if (result === 'unrecognized') {
|
||||
console.warn(chalk.yellow('JS server not recognized, continuing with build...'));
|
||||
} else {
|
||||
// result == 'not_running'
|
||||
console.log(chalk.bold('Starting JS server...'));
|
||||
startServerInNewWindow();
|
||||
}
|
||||
buildAndRun(args);
|
||||
}));
|
||||
}
|
||||
|
||||
// Verifies this is an Android project
|
||||
function checkAndroid() {
|
||||
return fs.existsSync('android/gradlew');
|
||||
}
|
||||
|
||||
// Builds the app and runs it on a connected emulator / device.
|
||||
function buildAndRun(args, reject) {
|
||||
process.chdir('android');
|
||||
try {
|
||||
const cmd = process.platform.startsWith('win')
|
||||
? 'gradlew.bat'
|
||||
: './gradlew';
|
||||
|
||||
const gradleArgs = ['installDebug'];
|
||||
if (args['install-debug']) {
|
||||
gradleArgs.push(args['install-debug']);
|
||||
}
|
||||
|
||||
console.log(chalk.bold(
|
||||
'Building and installing the app on the device (cd android && ' + cmd +
|
||||
' ' + gradleArgs.join(' ') + ')...'
|
||||
));
|
||||
|
||||
child_process.execFileSync(cmd, gradleArgs, {
|
||||
stdio: [process.stdin, process.stdout, process.stderr],
|
||||
});
|
||||
} catch (e) {
|
||||
console.log(chalk.red(
|
||||
'Could not install the app on the device, see the error above.'
|
||||
));
|
||||
// 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;
|
||||
}
|
||||
|
||||
try {
|
||||
const packageName = fs.readFileSync(
|
||||
'app/src/main/AndroidManifest.xml',
|
||||
'utf8'
|
||||
).match(/package="(.+?)"/)[1];
|
||||
|
||||
const adbPath = process.env.ANDROID_HOME
|
||||
? process.env.ANDROID_HOME + '/platform-tools/adb'
|
||||
: 'adb';
|
||||
|
||||
const adbArgs = [
|
||||
'shell', 'am', 'start', '-n', packageName + '/.MainActivity'
|
||||
];
|
||||
|
||||
console.log(chalk.bold(
|
||||
'Starting the app (' + adbPath + ' ' + adbArgs.join(' ') + ')...'
|
||||
));
|
||||
|
||||
child_process.spawnSync(adbPath, adbArgs, {stdio: 'inherit'});
|
||||
} catch (e) {
|
||||
console.log(chalk.red(
|
||||
'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;
|
||||
}
|
||||
}
|
||||
|
||||
function startServerInNewWindow() {
|
||||
const launchPackagerScript = path.resolve(
|
||||
__dirname, '..', '..', '..', 'packager', 'launchPackager.command'
|
||||
);
|
||||
|
||||
if (process.platform === 'darwin') {
|
||||
child_process.spawnSync('open', [launchPackagerScript]);
|
||||
} else if (process.platform === 'linux') {
|
||||
child_process.spawn(
|
||||
'xterm',
|
||||
['-e', 'sh', launchPackagerScript],
|
||||
{detached: true}
|
||||
);
|
||||
} else {
|
||||
console.error(chalk.yellow(
|
||||
'Starting packager in new window is not supported on Windows yet. ' +
|
||||
'See https://github.com/facebook/react-native/issues/3469 on how to ' +
|
||||
'start it manually.'
|
||||
));
|
||||
throw new Error('Windows is not yet supported');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = runAndroid;
|
||||
31
private-cli/src/util/isPackagerRunning.js
Normal file
31
private-cli/src/util/isPackagerRunning.js
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
const fetch = require('node-fetch');
|
||||
|
||||
/**
|
||||
* Indicates whether or not the packager is running. It ruturns a promise that
|
||||
* when fulfilled can returns one out of these possible values:
|
||||
* - `running`: the packager is running
|
||||
* - `not_running`: the packager nor any process is running on the expected
|
||||
* port.
|
||||
* - `unrecognized`: one other process is running on the port ew expect the
|
||||
* packager to be running.
|
||||
*/
|
||||
function isPackagerRunning() {
|
||||
return fetch('http://localhost:8081/status').then(
|
||||
res => res.text().then(body =>
|
||||
body === 'packager-status:running' ? 'running' : 'unrecognized'
|
||||
),
|
||||
() => 'not_running'
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = isPackagerRunning;
|
||||
Reference in New Issue
Block a user