feat(adb): subprocess API v3 (#786)

This commit is contained in:
Simon Chan 2025-08-24 15:04:52 +08:00 committed by GitHub
parent 927d50682d
commit 3a6fd9ca08
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
33 changed files with 979 additions and 580 deletions

View file

@ -1,3 +1,5 @@
import { escapeArg } from "@yume-chan/adb";
export function buildArguments<T>(
commands: readonly string[],
options: Partial<T> | undefined,
@ -6,19 +8,34 @@ export function buildArguments<T>(
const args = commands.slice();
if (options) {
for (const [key, value] of Object.entries(options)) {
if (value) {
const option = map[key as keyof T];
if (option) {
args.push(option);
switch (typeof value) {
case "number":
args.push(value.toString());
break;
case "string":
args.push(value);
break;
if (value === undefined || value === null) {
continue;
}
const option = map[key as keyof T];
// Empty string means positional argument,
// they must be added at the end,
// so let the caller handle it.
if (option === undefined || option === "") {
continue;
}
switch (typeof value) {
case "boolean":
if (value) {
args.push(option);
}
}
break;
case "number":
args.push(option, value.toString());
break;
case "string":
args.push(option, escapeArg(value));
break;
default:
throw new Error(
`Unsupported type for option ${key}: ${typeof value}`,
);
}
}
}
@ -27,3 +44,5 @@ export function buildArguments<T>(
export type SingleUser = number | "current";
export type SingleUserOrAll = SingleUser | "all";
export type Optional<T extends object> = { [K in keyof T]?: T[K] | undefined };