-
Notifications
You must be signed in to change notification settings - Fork 950
Expand file tree
/
Copy pathtryLaunchAppOnDevice.ts
More file actions
66 lines (57 loc) · 1.78 KB
/
Copy pathtryLaunchAppOnDevice.ts
File metadata and controls
66 lines (57 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import execa from 'execa';
import {AndroidProject, Flags} from '.';
import {logger, CLIError} from '@react-native-community/cli-tools';
function tryLaunchAppOnDevice(
device: string | void,
androidProject: AndroidProject,
adbPath: string,
args: Flags,
) {
const {appId, appIdSuffix} = args;
const {packageName, mainActivity, applicationId} = androidProject;
const applicationIdWithSuffix = [appId || applicationId, appIdSuffix]
.filter(Boolean)
.join('.');
const activityToLaunch =
mainActivity.startsWith(packageName) ||
(!mainActivity.startsWith('.') && mainActivity.includes('.'))
? mainActivity
: mainActivity.startsWith('.')
? [packageName, mainActivity].join('')
: [packageName, mainActivity].filter(Boolean).join('.');
try {
// Here we're using the same flags as Android Studio to launch the app
const adbArgs = [
'shell',
'am',
'start',
'-n',
`${applicationIdWithSuffix}/${activityToLaunch}`,
'-a',
'android.intent.action.MAIN',
'-c',
'android.intent.category.LAUNCHER',
];
if (args.user !== undefined) {
adbArgs.push('--user', `${args.user}`);
}
if (device) {
adbArgs.unshift('-s', device);
logger.info(`Starting the app on "${device}"...`);
} else {
logger.info('Starting the app...');
}
logger.debug(`Running command "${adbPath} ${adbArgs.join(' ')}"`);
execa.sync(adbPath, adbArgs, {stdio: 'inherit'});
} catch (error) {
throw new CLIError('Failed to start the app.', error as any);
}
}
export default tryLaunchAppOnDevice;