Skip to content

Commit 8234c7c

Browse files
authored
feat: list installed avd and connected devices (#66)
1 parent 1c5410e commit 8234c7c

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

src/commands/android/constants.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ export const AVAILABLE_SUBCOMMANDS: AvailableSubcommands = {
4242
}
4343
]
4444
},
45+
list: {
46+
description: 'List connected devices or installed AVDs',
47+
flags: [{
48+
name: 'device',
49+
description: 'List connected devices (real devices and AVDs)'
50+
},
51+
{
52+
name: 'avd',
53+
description: 'List installed AVDs'
54+
}]
55+
},
4556
install: {
4657
description: 'Install APK or AVD on a device',
4758
flags: [

src/commands/android/subcommands/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {checkJavaInstallation, getSdkRootFromEnv, getSubcommandHelp} from '../ut
1010
import {connect} from './connect';
1111
import {showHelp} from './help';
1212
import {install} from './install';
13+
import {list} from './list';
1314

1415
export class AndroidSubcommand {
1516
sdkRoot: string;
@@ -76,6 +77,8 @@ export class AndroidSubcommand {
7677
return await connect(this.options, this.sdkRoot, this.platform);
7778
} else if (this.subcommand === 'install') {
7879
return await install(this.options, this.sdkRoot, this.platform);
80+
} else if (this.subcommand === 'list') {
81+
return await list(this.options, this.sdkRoot, this.platform);
7982
}
8083

8184
return false;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import colors from 'ansi-colors';
2+
3+
import Logger from '../../../../logger';
4+
import {Platform} from '../../interfaces';
5+
import {getBinaryLocation} from '../../utils/common';
6+
import {execBinarySync} from '../../utils/sdk';
7+
import {showMissingBinaryHelp} from '../common';
8+
9+
export async function listInstalledAVDs(sdkRoot: string, platform: Platform): Promise<boolean> {
10+
try {
11+
const avdmanagerLocation = getBinaryLocation(sdkRoot, platform, 'avdmanager', true);
12+
if (!avdmanagerLocation) {
13+
showMissingBinaryHelp('avdmanager');
14+
15+
return false;
16+
}
17+
18+
const installedAVDs = execBinarySync(avdmanagerLocation, 'avd', platform, 'list avd');
19+
if (!installedAVDs) {
20+
Logger.log(`\n${colors.red('Failed to list installed AVDs!')} Please try again.`);
21+
22+
return false;
23+
}
24+
25+
if (installedAVDs.split('\n').length < 3) {
26+
Logger.log(colors.red('No installed AVDs found!'));
27+
} else {
28+
Logger.log(installedAVDs);
29+
}
30+
31+
return true;
32+
} catch (err) {
33+
Logger.log(colors.red('Error occurred while listing installed AVDs.'));
34+
console.error(err);
35+
36+
return false;
37+
}
38+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import colors from 'ansi-colors';
2+
3+
import Logger from '../../../../logger';
4+
import {Platform} from '../../interfaces';
5+
import ADB from '../../utils/appium-adb';
6+
import {getBinaryLocation} from '../../utils/common';
7+
import {showConnectedEmulators, showConnectedRealDevices, showMissingBinaryHelp} from '../common';
8+
9+
export async function listConnectedDevices(sdkRoot: string, platform: Platform): Promise<boolean> {
10+
const adbLocation = getBinaryLocation(sdkRoot, platform, 'adb', true);
11+
if (adbLocation === '') {
12+
showMissingBinaryHelp('adb');
13+
14+
return false;
15+
}
16+
17+
const adb = await ADB.createADB({allowOfflineDevices: true});
18+
const devices = await adb.getConnectedDevices();
19+
20+
if (!devices.length) {
21+
Logger.log(colors.yellow('No connected devices found.\n'));
22+
23+
return true;
24+
}
25+
26+
await showConnectedRealDevices();
27+
await showConnectedEmulators();
28+
29+
return true;
30+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import inquirer from 'inquirer';
2+
3+
import Logger from '../../../../logger';
4+
import {Options, Platform} from '../../interfaces';
5+
import {verifyOptions} from '../common';
6+
import {listInstalledAVDs} from './avd';
7+
import {listConnectedDevices} from './device';
8+
9+
export async function list(options: Options, sdkRoot: string, platform: Platform): Promise<boolean> {
10+
const optionsVerified = verifyOptions('list', options);
11+
if (!optionsVerified) {
12+
return false;
13+
}
14+
15+
let subcommandFlag = optionsVerified.subcommandFlag;
16+
if (subcommandFlag === '') {
17+
subcommandFlag = await promptForFlag();
18+
}
19+
20+
if (subcommandFlag === 'avd') {
21+
return await listInstalledAVDs(sdkRoot, platform);
22+
} else if (subcommandFlag === 'device') {
23+
return await listConnectedDevices(sdkRoot, platform);
24+
}
25+
26+
return false;
27+
}
28+
29+
async function promptForFlag(): Promise<string> {
30+
const flagAnswer = await inquirer.prompt({
31+
type: 'list',
32+
name: 'flag',
33+
message: 'Select what do you want to list:',
34+
choices: ['Connected devices', 'Installed AVDs']
35+
});
36+
Logger.log();
37+
38+
const flag = flagAnswer.flag;
39+
if (flag === 'Connected devices') {
40+
return 'device';
41+
}
42+
43+
return 'avd';
44+
}

0 commit comments

Comments
 (0)