@@ -4,11 +4,10 @@ import inquirer from 'inquirer';
44
55import Logger from '../../../../logger' ;
66import { symbols } from '../../../../utils' ;
7- import { killEmulatorWithoutWait } from '../../adb' ;
87import { Options , Platform } from '../../interfaces' ;
98import { getBinaryLocation } from '../../utils/common' ;
109import { execBinarySync } from '../../utils/sdk' ;
11- import { showConnectedRealDevices , showRunningAVDs } from '../common' ;
10+ import { showConnectedRealDevices , showConnectedEmulators } from '../common' ;
1211
1312export async function disconnect ( options : Options , sdkRoot : string , platform : Platform ) {
1413 try {
@@ -30,12 +29,12 @@ export async function disconnect(options: Options, sdkRoot: string, platform: Pl
3029 return true ;
3130 }
3231
33- const devicesList = devices . map ( ( device ) => device . udid ) ;
32+ const deviceIdsList = devices . map ( ( device ) => device . udid ) ;
3433
3534 // Here, options.deviceId represent the device id to disconnect.
3635 // If the provided device id is not found then prompt the user to select the device.
3736 if ( options . deviceId && typeof options . deviceId === 'string' ) {
38- if ( ! devicesList . includes ( options . deviceId ) ) {
37+ if ( ! deviceIdsList . includes ( options . deviceId ) ) {
3938 Logger . log ( `${ colors . yellow ( 'Device with the provided ID was not found.' ) } \n` ) ;
4039 options . deviceId = '' ;
4140 }
@@ -46,23 +45,61 @@ export async function disconnect(options: Options, sdkRoot: string, platform: Pl
4645 }
4746
4847 await showConnectedRealDevices ( ) ;
49- await showRunningAVDs ( ) ;
48+ await showConnectedEmulators ( ) ;
5049
5150 if ( ! options . deviceId ) {
5251 const deviceAnswer = await inquirer . prompt ( {
5352 type : 'list' ,
5453 name : 'device' ,
5554 message : 'Select the device to disconnect:' ,
56- choices : devicesList
55+ choices : [ ... deviceIdsList , 'Disconnect all' ]
5756 } ) ;
5857 options . deviceId = deviceAnswer . device ;
5958
6059 Logger . log ( ) ;
6160 }
6261
63- if ( ( options . deviceId as string ) . includes ( 'emulator' ) ) {
64- killEmulatorWithoutWait ( sdkRoot , platform , options . deviceId as string ) ;
65- Logger . log ( colors . green ( 'Successfully shut down the AVD.' ) ) ;
62+ if ( ( options . deviceId as string ) . includes ( 'emulator' ) || options . deviceId === 'Disconnect all' ) {
63+ if ( options . deviceId === 'Disconnect all' ) {
64+ // kill adb server to disconnect all wirelessly connected real devices
65+ adb . killServer ( ) ;
66+ const realDevices = deviceIdsList . filter ( deviceId => ! deviceId . includes ( 'emulator' ) ) ;
67+ if ( realDevices . length ) {
68+ Logger . log ( colors . green ( 'Successfully disconnected all real devices.\n' ) ) ;
69+ }
70+ }
71+
72+ const avdmanagerLocation = getBinaryLocation ( sdkRoot , platform , 'avdmanager' , true ) ;
73+ if ( avdmanagerLocation === '' ) {
74+ Logger . log ( ` ${ colors . red ( symbols ( ) . fail ) } ${ colors . cyan ( 'avdmanager' ) } binary not found.\n` ) ;
75+ Logger . log ( `Run: ${ colors . cyan ( 'npx @nightwatch/mobile-helper android --standalone' ) } to setup missing requirements.` ) ;
76+ Logger . log ( `(Remove the ${ colors . gray ( '--standalone' ) } flag from the above command if setting up for testing.)\n` ) ;
77+
78+ return false ;
79+ }
80+ const stdout = execBinarySync ( avdmanagerLocation , 'avdmanager' , platform , 'list avd -c' ) ;
81+ const installedAvds = stdout ?. split ( '\n' ) . filter ( ( avd ) => avd !== '' ) ;
82+
83+ installedAvds ?. forEach ( avdName => {
84+ adb . getRunningAVDWithRetry ( avdName , 1000 ) . then ( runningAvd => {
85+ if ( runningAvd ) {
86+ if ( options . deviceId !== 'Disconnect all' && runningAvd . udid !== options . deviceId ) {
87+ // If user has selected Disconnect all option then shut down all running avds.
88+ // If not, then we will return until we encounter the selected avd.
89+ return ;
90+ }
91+ adb . killEmulator ( avdName ) . then ( avdShutDown => {
92+ if ( avdShutDown ) {
93+ Logger . log ( `${ colors . green ( 'Successfully shut down: ' ) } ${ runningAvd . udid } ` ) ;
94+ } else {
95+ Logger . log ( `${ colors . red ( 'Failed shut down:' ) } ${ runningAvd . udid } ` ) ;
96+ }
97+ } ) ;
98+ }
99+ } ) . catch ( err => {
100+ options . err = err ;
101+ } ) ;
102+ } ) ;
66103
67104 return true ;
68105 }
0 commit comments