2015-10-20 18:46:37 +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.
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const chalk = require('chalk');
|
|
|
|
const child_process = require('child_process');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2015-10-26 19:59:54 +00:00
|
|
|
const parseCommandLine = require('../util/parseCommandLine');
|
2015-10-20 18:46:37 +00:00
|
|
|
const isPackagerRunning = require('../util/isPackagerRunning');
|
|
|
|
const Promise = require('promise');
|
2016-03-08 20:07:59 +00:00
|
|
|
const adb = require('./adb');
|
2015-10-20 18:46:37 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-21 16:37:44 +00:00
|
|
|
* Starts the app on a connected Android emulator or device.
|
2015-10-20 18:46:37 +00:00
|
|
|
*/
|
|
|
|
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,
|
2016-02-22 00:52:58 +00:00
|
|
|
}, {
|
|
|
|
command: 'root',
|
|
|
|
type: 'string',
|
|
|
|
description: 'Override the root directory for the android build (which contains the android directory)',
|
2016-03-05 04:26:31 +00:00
|
|
|
}, {
|
|
|
|
command: 'flavor',
|
|
|
|
type: 'string',
|
|
|
|
required: false,
|
2016-04-29 12:17:56 +00:00
|
|
|
}, {
|
|
|
|
command: 'variant',
|
|
|
|
type: 'string',
|
|
|
|
required: false,
|
2015-10-20 18:46:37 +00:00
|
|
|
}], argv);
|
|
|
|
|
2016-02-22 00:52:58 +00:00
|
|
|
args.root = args.root || '';
|
|
|
|
|
|
|
|
if (!checkAndroid(args)) {
|
2015-10-20 18:46:37 +00:00
|
|
|
console.log(chalk.red('Android project not found. Maybe run react-native android first?'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(isPackagerRunning().then(result => {
|
|
|
|
if (result === 'running') {
|
2016-03-08 20:07:59 +00:00
|
|
|
console.log(chalk.bold(`JS server already running.`));
|
2015-10-20 18:46:37 +00:00
|
|
|
} else if (result === 'unrecognized') {
|
2016-03-08 20:07:59 +00:00
|
|
|
console.warn(chalk.yellow(`JS server not recognized, continuing with build...`));
|
2015-10-20 18:46:37 +00:00
|
|
|
} else {
|
|
|
|
// result == 'not_running'
|
2016-03-08 20:07:59 +00:00
|
|
|
console.log(chalk.bold(`Starting JS server...`));
|
2015-10-20 18:46:37 +00:00
|
|
|
startServerInNewWindow();
|
|
|
|
}
|
2015-10-21 16:37:44 +00:00
|
|
|
buildAndRun(args, reject);
|
2015-10-20 18:46:37 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verifies this is an Android project
|
2016-02-22 00:52:58 +00:00
|
|
|
function checkAndroid(args) {
|
|
|
|
return fs.existsSync(path.join(args.root, 'android/gradlew'));
|
2015-10-20 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Builds the app and runs it on a connected emulator / device.
|
|
|
|
function buildAndRun(args, reject) {
|
2016-02-22 00:52:58 +00:00
|
|
|
process.chdir(path.join(args.root, 'android'));
|
2015-10-20 18:46:37 +00:00
|
|
|
try {
|
|
|
|
const cmd = process.platform.startsWith('win')
|
|
|
|
? 'gradlew.bat'
|
|
|
|
: './gradlew';
|
|
|
|
|
2016-03-05 04:26:31 +00:00
|
|
|
const gradleArgs = [];
|
2016-04-29 12:17:56 +00:00
|
|
|
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`
|
|
|
|
));
|
2016-03-05 04:26:31 +00:00
|
|
|
gradleArgs.push('install' +
|
|
|
|
args['flavor'][0].toUpperCase() + args['flavor'].slice(1)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
gradleArgs.push('installDebug');
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:46:37 +00:00
|
|
|
if (args['install-debug']) {
|
|
|
|
gradleArgs.push(args['install-debug']);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(chalk.bold(
|
2016-03-08 20:07:59 +00:00
|
|
|
`Building and installing the app on the device (cd android && ${cmd} ${gradleArgs.join(' ')}...`
|
2015-10-20 18:46:37 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
child_process.execFileSync(cmd, gradleArgs, {
|
|
|
|
stdio: [process.stdin, process.stdout, process.stderr],
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.log(chalk.red(
|
2015-11-03 11:46:08 +00:00
|
|
|
'Could not install the app on the device, read the error above for details.\n' +
|
|
|
|
'Make sure you have an Android emulator running or a device connected and have\n' +
|
|
|
|
'set up your Android development environment:\n' +
|
|
|
|
'https://facebook.github.io/react-native/docs/android-setup.html'
|
2015-10-20 18:46:37 +00:00
|
|
|
));
|
|
|
|
// 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';
|
|
|
|
|
2016-03-08 20:07:59 +00:00
|
|
|
const devices = adb.getDevices();
|
2016-04-06 16:20:39 +00:00
|
|
|
|
2016-03-08 20:07:59 +00:00
|
|
|
if (devices && devices.length > 0) {
|
|
|
|
devices.forEach((device) => {
|
2015-10-20 18:46:37 +00:00
|
|
|
|
2016-03-08 20:07:59 +00:00
|
|
|
const adbArgs = ['-s', device, 'shell', 'am', 'start', '-n', packageName + '/.MainActivity'];
|
|
|
|
|
|
|
|
console.log(chalk.bold(
|
|
|
|
`Starting the app on ${device} (${adbPath} ${adbArgs.join(' ')})...`
|
|
|
|
));
|
|
|
|
|
2016-04-06 16:20:39 +00:00
|
|
|
child_process.spawnSync(adbPath, adbArgs, {stdio: 'inherit'});
|
|
|
|
});
|
2016-03-08 20:07:59 +00:00
|
|
|
} else {
|
|
|
|
// If we cannot execute based on adb devices output, fall back to
|
|
|
|
// shell am start
|
|
|
|
const fallbackAdbArgs = [
|
|
|
|
'shell', 'am', 'start', '-n', packageName + '/.MainActivity'
|
|
|
|
];
|
|
|
|
console.log(chalk.bold(
|
|
|
|
`Starting the app (${adbPath} ${fallbackAdbArgs.join(' ')}...`
|
|
|
|
));
|
|
|
|
child_process.spawnSync(adbPath, fallbackAdbArgs, {stdio: 'inherit'});
|
|
|
|
}
|
2015-10-20 18:46:37 +00:00
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
console.log(chalk.red(
|
2016-03-08 20:07:59 +00:00
|
|
|
`adb invocation failed. Do you have adb in your PATH?`
|
2015-10-20 18:46:37 +00:00
|
|
|
));
|
|
|
|
// 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() {
|
2016-05-04 09:44:42 +00:00
|
|
|
const yargV = require('yargs').argv;
|
2016-04-29 11:13:30 +00:00
|
|
|
const scriptFile = /^win/.test(process.platform) ?
|
|
|
|
'launchPackager.bat' :
|
|
|
|
'launchPackager.command';
|
2016-05-04 09:44:42 +00:00
|
|
|
const packagerDir = path.resolve(__dirname, '..', '..', 'packager');
|
|
|
|
const launchPackagerScript = path.resolve(packagerDir, scriptFile);
|
|
|
|
const procConfig = {cwd: packagerDir};
|
2015-10-20 18:46:37 +00:00
|
|
|
|
|
|
|
if (process.platform === 'darwin') {
|
2016-03-05 05:54:47 +00:00
|
|
|
if (yargV.open) {
|
2016-05-04 09:44:42 +00:00
|
|
|
return child_process.spawnSync('open', ['-a', yargV.open, launchPackagerScript], procConfig);
|
2016-03-05 05:54:47 +00:00
|
|
|
}
|
2016-05-04 09:44:42 +00:00
|
|
|
return child_process.spawnSync('open', [launchPackagerScript], procConfig);
|
2016-03-05 05:54:47 +00:00
|
|
|
|
2015-10-20 18:46:37 +00:00
|
|
|
} else if (process.platform === 'linux') {
|
2016-05-04 09:44:42 +00:00
|
|
|
procConfig.detached = true;
|
2016-03-05 05:54:47 +00:00
|
|
|
if (yargV.open){
|
2016-05-04 09:44:42 +00:00
|
|
|
return child_process.spawn(yargV.open,['-e', 'sh', launchPackagerScript], procConfig);
|
2016-03-05 05:54:47 +00:00
|
|
|
}
|
2016-05-04 09:44:42 +00:00
|
|
|
return child_process.spawn('sh', [launchPackagerScript], procConfig);
|
2016-03-05 05:54:47 +00:00
|
|
|
|
2015-10-22 14:08:51 +00:00
|
|
|
} else if (/^win/.test(process.platform)) {
|
2016-05-04 09:44:42 +00:00
|
|
|
procConfig.detached = true;
|
|
|
|
procConfig.stdio = 'ignore';
|
|
|
|
return child_process.spawn('cmd.exe', ['/C', 'start', launchPackagerScript], procConfig);
|
2015-10-20 18:46:37 +00:00
|
|
|
} else {
|
2016-03-08 20:07:59 +00:00
|
|
|
console.log(chalk.red(`Cannot start the packager. Unknown platform ${process.platform}`));
|
2015-10-20 18:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = runAndroid;
|