Skip to content

Commit

Permalink
feat: list installed avd and connected devices (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsspriyansh authored Aug 22, 2024
1 parent 1c5410e commit 8234c7c
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/commands/android/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ export const AVAILABLE_SUBCOMMANDS: AvailableSubcommands = {
}
]
},
list: {
description: 'List connected devices or installed AVDs',
flags: [{
name: 'device',
description: 'List connected devices (real devices and AVDs)'
},
{
name: 'avd',
description: 'List installed AVDs'
}]
},
install: {
description: 'Install APK or AVD on a device',
flags: [
Expand Down
3 changes: 3 additions & 0 deletions src/commands/android/subcommands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {checkJavaInstallation, getSdkRootFromEnv, getSubcommandHelp} from '../ut
import {connect} from './connect';
import {showHelp} from './help';
import {install} from './install';
import {list} from './list';

export class AndroidSubcommand {
sdkRoot: string;
Expand Down Expand Up @@ -76,6 +77,8 @@ export class AndroidSubcommand {
return await connect(this.options, this.sdkRoot, this.platform);
} else if (this.subcommand === 'install') {
return await install(this.options, this.sdkRoot, this.platform);
} else if (this.subcommand === 'list') {
return await list(this.options, this.sdkRoot, this.platform);
}

return false;
Expand Down
38 changes: 38 additions & 0 deletions src/commands/android/subcommands/list/avd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import colors from 'ansi-colors';

import Logger from '../../../../logger';
import {Platform} from '../../interfaces';
import {getBinaryLocation} from '../../utils/common';
import {execBinarySync} from '../../utils/sdk';
import {showMissingBinaryHelp} from '../common';

export async function listInstalledAVDs(sdkRoot: string, platform: Platform): Promise<boolean> {
try {
const avdmanagerLocation = getBinaryLocation(sdkRoot, platform, 'avdmanager', true);
if (!avdmanagerLocation) {
showMissingBinaryHelp('avdmanager');

return false;
}

const installedAVDs = execBinarySync(avdmanagerLocation, 'avd', platform, 'list avd');
if (!installedAVDs) {
Logger.log(`\n${colors.red('Failed to list installed AVDs!')} Please try again.`);

return false;
}

if (installedAVDs.split('\n').length < 3) {
Logger.log(colors.red('No installed AVDs found!'));
} else {
Logger.log(installedAVDs);
}

return true;
} catch (err) {
Logger.log(colors.red('Error occurred while listing installed AVDs.'));
console.error(err);

return false;
}
}
30 changes: 30 additions & 0 deletions src/commands/android/subcommands/list/device.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import colors from 'ansi-colors';

import Logger from '../../../../logger';
import {Platform} from '../../interfaces';
import ADB from '../../utils/appium-adb';
import {getBinaryLocation} from '../../utils/common';
import {showConnectedEmulators, showConnectedRealDevices, showMissingBinaryHelp} from '../common';

export async function listConnectedDevices(sdkRoot: string, platform: Platform): Promise<boolean> {
const adbLocation = getBinaryLocation(sdkRoot, platform, 'adb', true);
if (adbLocation === '') {
showMissingBinaryHelp('adb');

return false;
}

const adb = await ADB.createADB({allowOfflineDevices: true});
const devices = await adb.getConnectedDevices();

if (!devices.length) {
Logger.log(colors.yellow('No connected devices found.\n'));

return true;
}

await showConnectedRealDevices();
await showConnectedEmulators();

return true;
}
44 changes: 44 additions & 0 deletions src/commands/android/subcommands/list/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import inquirer from 'inquirer';

import Logger from '../../../../logger';
import {Options, Platform} from '../../interfaces';
import {verifyOptions} from '../common';
import {listInstalledAVDs} from './avd';
import {listConnectedDevices} from './device';

export async function list(options: Options, sdkRoot: string, platform: Platform): Promise<boolean> {
const optionsVerified = verifyOptions('list', options);
if (!optionsVerified) {
return false;
}

let subcommandFlag = optionsVerified.subcommandFlag;
if (subcommandFlag === '') {
subcommandFlag = await promptForFlag();
}

if (subcommandFlag === 'avd') {
return await listInstalledAVDs(sdkRoot, platform);
} else if (subcommandFlag === 'device') {
return await listConnectedDevices(sdkRoot, platform);
}

return false;
}

async function promptForFlag(): Promise<string> {
const flagAnswer = await inquirer.prompt({
type: 'list',
name: 'flag',
message: 'Select what do you want to list:',
choices: ['Connected devices', 'Installed AVDs']
});
Logger.log();

const flag = flagAnswer.flag;
if (flag === 'Connected devices') {
return 'device';
}

return 'avd';
}

0 comments on commit 8234c7c

Please sign in to comment.