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

Commit b1abe55

Browse files
author
Kristian D. Dimitrov
committed
Fix simulator started on run with fake device id.
1 parent 359f750 commit b1abe55

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

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

+28-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);
400405

406+
this.$errors.failWithoutHelp(errorMessage);
407+
}
408+
}
409+
}
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)) {
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,17 @@ 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 = "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+
}
675700
}
676701

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

0 commit comments

Comments
 (0)