Skip to content

Commit 9453f2c

Browse files
committed
fix(android): catch api issues for --list
This commit catches errors related to unsuitable API installations. Without system images, AVDs cannot be listed as valid targets. fixes #23
1 parent 944fa13 commit 9453f2c

File tree

2 files changed

+69
-45
lines changed

2 files changed

+69
-45
lines changed

src/android/list.ts

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { Target, list } from '../utils/list';
1+
import { list } from '../utils/list';
22

3-
import { Device, getDevices } from './utils/adb';
4-
import { AVD, getDefaultAVD, getInstalledAVDs } from './utils/avd';
5-
import { SDK, getSDK } from './utils/sdk';
3+
import { getDeviceTargets, getVirtualTargets } from './utils/list';
4+
import { getSDK } from './utils/sdk';
65

76
export async function run(args: string[]) {
87
const sdk = await getSDK();
@@ -13,44 +12,3 @@ export async function run(args: string[]) {
1312

1413
return list(args, devices, virtualDevices);
1514
}
16-
17-
export async function getDeviceTargets(sdk: SDK) {
18-
return (await getDevices(sdk))
19-
.filter(device => device.type === 'hardware')
20-
.map(deviceToTarget);
21-
}
22-
23-
export async function getVirtualTargets(sdk: SDK) {
24-
const avds = await getInstalledAVDs(sdk);
25-
const defaultAvd = await getDefaultAVD(sdk, avds);
26-
27-
if (!avds.includes(defaultAvd)) {
28-
avds.push(defaultAvd);
29-
}
30-
31-
return avds.map(avdToTarget);
32-
}
33-
34-
function deviceToTarget(device: Device): Target {
35-
return {
36-
platform: 'android',
37-
model: `${device.manufacturer} ${device.model}`,
38-
sdkVersion: device.sdkVersion,
39-
id: device.serial,
40-
format() {
41-
return `${this.model} (API ${this.sdkVersion}) ${this.id}`;
42-
},
43-
};
44-
}
45-
46-
function avdToTarget(avd: AVD): Target {
47-
return {
48-
platform: 'android',
49-
name: avd.name,
50-
sdkVersion: avd.sdkVersion,
51-
id: avd.id,
52-
format() {
53-
return `${this.name} (API ${this.sdkVersion}) ${this.id}`;
54-
},
55-
};
56-
}

src/android/utils/list.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import * as Debug from 'debug';
2+
3+
import { Target } from '../../utils/list';
4+
5+
import { Device, getDevices } from './adb';
6+
import { AVD, getDefaultAVD, getInstalledAVDs } from './avd';
7+
import { SDK } from './sdk';
8+
9+
const modulePrefix = 'native-run:android:utils:list';
10+
11+
export async function getDeviceTargets(sdk: SDK): Promise<Target[]> {
12+
const debug = Debug(`${modulePrefix}:${getDeviceTargets.name}`);
13+
14+
try {
15+
return (await getDevices(sdk))
16+
.filter(device => device.type === 'hardware')
17+
.map(deviceToTarget);
18+
} catch (e) {
19+
debug('Error getting device targets: %O', e);
20+
}
21+
22+
return [];
23+
}
24+
25+
export async function getVirtualTargets(sdk: SDK): Promise<Target[]> {
26+
const debug = Debug(`${modulePrefix}:${getVirtualTargets.name}`);
27+
28+
try {
29+
const avds = await getInstalledAVDs(sdk);
30+
const defaultAvd = await getDefaultAVD(sdk, avds);
31+
32+
if (!avds.includes(defaultAvd)) {
33+
avds.push(defaultAvd);
34+
}
35+
36+
return avds.map(avdToTarget);
37+
} catch (e) {
38+
debug('Error getting virtual targets: %O', e);
39+
}
40+
41+
return [];
42+
}
43+
44+
export function deviceToTarget(device: Device): Target {
45+
return {
46+
platform: 'android',
47+
model: `${device.manufacturer} ${device.model}`,
48+
sdkVersion: device.sdkVersion,
49+
id: device.serial,
50+
format() {
51+
return `${this.model} (API ${this.sdkVersion}) ${this.id}`;
52+
},
53+
};
54+
}
55+
56+
export function avdToTarget(avd: AVD): Target {
57+
return {
58+
platform: 'android',
59+
name: avd.name,
60+
sdkVersion: avd.sdkVersion,
61+
id: avd.id,
62+
format() {
63+
return `${this.name} (API ${this.sdkVersion}) ${this.id}`;
64+
},
65+
};
66+
}

0 commit comments

Comments
 (0)