forked from react-native-community/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtryInstallAppOnDevice.ts
More file actions
90 lines (78 loc) · 2.75 KB
/
tryInstallAppOnDevice.ts
File metadata and controls
90 lines (78 loc) · 2.75 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import execa from 'execa';
import fs from 'fs';
import {logger, CLIError} from '@react-native-community/cli-tools';
import adb from './adb';
import type {AndroidProject, Flags} from './';
function tryInstallAppOnDevice(
args: Flags,
adbPath: string,
device: string,
androidProject: AndroidProject,
selectedTask?: string,
) {
try {
// "app" is usually the default value for Android apps with only 1 app
const {appName, sourceDir} = androidProject;
const defaultVariant = 'debug';
// handle if selected task from interactive mode, or mode from arguments, includes build flavour as well, eg. installProductionDebug should create ['production','debug'] array
const variantFromSelectedTask = (selectedTask ?? args.mode)
?.replace('install', '')
.split(/(?=[A-Z])/);
// create path to output file, eg. `production/debug`
const variantPath =
variantFromSelectedTask?.join('/')?.toLowerCase() ?? defaultVariant;
// create output file name, eg. `production-debug`
const variantAppName =
variantFromSelectedTask?.join('-')?.toLowerCase() ?? defaultVariant;
let pathToApk;
if (!args.binaryPath) {
const buildDirectory = `${sourceDir}/${appName}/build/outputs/apk/${variantPath}`;
const apkFile = getInstallApkName(
appName,
adbPath,
variantAppName,
device,
buildDirectory,
);
pathToApk = `${buildDirectory}/${apkFile}`;
} else {
pathToApk = args.binaryPath;
}
const installArgs = ['-s', device, 'install', '-r', '-d'];
if (args.user !== undefined) {
installArgs.push('--user', `${args.user}`);
}
const adbArgs = [...installArgs, pathToApk];
logger.info(`Installing the app on the device "${device}"...`);
logger.debug(`Running command "cd android && adb ${adbArgs.join(' ')}"`);
execa.sync(adbPath, adbArgs, {stdio: 'inherit'});
} catch (error) {
throw new CLIError(
'Failed to install the app on the device.',
error as any,
);
}
}
function getInstallApkName(
appName: string,
adbPath: string,
variant: string,
device: string,
buildDirectory: string,
) {
const availableCPUs = adb.getAvailableCPUs(adbPath, device);
// check if there is an apk file like app-armeabi-v7a-debug.apk
for (const availableCPU of availableCPUs.concat('universal')) {
const apkName = `${appName}-${availableCPU}-${variant}.apk`;
if (fs.existsSync(`${buildDirectory}/${apkName}`)) {
return apkName;
}
}
// check if there is a default file like app-debug.apk
const apkName = `${appName}-${variant}.apk`;
if (fs.existsSync(`${buildDirectory}/${apkName}`)) {
return apkName;
}
throw new Error('Could not find the correct install APK file.');
}
export default tryInstallAppOnDevice;