Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/commands/android/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import inquirer from 'inquirer';
import path from 'path';
import os from 'os';
import path from 'path';

import {AvailableOptions, AvailableSubcommands, SdkBinary} from './interfaces';

Expand Down Expand Up @@ -40,6 +40,10 @@ export const AVAILABLE_SUBCOMMANDS: AvailableSubcommands = {
description: 'Connect a real device wirelessly'
}
]
},
disconnect: {
description: 'Disconnect a real device or emulator',
options: []
}
};

Expand Down
78 changes: 78 additions & 0 deletions src/commands/android/subcommands/disconnect/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import colors from 'ansi-colors';
import ADB from 'appium-adb';
import inquirer from 'inquirer';

import Logger from '../../../../logger';
import {killEmulatorWithoutWait} from '../../adb';
import {Options, Platform} from '../../interfaces';
import {getBinaryLocation} from '../../utils/common';
import {execBinarySync} from '../../utils/sdk';
import {showConnectedRealDevices, showRunningAVDs} from '../common';

export async function disconnect(options: Options, sdkRoot: string, platform: Platform) {
try {
const adbLocation = getBinaryLocation(sdkRoot, platform, 'adb', true);
const adb = await ADB.createADB({allowOfflineDevices: true});
const devices = await adb.getConnectedDevices();

if (devices.length === 0) {
Logger.log(`${colors.yellow('No device found running.')}`);

return true;
}

const devicesList = devices.map((device) => device.udid);

// Here, options.s represent the device id to disconnect.
// If the provided device id is not found then prompt the user to select the device.
if (options.s && typeof options.s === 'string') {
if (!devicesList.includes(options.s)) {
Logger.log(`${colors.yellow('Device with the provided ID was not found.')}\n`);
options.s = '';
}
} else if (options.s === true) {
// If the --s flag is present without a value then assign it an empty string
// to follow the default flow.
options.s = '';
}

await showConnectedRealDevices();
await showRunningAVDs();

if (!options.s) {
const deviceAnswer = await inquirer.prompt({
type: 'list',
name: 'device',
message: 'Select the device to disconnect:',
choices: devicesList
});
options.s = deviceAnswer.device;

Logger.log();
}

if ((options.s as string).includes('emulator')) {
killEmulatorWithoutWait(sdkRoot, platform, options.s as string);
Logger.log(colors.green('Successfully shut down the AVD.'));

return true;
}

const disconnectionStatus = execBinarySync(adbLocation, 'adb', platform, `disconnect ${options.s}`);
if (disconnectionStatus?.includes('disconnected')) {
Logger.log(colors.green('Successfully disconnected the device.'));

return true;
} else {
Logger.log(`${colors.red('Failed to disconnect the device.')} Please try again.`);
}

return false;
} catch (err) {
Logger.log(colors.red('Error occured while disconnecting a device.'));
console.error(err);

return false;
}
}

9 changes: 6 additions & 3 deletions src/commands/android/subcommands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import colors from 'ansi-colors';
import * as dotenv from 'dotenv';
import path from 'path';

import {checkJavaInstallation, getSdkRootFromEnv} from '../utils/common';
import {connect} from './connect';
import {getPlatformName} from '../../../utils';
import Logger from '../../../logger';
import {getPlatformName} from '../../../utils';
import {Options, Platform} from '../interfaces';
import {checkJavaInstallation, getSdkRootFromEnv} from '../utils/common';
import {connect} from './connect';
import {disconnect} from './disconnect';

export class AndroidSubcommand {
sdkRoot: string;
Expand Down Expand Up @@ -56,6 +57,8 @@ export class AndroidSubcommand {
async executeSubcommand(): Promise<boolean> {
if (this.subcommand === 'connect') {
return await connect(this.options, this.sdkRoot, this.platform);
} else if (this.subcommand === 'disconnect') {
return await disconnect(this.options, this.sdkRoot, this.platform);
}

return false;
Expand Down