Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly add async to Promise returning functions. #81

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 29 additions & 26 deletions src/fastboot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class Fastboot extends Tool {
}

/** Write a file to a flash partition */
async flash(
public async flash(
images: FastbootFlashImage[],
progress: ProgressCallback = () => {}
): Promise<void> {
Expand Down Expand Up @@ -249,45 +249,48 @@ export class Fastboot extends Tool {
}

/** Download and boot kernel */
async boot(image: string): Promise<void> {
public async boot(image: string): Promise<void> {
await this.exec("boot", image);
}

/** Reflash device from update.zip and set the flashed slot as active */
async update(image: string, wipe: string | boolean = false): Promise<void> {
public async update(
image: string,
wipe: string | boolean = false
): Promise<void> {
await this._withConfig({ wipe }).exec("update", image);
}

/** Reboot device into bootloader */
async rebootBootloader(): Promise<void> {
public async rebootBootloader(): Promise<void> {
await this.exec("reboot-bootloader");
}

/**
* Reboot device into userspace fastboot (fastbootd) mode
* Note: this only works on devices that support dynamic partitions.
*/
async rebootFastboot(): Promise<void> {
public async rebootFastboot(): Promise<void> {
await this.exec("reboot-fastboot");
}

/** Reboot device into recovery */
async rebootRecovery(): Promise<void> {
public async rebootRecovery(): Promise<void> {
await this.exec("reboot-recovery");
}

/** Reboot device */
async reboot(): Promise<void> {
public async reboot(): Promise<void> {
await this.exec("reboot");
}

/** Continue with autoboot */
async continue(): Promise<void> {
public async continue(): Promise<void> {
await this.exec("continue");
}

/** Format a flash partition. Can override the fs type and/or size the bootloader reports */
async format(
public async format(
partition: string,
type?: string,
size?: string | number
Expand All @@ -304,12 +307,12 @@ export class Fastboot extends Tool {
}

/** Erase a flash partition */
async erase(partition: string): Promise<void> {
public async erase(partition: string): Promise<void> {
await this.exec("erase", partition);
}

/** Sets the active slot */
setActive(slot: string) {
public async setActive(slot: string): Promise<void> {
return this._withConfig({ setActive: slot })
.exec()
.then(stdout => {
Expand All @@ -322,7 +325,7 @@ export class Fastboot extends Tool {
}

/** Create a logical partition with the given name and size, in the super partition */
createLogicalPartition(
public async createLogicalPartition(
partition: string,
size: string | number
): Promise<void> {
Expand All @@ -332,20 +335,20 @@ export class Fastboot extends Tool {
}

/** Resize a logical partition with the given name and final size, in the super partition */
async resizeLogicalPartition(
public async resizeLogicalPartition(
partition: string,
size: string | number
): Promise<void> {
await this.exec("resize-logical-partition", partition, size);
}

/** Delete a logical partition with the given name */
async deleteLogicalPartition(partition: string): Promise<void> {
public async deleteLogicalPartition(partition: string): Promise<void> {
await this.exec("delete-logical-partition", partition);
}

/** Wipe the super partition and reset the partition layout */
async wipeSuper(image: string): Promise<void> {
public async wipeSuper(image: string): Promise<void> {
await this.exec("wipe-super", image);
}

Expand All @@ -354,7 +357,7 @@ export class Fastboot extends Tool {
//////////////////////////////////////////////////////////////////////////////

/** Lift OEM lock */
async oemUnlock(
public async oemUnlock(
/** optional unlock code (including 0x if necessary) */
code?: string | number
): Promise<void> {
Expand All @@ -372,50 +375,50 @@ export class Fastboot extends Tool {
}

/** Enforce OEM lock */
async oemLock(): Promise<void> {
public async oemLock(): Promise<void> {
await this.exec("oem", "lock");
}

/** unlock partitions for flashing */
async flashingUnlock(): Promise<void> {
public async flashingUnlock(): Promise<void> {
await this.exec("flashing", "unlock");
}

/** lock partitions for flashing */
async flashingLock(): Promise<void> {
public async flashingLock(): Promise<void> {
await this.exec("flashing", "lock");
}

/** unlock 'critical' bootloader partitions */
async flashingUnlockCritical(): Promise<void> {
public async flashingUnlockCritical(): Promise<void> {
await this.exec("flashing", "unlock_critical");
}

/** lock 'critical' bootloader partitions */
async flashingLockCritical(): Promise<void> {
public async flashingLockCritical(): Promise<void> {
await this.exec("flashing", "lock_critical");
}

/** Find out if a device can be flashing-unlocked */
async getUnlockAbility(): Promise<boolean> {
public async getUnlockAbility(): Promise<boolean> {
return this.exec("flashing", "get_unlock_ability")
.then(stdout => stdout === "1")
.catch(() => false);
}

/** Find out if a device can be seen by fastboot */
async hasAccess(): Promise<boolean> {
public async hasAccess(): Promise<boolean> {
return (await this.exec("devices")).includes("fastboot");
}

/** wait for a device */
async wait(): Promise<"bootloader"> {
public async wait(): Promise<"bootloader"> {
await super.wait();
return "bootloader";
}

/** get bootloader var */
async getvar(variable: string): Promise<string> {
public async getvar(variable: string): Promise<string> {
const result = await this.exec("getvar", variable);
const [name, value] = result
.replace(/\r\n/g, "\n")
Expand All @@ -433,7 +436,7 @@ export class Fastboot extends Tool {
}

/** get device codename from product bootloader var */
getDeviceName(): Promise<string> {
public async getDeviceName(): Promise<string> {
return this.getvar("product");
}
}
14 changes: 8 additions & 6 deletions src/heimdall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ export class Heimdall extends Tool {
}

/** Find out if a device in download mode can be seen by heimdall */
detect(): Promise<boolean> {
public async detect(): Promise<boolean> {
return this.hasAccess();
}

/** Find out if a device in download mode can be seen by heimdall */
hasAccess(): Promise<boolean> {
public async hasAccess(): Promise<boolean> {
return this.exec("detect")
.then(() => true)
.catch(error => {
Expand All @@ -58,12 +58,12 @@ export class Heimdall extends Tool {
}

/** Wait for a device */
wait(): Promise<"download"> {
public async wait(): Promise<"download"> {
return super.wait().then(() => "download");
}

/** Prints the contents of a PIT file in a human readable format. If a filename is not provided then Heimdall retrieves the PIT file from the connected device. */
printPit(file?: string): Promise<string[]> {
public async printPit(file?: string): Promise<string[]> {
return this.exec("print-pit", ...(file ? ["--file", file] : [])).then(r =>
r
.split("\n\nEnding session...")[0]
Expand All @@ -74,7 +74,7 @@ export class Heimdall extends Tool {
}

/** get partitions from pit file */
getPartitions(): Promise<{}[]> {
public async getPartitions(): Promise<{}[]> {
return this.printPit().then(r =>
r.map(r =>
r
Expand All @@ -89,7 +89,9 @@ export class Heimdall extends Tool {
}

/** Flash firmware files to partitions (names or identifiers) */
async flash(images: { partition: string; file: string }[]): Promise<void> {
public async flash(
images: { partition: string; file: string }[]
): Promise<void> {
// TODO report progress similar to fastboot.flash()
await this.exec(
"flash",
Expand Down
26 changes: 15 additions & 11 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,23 @@ export class DeviceTools extends Interface {
this.heimdall = new Heimdall({ ...heimdallOptions, signals });

["adb", "fastboot", "heimdall"].forEach(tool => {
//@ts-ignore
this[tool].on("exec", r => this.emit("exec", r));
//@ts-ignore
this[tool].on("spawn:start", r => this.emit("spawn:start", r));
//@ts-ignore
this[tool].on("spawn:exit", r => this.emit("spawn:exit", r));
//@ts-ignore
this[tool].on("spawn:error", r => this.emit("spawn:error", r));
this[tool].on("exec", (r: (string | number | null | undefined)[]) =>
this.emit("exec", r)
);
this[tool].on("spawn:start", (r: { cmd: string[] }) =>
this.emit("spawn:start", r)
);
this[tool].on("spawn:exit", (r: { cmd: string[]; error?: Error }) =>
this.emit("spawn:exit", r)
);
this[tool].on("spawn:error", (r: { cmd: string[]; error?: Error }) =>
this.emit("spawn:error", r)
);
});
}

/** returns clone with variation in env vars */
_withEnv(env: NodeJS.ProcessEnv): this {
public _withEnv(env: NodeJS.ProcessEnv): this {
const ret = Object.create(this);
ret.adb = this.adb._withEnv(env);
ret.fastboot = this.fastboot._withEnv(env);
Expand All @@ -69,7 +73,7 @@ export class DeviceTools extends Interface {
}

/** Wait for a device */
wait(): Promise<ActualDeviceState | "bootloader" | "download"> {
public async wait(): Promise<ActualDeviceState | "bootloader" | "download"> {
const controller = new AbortController();
const _this = this._withSignals(controller.signal);
return Promise.race([
Expand All @@ -80,7 +84,7 @@ export class DeviceTools extends Interface {
}

/** Resolve device name */
getDeviceName(): Promise<string> {
public async getDeviceName(): Promise<string> {
// TODO support heimdall
return this.adb
.getDeviceName()
Expand Down
10 changes: 6 additions & 4 deletions src/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export abstract class Tool extends Interface {
}

/** return a clone with a specified variation in the config options */
_withConfig(config: typeof this.config): this {
public _withConfig(config: typeof this.config): this {
const ret = Object.create(this);
ret.config = { ...this.config };
for (const key in config) {
Expand Down Expand Up @@ -274,7 +274,7 @@ export abstract class Tool extends Interface {
[key: `__${keyof typeof this.argsModel}`]: (val?: any) => this;

/** apply config options to the tool instance */
applyConfig(config: typeof this.config): void {
public applyConfig(config: typeof this.config): void {
for (const key in this.config) {
if (
(
Expand All @@ -298,7 +298,9 @@ export abstract class Tool extends Interface {
}

/** Execute a command. Used for quick operations that do not require real-time data access. Output is trimmed. */
async exec(...args: (string | number | null | undefined)[]): Promise<string> {
public async exec(
...args: (string | number | null | undefined)[]
): Promise<string> {
this.signal.throwIfAborted();
const allArgs: string[] = this.constructArgs(args);
const cmd = [this.tool, ...allArgs];
Expand All @@ -323,7 +325,7 @@ export abstract class Tool extends Interface {
}

/** Spawn a child process. Used for long-running operations that require real-time data access. */
spawn(
public spawn(
...args: (string | number | null | undefined)[]
): ChildProcess & { stdin: Writable; stdout: Readable; stderr: Readable } {
this.signal.throwIfAborted();
Expand Down