diff --git a/src/commands/android/constants.ts b/src/commands/android/constants.ts index 4c30706..c504bd7 100644 --- a/src/commands/android/constants.ts +++ b/src/commands/android/constants.ts @@ -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: [ diff --git a/src/commands/android/subcommands/index.ts b/src/commands/android/subcommands/index.ts index cdcc05d..47209b8 100644 --- a/src/commands/android/subcommands/index.ts +++ b/src/commands/android/subcommands/index.ts @@ -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; @@ -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; diff --git a/src/commands/android/subcommands/list/avd.ts b/src/commands/android/subcommands/list/avd.ts new file mode 100644 index 0000000..93f3f04 --- /dev/null +++ b/src/commands/android/subcommands/list/avd.ts @@ -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 { + 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; + } +} diff --git a/src/commands/android/subcommands/list/device.ts b/src/commands/android/subcommands/list/device.ts new file mode 100644 index 0000000..35c50b9 --- /dev/null +++ b/src/commands/android/subcommands/list/device.ts @@ -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 { + 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; +} diff --git a/src/commands/android/subcommands/list/index.ts b/src/commands/android/subcommands/list/index.ts new file mode 100644 index 0000000..0bf7cd1 --- /dev/null +++ b/src/commands/android/subcommands/list/index.ts @@ -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 { + 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 { + 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'; +}