Skip to content

Commit

Permalink
fix: The default execution timeout is changed to 5 minutes.
Browse files Browse the repository at this point in the history
android.install adds options parameter.
  • Loading branch information
wtto00 committed Oct 1, 2023
1 parent c6e9b8e commit b4ae5e8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
11 changes: 6 additions & 5 deletions README-ZH_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
})
Expand All @@ -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

Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
})
Expand All @@ -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

Expand Down
21 changes: 7 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
filterGroupImages,
getLocalArch,
isExecExpectedResult,
params2Cmd,
processKeyValueGroups,
retry,
spawnExec,
Expand Down Expand Up @@ -184,19 +185,10 @@ class Android {
*/
async start(options: Required<Pick<EmulatorOptions, 'avd'>> & Omit<EmulatorOptions, 'avd'>) {
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 {
Expand All @@ -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');
}

/**
Expand Down Expand Up @@ -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');
}
Expand Down
20 changes: 17 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChildProcessWithoutNullStreams & { output: string }>((resolve, reject) => {
const { cmd, args } = transformCommand(command);
const proc = spawn(cmd, args) as ChildProcessWithoutNullStreams & {
Expand Down Expand Up @@ -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');
Expand All @@ -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<null, internal.Readable, null>;
matches: RegExpMatchArray;
Expand Down Expand Up @@ -193,3 +193,17 @@ export function getLocalArch() {
if (process.arch === 'x64') return 'x86_64';
return '';
}

export function params2Cmd(options: Record<string, string | number | boolean> = {}) {
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(' ');
}

0 comments on commit b4ae5e8

Please sign in to comment.