|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +const chalk = require('chalk'); |
| 12 | +const child_process = require('child_process'); |
| 13 | +const fs = require('fs'); |
| 14 | +const path = require('path'); |
| 15 | +const parseCommandLine = require('../../../packager/parseCommandLine'); |
| 16 | +const isPackagerRunning = require('../util/isPackagerRunning'); |
| 17 | +const Promise = require('promise'); |
| 18 | + |
| 19 | +/** |
| 20 | + * Starts the app on the Android simulator. |
| 21 | + */ |
| 22 | +function runAndroid(argv, config) { |
| 23 | + return new Promise((resolve, reject) => { |
| 24 | + _runAndroid(argv, config, resolve, reject); |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +function _runAndroid(argv, config, resolve, reject) { |
| 29 | + const args = parseCommandLine([{ |
| 30 | + command: 'install-debug', |
| 31 | + type: 'string', |
| 32 | + required: false, |
| 33 | + }], argv); |
| 34 | + |
| 35 | + if (!checkAndroid()) { |
| 36 | + console.log(chalk.red('Android project not found. Maybe run react-native android first?')); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + resolve(isPackagerRunning().then(result => { |
| 41 | + if (result === 'running') { |
| 42 | + console.log(chalk.bold('JS server already running.')); |
| 43 | + } else if (result === 'unrecognized') { |
| 44 | + console.warn(chalk.yellow('JS server not recognized, continuing with build...')); |
| 45 | + } else { |
| 46 | + // result == 'not_running' |
| 47 | + console.log(chalk.bold('Starting JS server...')); |
| 48 | + startServerInNewWindow(); |
| 49 | + } |
| 50 | + buildAndRun(args); |
| 51 | + })); |
| 52 | +} |
| 53 | + |
| 54 | +// Verifies this is an Android project |
| 55 | +function checkAndroid() { |
| 56 | + return fs.existsSync('android/gradlew'); |
| 57 | +} |
| 58 | + |
| 59 | +// Builds the app and runs it on a connected emulator / device. |
| 60 | +function buildAndRun(args, reject) { |
| 61 | + process.chdir('android'); |
| 62 | + try { |
| 63 | + const cmd = process.platform.startsWith('win') |
| 64 | + ? 'gradlew.bat' |
| 65 | + : './gradlew'; |
| 66 | + |
| 67 | + const gradleArgs = ['installDebug']; |
| 68 | + if (args['install-debug']) { |
| 69 | + gradleArgs.push(args['install-debug']); |
| 70 | + } |
| 71 | + |
| 72 | + console.log(chalk.bold( |
| 73 | + 'Building and installing the app on the device (cd android && ' + cmd + |
| 74 | + ' ' + gradleArgs.join(' ') + ')...' |
| 75 | + )); |
| 76 | + |
| 77 | + child_process.execFileSync(cmd, gradleArgs, { |
| 78 | + stdio: [process.stdin, process.stdout, process.stderr], |
| 79 | + }); |
| 80 | + } catch (e) { |
| 81 | + console.log(chalk.red( |
| 82 | + 'Could not install the app on the device, see the error above.' |
| 83 | + )); |
| 84 | + // stderr is automatically piped from the gradle process, so the user |
| 85 | + // should see the error already, there is no need to do |
| 86 | + // `console.log(e.stderr)` |
| 87 | + reject(); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + try { |
| 92 | + const packageName = fs.readFileSync( |
| 93 | + 'app/src/main/AndroidManifest.xml', |
| 94 | + 'utf8' |
| 95 | + ).match(/package="(.+?)"/)[1]; |
| 96 | + |
| 97 | + const adbPath = process.env.ANDROID_HOME |
| 98 | + ? process.env.ANDROID_HOME + '/platform-tools/adb' |
| 99 | + : 'adb'; |
| 100 | + |
| 101 | + const adbArgs = [ |
| 102 | + 'shell', 'am', 'start', '-n', packageName + '/.MainActivity' |
| 103 | + ]; |
| 104 | + |
| 105 | + console.log(chalk.bold( |
| 106 | + 'Starting the app (' + adbPath + ' ' + adbArgs.join(' ') + ')...' |
| 107 | + )); |
| 108 | + |
| 109 | + child_process.spawnSync(adbPath, adbArgs, {stdio: 'inherit'}); |
| 110 | + } catch (e) { |
| 111 | + console.log(chalk.red( |
| 112 | + 'adb invocation failed. Do you have adb in your PATH?' |
| 113 | + )); |
| 114 | + // stderr is automatically piped from the gradle process, so the user |
| 115 | + // should see the error already, there is no need to do |
| 116 | + // `console.log(e.stderr)` |
| 117 | + reject(); |
| 118 | + return; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +function startServerInNewWindow() { |
| 123 | + const launchPackagerScript = path.resolve( |
| 124 | + __dirname, '..', '..', '..', 'packager', 'launchPackager.command' |
| 125 | + ); |
| 126 | + |
| 127 | + if (process.platform === 'darwin') { |
| 128 | + child_process.spawnSync('open', [launchPackagerScript]); |
| 129 | + } else if (process.platform === 'linux') { |
| 130 | + child_process.spawn( |
| 131 | + 'xterm', |
| 132 | + ['-e', 'sh', launchPackagerScript], |
| 133 | + {detached: true} |
| 134 | + ); |
| 135 | + } else { |
| 136 | + console.error(chalk.yellow( |
| 137 | + 'Starting packager in new window is not supported on Windows yet. ' + |
| 138 | + 'See https://github.com/facebook/react-native/issues/3469 on how to ' + |
| 139 | + 'start it manually.' |
| 140 | + )); |
| 141 | + throw new Error('Windows is not yet supported'); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +module.exports = runAndroid; |
0 commit comments