-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathshell.ts
40 lines (37 loc) · 1.31 KB
/
shell.ts
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
import {util} from '@appium/support';
import {errors} from 'appium/driver';
import _ from 'lodash';
import {exec} from 'teen_process';
import {ADB_SHELL_FEATURE} from '../utils';
export async function mobileShell<T extends boolean>(
command: string,
args: string[] = [],
timeout: number = 20000,
includeStderr?: T,
): Promise<T extends true ? { stdout: string; stderr: string; } : string> {
this.assertFeatureEnabled(ADB_SHELL_FEATURE);
if (!_.isString(command)) {
throw new errors.InvalidArgumentError(`The 'command' argument is mandatory`);
}
const adbArgs = [...this.adb.executable.defaultArgs, 'shell', command, ..._.castArray(args)];
this.log.debug(`Running '${this.adb.executable.path} ${util.quote(adbArgs)}'`);
try {
const {stdout, stderr} = await exec(this.adb.executable.path, adbArgs, {timeout});
if (includeStderr) {
// @ts-ignore We know what we are doing here
return {
stdout,
stderr,
};
}
// @ts-ignore We know what we are doing here
return stdout;
} catch (e) {
const err = /** @type {import('teen_process').ExecError} */ (e);
throw this.log.errorWithException(
`Cannot execute the '${command}' shell command. ` +
`Original error: ${err.message}. ` +
`StdOut: ${err.stdout}. StdErr: ${err.stderr}`,
);
}
}