Skip to content

Commit

Permalink
feat: listInstalledImages
Browse files Browse the repository at this point in the history
  • Loading branch information
wtto00 committed Sep 17, 2023
1 parent fb20d03 commit 92f9b4a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
19 changes: 19 additions & 0 deletions README-ZH_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,25 @@ android
});
```

### listInstalledImages

列出已安装的所有安卓镜像。

```js
android
.listInstalledImages()
.then((res) => {
res.forEach((item) => {
console.log(
`name: ${item.name}, type: ${item.type}, sdk: ${item.sdk}, vendor: ${item.vendor}, arch: ${item.arch}`
);
});
})
.catch((err) => {
console.log(err);
});
```

### adb

使用`adb`执行自定义命令。
Expand Down
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ android

### listImages

List available Android images on this machine.
List available android images on this machine.

```js
android
Expand All @@ -343,6 +343,25 @@ android
});
```

### listInstalledImages

List installed android images on this machine.

```js
android
.listInstalledImages()
.then((res) => {
res.forEach((item) => {
console.log(
`name: ${item.name}, type: ${item.type}, sdk: ${item.sdk}, vendor: ${item.vendor}, arch: ${item.arch}`
);
});
})
.catch((err) => {
console.log(err);
});
```

### adb

Use `adb` to execute commands.
Expand Down
6 changes: 3 additions & 3 deletions spec/emulatorAVD.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe("emulator AVD", () => {

test("create AVD", async () => {
const avdName = `TestCreate_${Math.random()}`;
const images = (await android.listImages()).filter((item) => item.vendor === "default");
const images = (await android.listInstalledImages()).filter((item) => item.vendor === "default");
if (images.length === 0) return;
await android.createAVD({ name: avdName, package: images[images.length - 1].name, force: false });
const res = await android.hasAVD(avdName);
Expand All @@ -16,7 +16,7 @@ describe("emulator AVD", () => {
test("create an existing AVD", async () => {
const avds = await android.listAVDs();
if (avds.length === 0) return;
const images = (await android.listImages()).filter((item) => item.vendor === "default");
const images = (await android.listInstalledImages()).filter((item) => item.vendor === "default");
if (images.length === 0) return;
await expect(async () => {
await android.createAVD({ name: avds[0].Name, package: images[images.length - 1].name });
Expand All @@ -26,7 +26,7 @@ describe("emulator AVD", () => {
test("force create an existing AVD", async () => {
const avds = await android.listAVDs();
if (avds.length === 0) return;
const images = (await android.listImages()).filter((item) => item.vendor === "default");
const images = (await android.listInstalledImages()).filter((item) => item.vendor === "default");
if (images.length === 0) return;
await android.createAVD({ name: avds[0].Name, package: images[images.length - 1].name, force: true });
const res = await android.hasAVD(avds[0].Name);
Expand Down
32 changes: 22 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class Android {
*/
async createAVD(options: CreateAVDOptions) {
if (options.package) {
await this.sdkmanager(`--install ${options.package}`);
await this.sdkmanager(`--install ${options.package}`, 180000);
}
const cmdParams = Object.keys(options).reduce((prev, curr) => {
const val = options[curr as keyof CreateAVDOptions];
Expand Down Expand Up @@ -373,38 +373,50 @@ class Android {
return filterGroupImages(proc.output);
}

/**
* List installed Android images on this machine.
*/
async listInstalledImages() {
const proc = await this.sdkmanager("--list_installed");
return filterGroupImages(proc.output);
}

/**
* Use `adb` to execute commands.
* @param emulatorId id of emulator
* @param cmd command
* @param timeout Execution timeout
*/
adb(emulatorId: string, cmd?: string) {
if (cmd) return spawnExec(`${this.adbBin} -s ${emulatorId} ${cmd}`);
return spawnExec(`${this.adbBin} ${emulatorId}`);
adb(emulatorId: string, cmd?: string, timeout?: number) {
if (cmd) return spawnExec(`${this.adbBin} -s ${emulatorId} ${cmd}`, timeout);
return spawnExec(`${this.adbBin} ${emulatorId}`, timeout);
}

/**
* Use `avdmanager` to execute commands.
* @param cmd command
* @param timeout Execution timeout
*/
avdmanager(cmd: string) {
return spawnExec(`${this.avdmanagerBin} ${cmd}`);
avdmanager(cmd: string, timeout?: number) {
return spawnExec(`${this.avdmanagerBin} ${cmd}`, timeout);
}

/**
* Use `sdkmanager` to execute commands.
* @param cmd command
* @param timeout Execution timeout
*/
sdkmanager(cmd: string) {
return spawnExec(`${this.sdkmanagerBin} ${cmd}`);
sdkmanager(cmd: string, timeout?: number) {
return spawnExec(`${this.sdkmanagerBin} ${cmd}`, timeout);
}

/**
* Use `emulator` to execute commands.
* @param cmd command
* @param timeout Execution timeout
*/
emulator(cmd: string) {
return spawnExec(`${this.emulatorBin} ${cmd}`);
emulator(cmd: string, timeout?: number) {
return spawnExec(`${this.emulatorBin} ${cmd}`, timeout);
}
}

Expand Down

0 comments on commit 92f9b4a

Please sign in to comment.