Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit bf42d51

Browse files
KristianDDPlamen5kov
authored andcommitted
Fix simulator started on run with fake device id. (#993)
* Fix simulator started on run with fake device id. * Fix comments
1 parent 0f10a81 commit bf42d51

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export class ProvisionType {
88

99
export class DeviceTypes {
1010
static Emulator = "Emulator";
11+
static Simulator = "Simulator";
1112
static Device = "Device";
1213
}
1314

mobile/android/android-emulator-services.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class AndroidEmulatorServices implements Mobile.IAndroidEmulatorServices {
119119
await this.unlockScreen(emulatorId);
120120
} else {
121121
if (emulatorImage) {
122-
this.$errors.fail(this.$messages.Devices.NotFoundDeviceByIdentifierErrorMessageWithIdentifier, emulatorImage, this.$staticConfig.CLIENT_NAME.toLowerCase());
122+
this.$errors.fail(`No emulator image available for device identifier '${emulatorImage}'.`);
123123
} else {
124124
this.$errors.fail(this.$messages.Devices.NotFoundDeviceByIdentifierErrorMessage, this.$staticConfig.CLIENT_NAME.toLowerCase());
125125
}

mobile/mobile-core/devices-service.ts

+30-3
Original file line numberDiff line numberDiff line change
@@ -392,19 +392,34 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
392392
}
393393
let deviceInstances = this.getDeviceInstances();
394394

395-
//if no --device is passed and no devices are found, the default emulator is started
396395
if (!data.deviceId && _.isEmpty(deviceInstances)) {
397396
if (!this.$hostInfo.isDarwin && this.$mobileHelper.isiOSPlatform(data.platform)) {
398397
this.$errors.failWithoutHelp(constants.ERROR_NO_DEVICES_CANT_USE_IOS_SIMULATOR);
399398
}
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+
}
410+
411+
private async _startEmulatorIfNecessary(data?: Mobile.IDevicesServicesInitializationOptions): Promise<void> {
412+
const deviceInstances = this.getDeviceInstances();
400413

414+
//if no --device is passed and no devices are found, the default emulator is started
415+
if (!data.deviceId && _.isEmpty(deviceInstances)) {
401416
return await this.startEmulator(data.platform);
402417
}
403418

404419
//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)
405420
if (data.deviceId) {
406421
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; });
408423
if (!activeDeviceInstance) {
409424
return await this.startEmulator(data.platform, data.deviceId);
410425
}
@@ -418,7 +433,6 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
418433
return await this.startEmulator(data.platform);
419434
}
420435
}
421-
}
422436
}
423437

424438
/**
@@ -672,6 +686,19 @@ export class DevicesService extends EventEmitter implements Mobile.IDevicesServi
672686
shouldReturnImmediateResult = shouldReturnImmediateResult || false;
673687
return { shouldReturnImmediateResult: shouldReturnImmediateResult, platform: platform };
674688
}
689+
690+
private getEmulatorError(error: Error, platform: string): string {
691+
let emulatorName = constants.DeviceTypes.Emulator;
692+
693+
if (this.$mobileHelper.isiOSPlatform(platform)) {
694+
emulatorName = constants.DeviceTypes.Simulator;
695+
}
696+
697+
return `Cannot find connected devices.${EOL}` +
698+
`${emulatorName} start failed with: ${error.message}${EOL}` +
699+
`To list currently connected devices and verify that the specified identifier exists, run '${this.$staticConfig.CLIENT_NAME.toLowerCase()} device'.${EOL}` +
700+
`To list available ${emulatorName.toLowerCase()} images, run '${this.$staticConfig.CLIENT_NAME.toLowerCase()} device <Platform> --available-devices'.`;
701+
}
675702
}
676703

677704
$injector.register("devicesService", DevicesService);

0 commit comments

Comments
 (0)