@@ -392,19 +392,34 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
392
392
}
393
393
let deviceInstances = this . getDeviceInstances ( ) ;
394
394
395
- //if no --device is passed and no devices are found, the default emulator is started
396
395
if ( ! data . deviceId && _ . isEmpty ( deviceInstances ) ) {
397
396
if ( ! this . $hostInfo . isDarwin && this . $mobileHelper . isiOSPlatform ( data . platform ) ) {
398
397
this . $errors . failWithoutHelp ( constants . ERROR_NO_DEVICES_CANT_USE_IOS_SIMULATOR ) ;
399
398
}
399
+ }
400
+
401
+ try {
402
+ await this . _startEmulatorIfNecessary ( data ) ;
403
+ } catch ( err ) {
404
+ const errorMessage = this . getEmulatorError ( err , data . platform ) ;
405
+
406
+ this . $errors . failWithoutHelp ( errorMessage ) ;
407
+ }
408
+ }
409
+ }
400
410
411
+ private async _startEmulatorIfNecessary ( data ?: Mobile . IDevicesServicesInitializationOptions ) : Promise < void > {
412
+ let deviceInstances = this . getDeviceInstances ( ) ;
413
+
414
+ //if no --device is passed and no devices are found, the default emulator is started
415
+ if ( ! data . deviceId && _ . isEmpty ( deviceInstances ) ) {
401
416
return await this . startEmulator ( data . platform ) ;
402
417
}
403
418
404
419
//check if --device(value) is running, if it's not or it's not the same as is specified, start with name from --device(value)
405
420
if ( data . deviceId ) {
406
421
if ( ! helpers . isNumber ( data . deviceId ) ) {
407
- let activeDeviceInstance = _ . find ( this . getDeviceInstances ( ) , ( device : Mobile . IDevice ) => { return device . deviceInfo . identifier === data . deviceId ; } ) ;
422
+ let activeDeviceInstance = _ . find ( deviceInstances , ( device : Mobile . IDevice ) => { return device . deviceInfo . identifier === data . deviceId ; } ) ;
408
423
if ( ! activeDeviceInstance ) {
409
424
return await this . startEmulator ( data . platform , data . deviceId ) ;
410
425
}
@@ -418,7 +433,6 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
418
433
return await this . startEmulator ( data . platform ) ;
419
434
}
420
435
}
421
- }
422
436
}
423
437
424
438
/**
@@ -672,6 +686,17 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
672
686
shouldReturnImmediateResult = shouldReturnImmediateResult || false ;
673
687
return { shouldReturnImmediateResult : shouldReturnImmediateResult , platform : platform } ;
674
688
}
689
+
690
+ private getEmulatorError ( error : Error , platform : string ) : string {
691
+ let emulatorName = "Emulator" ;
692
+ if ( this . $mobileHelper . isiOSPlatform ( platform ) ) {
693
+ emulatorName = "Simulator" ;
694
+ }
695
+
696
+ return `Cannot find connected devices.${ EOL } ` +
697
+ `${ emulatorName } start failed with: ${ error . message } ${ EOL } ` +
698
+ `To list currently connected devices and verify that the specified identifier exists, run '${ this . $staticConfig . CLIENT_NAME . toLowerCase ( ) } device'.` ;
699
+ }
675
700
}
676
701
677
702
$injector . register ( "devicesService" , DevicesService ) ;
0 commit comments