Skip to content

Commit a52dcb1

Browse files
authored
Merge pull request #4 from wtto00/dev
fix: The default execution timeout is changed to 5 minutes. android.install adds options parameter.
2 parents c6e9b8e + e01f765 commit a52dcb1

File tree

5 files changed

+37
-28
lines changed

5 files changed

+37
-28
lines changed

README-ZH_CN.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ android
228228

229229
```js
230230
android
231-
.isInstalled('emulator-id', '/path/to/apk')
231+
.install('emulator-id', '/path/to/apk', { r: true })
232232
.then(() => {
233233
console.log('installed');
234234
})
@@ -237,10 +237,11 @@ android
237237
});
238238
```
239239

240-
| field | type | required | default | note |
241-
| ---------- | ------ | -------- | ------- | ---------------------- |
242-
| emulatorId | string | true | - | 模拟器设备 ID |
243-
| apkPath | string | true | - | apk 安装包所在路径位置 |
240+
| field | type | required | default | note |
241+
| ---------- | ------ | -------- | ------- | ---------------------------- |
242+
| emulatorId | string | true | - | 模拟器设备 ID |
243+
| apkPath | string | true | - | apk 安装包所在路径位置 |
244+
| options | object | false | - | "adb install"的参数:-lrtsdg |
244245

245246
### inputKeyEvent
246247

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ Install an APK located by absolute URI `apkPath` onto device with `emulatorId`.
228228

229229
```js
230230
android
231-
.isInstalled('emulator-id', '/path/to/apk')
231+
.install('emulator-id', '/path/to/apk', { r: true })
232232
.then(() => {
233233
console.log('installed');
234234
})
@@ -237,10 +237,11 @@ android
237237
});
238238
```
239239

240-
| field | type | required | default | note |
241-
| ---------- | ------ | -------- | ------- | ------------------------- |
242-
| emulatorId | string | true | - | ID of emulator device |
243-
| apkPath | string | true | - | Absolute path of apk file |
240+
| field | type | required | default | note |
241+
| ---------- | ------ | -------- | ------- | ----------------------------------------- |
242+
| emulatorId | string | true | - | ID of emulator device |
243+
| apkPath | string | true | - | Absolute path of apk file |
244+
| options | object | false | - | The parameters for "adb install": -lrtsdg |
244245

245246
### inputKeyEvent
246247

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@wtto00/android-tools",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Node module for managing and controlling the Android Devices.",
55
"type": "module",
66
"main": "dist/lib/index.cjs",

src/index.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
filterGroupImages,
44
getLocalArch,
55
isExecExpectedResult,
6+
params2Cmd,
67
processKeyValueGroups,
78
retry,
89
spawnExec,
@@ -184,19 +185,10 @@ class Android {
184185
*/
185186
async start(options: Required<Pick<EmulatorOptions, 'avd'>> & Omit<EmulatorOptions, 'avd'>) {
186187
log('start: %s', JSON.stringify(options));
187-
const opts = [];
188188
options.verbose = true;
189-
for (const key in options) {
190-
const val = options[key as keyof EmulatorOptions];
191-
const cliKey = '-' + key.replace(/[A-Z]/g, (matched) => `-${matched.toLocaleLowerCase()}`);
192-
if (val === true) {
193-
opts.push(cliKey);
194-
} else if (val) {
195-
opts.push(cliKey, val);
196-
}
197-
}
189+
const cmd = params2Cmd(options);
198190
const res = await spawnWaitFor(
199-
`${this.emulatorBin} ${opts.join(' ')}`,
191+
`${this.emulatorBin} ${cmd}`,
200192
/ control console listening on port (\d+), ADB on port \d+/
201193
);
202194
return {
@@ -211,7 +203,7 @@ class Android {
211203
*/
212204
async waitForDevice(emulatorId: string) {
213205
log('waitForDevice: %s', JSON.stringify({ emulatorId }));
214-
await this.adb(emulatorId, 'wait-for-device', 300000);
206+
await this.adb(emulatorId, 'wait-for-device');
215207
}
216208

217209
/**
@@ -299,9 +291,10 @@ class Android {
299291
* @param emulatorId id of emulator
300292
* @param apkPath path of apk file
301293
*/
302-
async install(emulatorId: string, apkPath: string) {
294+
async install(emulatorId: string, apkPath: string, options?: Record<'l' | 'r' | 't' | 's' | 'd' | 'g', boolean>) {
303295
log('install: %s, %s', JSON.stringify({ emulatorId, apkPath }));
304-
const process = await this.adb(emulatorId, `install ${apkPath}`);
296+
const cmdParams = params2Cmd(options);
297+
const process = await this.adb(emulatorId, `install ${cmdParams} ${apkPath}`);
305298
if (process.output.match(/Success/)) return;
306299
throw new Error('Could not parse output of adb command');
307300
}

src/util.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function transformCommand(command: string) {
1818
/**
1919
* Run a shell command.
2020
*/
21-
export function spawnExec(command: string, timeout = 60000) {
21+
export function spawnExec(command: string, timeout = 300000) {
2222
return new Promise<ChildProcessWithoutNullStreams & { output: string }>((resolve, reject) => {
2323
const { cmd, args } = transformCommand(command);
2424
const proc = spawn(cmd, args) as ChildProcessWithoutNullStreams & {
@@ -54,7 +54,7 @@ export function spawnExec(command: string, timeout = 60000) {
5454
/**
5555
* Execute a shell command synchronously.
5656
*/
57-
export function spwanSyncExec(command: string, timeout = 60000) {
57+
export function spwanSyncExec(command: string, timeout = 300000) {
5858
const { cmd, args } = transformCommand(command);
5959
const clock = setTimeout(() => {
6060
throw Error('Execution timeout');
@@ -67,7 +67,7 @@ export function spwanSyncExec(command: string, timeout = 60000) {
6767
/**
6868
* Execute a shell command until a matching output is found.
6969
*/
70-
export function spawnWaitFor(command: string, regex: RegExp, timeout = 120000) {
70+
export function spawnWaitFor(command: string, regex: RegExp, timeout = 600000) {
7171
return new Promise<{
7272
process: ChildProcessByStdio<null, internal.Readable, null>;
7373
matches: RegExpMatchArray;
@@ -193,3 +193,17 @@ export function getLocalArch() {
193193
if (process.arch === 'x64') return 'x86_64';
194194
return '';
195195
}
196+
197+
export function params2Cmd(options: Record<string, string | number | boolean> = {}) {
198+
const opts = [];
199+
for (const key in options) {
200+
const val = options[key];
201+
const cliKey = '-' + key.replace(/[A-Z]/g, (matched) => `-${matched.toLocaleLowerCase()}`);
202+
if (val === true) {
203+
opts.push(cliKey);
204+
} else if (val) {
205+
opts.push(cliKey, val);
206+
}
207+
}
208+
return opts.join(' ');
209+
}

0 commit comments

Comments
 (0)