From b4ae5e8d310ffdf52caebc3ad562117ea768d97a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AE=80=E9=9D=99=E5=87=A1?= <30424139+wtto00@users.noreply.github.com> Date: Sun, 1 Oct 2023 22:27:10 +0800 Subject: [PATCH] fix: The default execution timeout is changed to 5 minutes. android.install adds options parameter. --- README-ZH_CN.md | 11 ++++++----- README.md | 11 ++++++----- src/index.ts | 21 +++++++-------------- src/util.ts | 20 +++++++++++++++++--- 4 files changed, 36 insertions(+), 27 deletions(-) diff --git a/README-ZH_CN.md b/README-ZH_CN.md index 630bcbe..8f453de 100644 --- a/README-ZH_CN.md +++ b/README-ZH_CN.md @@ -228,7 +228,7 @@ android ```js android - .isInstalled('emulator-id', '/path/to/apk') + .install('emulator-id', '/path/to/apk', { r: true }) .then(() => { console.log('installed'); }) @@ -237,10 +237,11 @@ android }); ``` -| field | type | required | default | note | -| ---------- | ------ | -------- | ------- | ---------------------- | -| emulatorId | string | true | - | 模拟器设备 ID | -| apkPath | string | true | - | apk 安装包所在路径位置 | +| field | type | required | default | note | +| ---------- | ------ | -------- | ------- | ---------------------------- | +| emulatorId | string | true | - | 模拟器设备 ID | +| apkPath | string | true | - | apk 安装包所在路径位置 | +| options | object | false | - | "adb install"的参数:-lrtsdg | ### inputKeyEvent diff --git a/README.md b/README.md index aacf92c..205bb14 100644 --- a/README.md +++ b/README.md @@ -228,7 +228,7 @@ Install an APK located by absolute URI `apkPath` onto device with `emulatorId`. ```js android - .isInstalled('emulator-id', '/path/to/apk') + .install('emulator-id', '/path/to/apk', { r: true }) .then(() => { console.log('installed'); }) @@ -237,10 +237,11 @@ android }); ``` -| field | type | required | default | note | -| ---------- | ------ | -------- | ------- | ------------------------- | -| emulatorId | string | true | - | ID of emulator device | -| apkPath | string | true | - | Absolute path of apk file | +| field | type | required | default | note | +| ---------- | ------ | -------- | ------- | ----------------------------------------- | +| emulatorId | string | true | - | ID of emulator device | +| apkPath | string | true | - | Absolute path of apk file | +| options | object | false | - | The parameters for "adb install": -lrtsdg | ### inputKeyEvent diff --git a/src/index.ts b/src/index.ts index 6f7d052..a80e3b1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,6 +3,7 @@ import { filterGroupImages, getLocalArch, isExecExpectedResult, + params2Cmd, processKeyValueGroups, retry, spawnExec, @@ -184,19 +185,10 @@ class Android { */ async start(options: Required> & Omit) { log('start: %s', JSON.stringify(options)); - const opts = []; options.verbose = true; - for (const key in options) { - const val = options[key as keyof EmulatorOptions]; - const cliKey = '-' + key.replace(/[A-Z]/g, (matched) => `-${matched.toLocaleLowerCase()}`); - if (val === true) { - opts.push(cliKey); - } else if (val) { - opts.push(cliKey, val); - } - } + const cmd = params2Cmd(options); const res = await spawnWaitFor( - `${this.emulatorBin} ${opts.join(' ')}`, + `${this.emulatorBin} ${cmd}`, / control console listening on port (\d+), ADB on port \d+/ ); return { @@ -211,7 +203,7 @@ class Android { */ async waitForDevice(emulatorId: string) { log('waitForDevice: %s', JSON.stringify({ emulatorId })); - await this.adb(emulatorId, 'wait-for-device', 300000); + await this.adb(emulatorId, 'wait-for-device'); } /** @@ -299,9 +291,10 @@ class Android { * @param emulatorId id of emulator * @param apkPath path of apk file */ - async install(emulatorId: string, apkPath: string) { + async install(emulatorId: string, apkPath: string, options?: Record<'l' | 'r' | 't' | 's' | 'd' | 'g', boolean>) { log('install: %s, %s', JSON.stringify({ emulatorId, apkPath })); - const process = await this.adb(emulatorId, `install ${apkPath}`); + const cmdParams = params2Cmd(options); + const process = await this.adb(emulatorId, `install ${cmdParams} ${apkPath}`); if (process.output.match(/Success/)) return; throw new Error('Could not parse output of adb command'); } diff --git a/src/util.ts b/src/util.ts index 881f320..646e0d8 100644 --- a/src/util.ts +++ b/src/util.ts @@ -18,7 +18,7 @@ export function transformCommand(command: string) { /** * Run a shell command. */ -export function spawnExec(command: string, timeout = 60000) { +export function spawnExec(command: string, timeout = 300000) { return new Promise((resolve, reject) => { const { cmd, args } = transformCommand(command); const proc = spawn(cmd, args) as ChildProcessWithoutNullStreams & { @@ -54,7 +54,7 @@ export function spawnExec(command: string, timeout = 60000) { /** * Execute a shell command synchronously. */ -export function spwanSyncExec(command: string, timeout = 60000) { +export function spwanSyncExec(command: string, timeout = 300000) { const { cmd, args } = transformCommand(command); const clock = setTimeout(() => { throw Error('Execution timeout'); @@ -67,7 +67,7 @@ export function spwanSyncExec(command: string, timeout = 60000) { /** * Execute a shell command until a matching output is found. */ -export function spawnWaitFor(command: string, regex: RegExp, timeout = 120000) { +export function spawnWaitFor(command: string, regex: RegExp, timeout = 600000) { return new Promise<{ process: ChildProcessByStdio; matches: RegExpMatchArray; @@ -193,3 +193,17 @@ export function getLocalArch() { if (process.arch === 'x64') return 'x86_64'; return ''; } + +export function params2Cmd(options: Record = {}) { + const opts = []; + for (const key in options) { + const val = options[key]; + const cliKey = '-' + key.replace(/[A-Z]/g, (matched) => `-${matched.toLocaleLowerCase()}`); + if (val === true) { + opts.push(cliKey); + } else if (val) { + opts.push(cliKey, val); + } + } + return opts.join(' '); +}