@@ -3,19 +3,25 @@ import ADB from 'appium-adb';
33import inquirer from 'inquirer' ;
44
55import Logger from '../../../../logger' ;
6- import { symbols } from '../../../../utils' ;
76import { Options , Platform } from '../../interfaces' ;
87import { getBinaryLocation } from '../../utils/common' ;
98import { execBinarySync } from '../../utils/sdk' ;
10- import { showConnectedRealDevices , showConnectedEmulators } from '../common' ;
9+ import { showConnectedRealDevices , showConnectedEmulators , verifyOptions , showMissingBinaryHelp } from '../common' ;
1110
1211export async function disconnect ( options : Options , sdkRoot : string , platform : Platform ) {
12+ const optionsVerified = verifyOptions ( 'disconnect' , options ) ;
13+ if ( ! optionsVerified ) {
14+ return false ;
15+ }
16+
17+ return await disconnectDevice ( options , sdkRoot , platform ) ;
18+ }
19+
20+ async function disconnectDevice ( options : Options , sdkRoot : string , platform : Platform ) {
1321 try {
1422 const adbLocation = getBinaryLocation ( sdkRoot , platform , 'adb' , true ) ;
1523 if ( adbLocation === '' ) {
16- Logger . log ( ` ${ colors . red ( symbols ( ) . fail ) } ${ colors . cyan ( 'adb' ) } binary not found.\n` ) ;
17- Logger . log ( `Run: ${ colors . cyan ( 'npx @nightwatch/mobile-helper android --standalone' ) } to setup missing requirements.` ) ;
18- Logger . log ( `(Remove the ${ colors . gray ( '--standalone' ) } flag from the above command if setting up for testing.)\n` ) ;
24+ showMissingBinaryHelp ( 'adb' ) ;
1925
2026 return false ;
2127 }
@@ -35,7 +41,7 @@ export async function disconnect(options: Options, sdkRoot: string, platform: Pl
3541 // If the provided device id is not found then prompt the user to select the device.
3642 if ( options . deviceId && typeof options . deviceId === 'string' ) {
3743 if ( ! deviceIdsList . includes ( options . deviceId ) ) {
38- Logger . log ( `${ colors . yellow ( 'Device with the provided ID was not found.' ) } \n` ) ;
44+ Logger . log ( `${ colors . yellow ( 'Device with the provided id was not found.' ) } \n` ) ;
3945 options . deviceId = '' ;
4046 }
4147 } else if ( options . deviceId === true ) {
@@ -62,25 +68,28 @@ export async function disconnect(options: Options, sdkRoot: string, platform: Pl
6268 if ( ( options . deviceId as string ) . includes ( 'emulator' ) || options . deviceId === 'Disconnect all' ) {
6369 if ( options . deviceId === 'Disconnect all' ) {
6470 // kill adb server to disconnect all wirelessly connected real devices
65- adb . killServer ( ) ;
6671 const realDevices = deviceIdsList . filter ( deviceId => ! deviceId . includes ( 'emulator' ) ) ;
6772 if ( realDevices . length ) {
73+ adb . killServer ( ) ;
6874 Logger . log ( colors . green ( 'Successfully disconnected all real devices.\n' ) ) ;
6975 }
7076 }
7177
7278 const avdmanagerLocation = getBinaryLocation ( sdkRoot , platform , 'avdmanager' , true ) ;
7379 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` ) ;
80+ showMissingBinaryHelp ( 'avdmanager' ) ;
7781
7882 return false ;
7983 }
8084 const stdout = execBinarySync ( avdmanagerLocation , 'avdmanager' , platform , 'list avd -c' ) ;
81- const installedAvds = stdout ?. split ( '\n' ) . filter ( ( avd ) => avd !== '' ) ;
85+ if ( stdout === null ) {
86+ Logger . log ( `${ colors . red ( 'Something went wrong when trying to shut down AVD.' ) } Please try again.` ) ;
87+
88+ return false ;
89+ }
90+ const installedAvds = stdout . split ( '\n' ) . filter ( ( avd ) => avd !== '' ) ;
8291
83- installedAvds ? .forEach ( avdName => {
92+ installedAvds . forEach ( avdName => {
8493 adb . getRunningAVDWithRetry ( avdName , 1000 ) . then ( runningAvd => {
8594 if ( runningAvd ) {
8695 if ( options . deviceId !== 'Disconnect all' && runningAvd . udid !== options . deviceId ) {
@@ -96,8 +105,10 @@ export async function disconnect(options: Options, sdkRoot: string, platform: Pl
96105 }
97106 } ) ;
98107 }
99- } ) . catch ( err => {
100- options . err = err ;
108+ } ) . catch ( _err => {
109+ // Error is caught to prevent unhandled rejection, but not used.
110+ // This error is not revelant to users.
111+ void _err ;
101112 } ) ;
102113 } ) ;
103114
@@ -106,11 +117,11 @@ export async function disconnect(options: Options, sdkRoot: string, platform: Pl
106117
107118 const disconnectionStatus = execBinarySync ( adbLocation , 'adb' , platform , `disconnect ${ options . deviceId } ` ) ;
108119 if ( disconnectionStatus ?. includes ( 'disconnected' ) ) {
109- Logger . log ( colors . green ( 'Successfully disconnected the device.' ) ) ;
120+ Logger . log ( ` ${ colors . green ( 'Successfully disconnected: ' ) } ${ options . deviceId } \n` ) ;
110121
111122 return true ;
112123 } else {
113- Logger . log ( `${ colors . red ( 'Failed to disconnect the device.' ) } Please try again.` ) ;
124+ Logger . log ( `${ colors . red ( 'Failed to disconnect the device.' ) } Please try again.\n ` ) ;
114125 }
115126
116127 return false ;
0 commit comments