Skip to content

Commit 02e3971

Browse files
martinbigiofacebook-github-bot-4
authored andcommitted
Move run-android to private-cli
Reviewed By: mkonicek Differential Revision: D2544567 fb-gh-sync-id: 3c62f6c30b5be7f480d8f44a48fb551fc30d477e
1 parent 93488a7 commit 02e3971

3 files changed

Lines changed: 184 additions & 4 deletions

File tree

local-cli/cli.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ var fs = require('fs');
1414
var generate = require('../private-cli/src/generate/generate');
1515
var init = require('./init.js');
1616
var library = require('../private-cli/src/library/library');
17-
var runAndroid = require('./run-android.js');
18-
var runPackager = require('./run-packager.js');
17+
var runAndroid = require('../private-cli/src/runAndroid/runAndroid');
1918
var server = require('../private-cli/src/server/server');
2019

2120
// TODO: remove once we fully roll out the `private-cli` based cli
2221
// var bundle_DEPRECATED = require('./bundle.js');
2322
// var generateAndroid_DEPRECATED = require('./generate-android.js');
2423
// var newLibrary_DEPRECATED = require('./new-library.js');
24+
// var runPackager_DEPRECATED = require('./run-packager.js');
25+
// var runAndroid_DEPRECATED = require('./run-android.js');
2526

2627
function printUsage() {
2728
console.log([
@@ -52,9 +53,11 @@ function run() {
5253
}
5354

5455
var config = Config.get(__dirname);
56+
5557
switch (args[0]) {
5658
case 'start':
57-
runPackager(false);
59+
server(args, config).done();
60+
// runPackager_DEPRECATED();
5861
break;
5962
case 'bundle':
6063
bundle(args, config).done();
@@ -84,7 +87,8 @@ function run() {
8487
// );
8588
break;
8689
case 'run-android':
87-
runAndroid();
90+
runAndroid(args, config).done();
91+
// runAndroid_DEPRECATED(); default:
8892
break;
8993
case 'help':
9094
printUsage();
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 fetch = require('node-fetch');
12+
13+
/**
14+
* Indicates whether or not the packager is running. It ruturns a promise that
15+
* when fulfilled can returns one out of these possible values:
16+
* - `running`: the packager is running
17+
* - `not_running`: the packager nor any process is running on the expected
18+
* port.
19+
* - `unrecognized`: one other process is running on the port ew expect the
20+
* packager to be running.
21+
*/
22+
function isPackagerRunning() {
23+
return fetch('http://localhost:8081/status').then(
24+
res => res.text().then(body =>
25+
body === 'packager-status:running' ? 'running' : 'unrecognized'
26+
),
27+
() => 'not_running'
28+
);
29+
}
30+
31+
module.exports = isPackagerRunning;

0 commit comments

Comments
 (0)