Skip to content

Commit

Permalink
fix(android): catch api issues for --list
Browse files Browse the repository at this point in the history
This commit catches errors related to unsuitable API installations.
Without system images, AVDs cannot be listed as valid targets.

fixes #23
  • Loading branch information
imhoffd committed May 29, 2019
1 parent 944fa13 commit 9453f2c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 45 deletions.
48 changes: 3 additions & 45 deletions src/android/list.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Target, list } from '../utils/list';
import { list } from '../utils/list';

import { Device, getDevices } from './utils/adb';
import { AVD, getDefaultAVD, getInstalledAVDs } from './utils/avd';
import { SDK, getSDK } from './utils/sdk';
import { getDeviceTargets, getVirtualTargets } from './utils/list';
import { getSDK } from './utils/sdk';

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

return list(args, devices, virtualDevices);
}

export async function getDeviceTargets(sdk: SDK) {
return (await getDevices(sdk))
.filter(device => device.type === 'hardware')
.map(deviceToTarget);
}

export async function getVirtualTargets(sdk: SDK) {
const avds = await getInstalledAVDs(sdk);
const defaultAvd = await getDefaultAVD(sdk, avds);

if (!avds.includes(defaultAvd)) {
avds.push(defaultAvd);
}

return avds.map(avdToTarget);
}

function deviceToTarget(device: Device): Target {
return {
platform: 'android',
model: `${device.manufacturer} ${device.model}`,
sdkVersion: device.sdkVersion,
id: device.serial,
format() {
return `${this.model} (API ${this.sdkVersion}) ${this.id}`;
},
};
}

function avdToTarget(avd: AVD): Target {
return {
platform: 'android',
name: avd.name,
sdkVersion: avd.sdkVersion,
id: avd.id,
format() {
return `${this.name} (API ${this.sdkVersion}) ${this.id}`;
},
};
}
66 changes: 66 additions & 0 deletions src/android/utils/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import * as Debug from 'debug';

import { Target } from '../../utils/list';

import { Device, getDevices } from './adb';
import { AVD, getDefaultAVD, getInstalledAVDs } from './avd';
import { SDK } from './sdk';

const modulePrefix = 'native-run:android:utils:list';

export async function getDeviceTargets(sdk: SDK): Promise<Target[]> {
const debug = Debug(`${modulePrefix}:${getDeviceTargets.name}`);

try {
return (await getDevices(sdk))
.filter(device => device.type === 'hardware')
.map(deviceToTarget);
} catch (e) {
debug('Error getting device targets: %O', e);
}

return [];
}

export async function getVirtualTargets(sdk: SDK): Promise<Target[]> {
const debug = Debug(`${modulePrefix}:${getVirtualTargets.name}`);

try {
const avds = await getInstalledAVDs(sdk);
const defaultAvd = await getDefaultAVD(sdk, avds);

if (!avds.includes(defaultAvd)) {
avds.push(defaultAvd);
}

return avds.map(avdToTarget);
} catch (e) {
debug('Error getting virtual targets: %O', e);
}

return [];
}

export function deviceToTarget(device: Device): Target {
return {
platform: 'android',
model: `${device.manufacturer} ${device.model}`,
sdkVersion: device.sdkVersion,
id: device.serial,
format() {
return `${this.model} (API ${this.sdkVersion}) ${this.id}`;
},
};
}

export function avdToTarget(avd: AVD): Target {
return {
platform: 'android',
name: avd.name,
sdkVersion: avd.sdkVersion,
id: avd.id,
format() {
return `${this.name} (API ${this.sdkVersion}) ${this.id}`;
},
};
}

0 comments on commit 9453f2c

Please sign in to comment.