From f4ee96f22cdf53cecdf059ca677fe43512cc64fc Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Tue, 7 Sep 2021 17:24:34 +0200 Subject: [PATCH 1/2] feat: improved --json output from devices --- lib/common/commands/device/list-devices.ts | 26 ++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/common/commands/device/list-devices.ts b/lib/common/commands/device/list-devices.ts index 951e2be9f3..ac60b5f3db 100644 --- a/lib/common/commands/device/list-devices.ts +++ b/lib/common/commands/device/list-devices.ts @@ -20,6 +20,13 @@ export class ListDevicesCommand implements ICommand { public allowedParameters = [this.$stringParameter]; public async execute(args: string[]): Promise { + const devices: { + available?: any[]; + devices: any[]; + } = { + devices: [], + }; + if (this.$options.availableDevices) { const platform = this.$mobileHelper.normalizePlatformName(args[0]); if (!platform && args[0]) { @@ -38,10 +45,17 @@ export class ListDevicesCommand implements ICommand { const emulators = this.$emulatorHelper.getEmulatorsFromAvailableEmulatorsOutput( availableEmulatorsOutput ); - this.printEmulators("\nAvailable emulators", emulators); + devices.available = emulators; + + if (!this.$options.json) { + this.printEmulators("\nAvailable emulators", emulators); + } + } + + if (!this.$options.json) { + this.$logger.info("\nConnected devices & emulators"); } - this.$logger.info("\nConnected devices & emulators"); let index = 1; await this.$devicesService.initialize({ platform: args[0], @@ -67,7 +81,7 @@ export class ListDevicesCommand implements ICommand { let action: (_device: Mobile.IDevice) => Promise; if (this.$options.json) { action = async (device) => { - this.$logger.info(JSON.stringify(device.deviceInfo)); + devices.devices.push(device.deviceInfo); }; } else { action = async (device) => { @@ -89,7 +103,11 @@ export class ListDevicesCommand implements ICommand { allowNoDevices: true, }); - if (!this.$options.json && table.length) { + if (this.$options.json) { + return this.$logger.info(JSON.stringify(devices, null, 2)); + } + + if (table.length) { this.$logger.info(table.toString()); } } From 0289546122c410a22fb20e434aba5ff0f1d0aa8a Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Tue, 15 Feb 2022 14:43:31 +0100 Subject: [PATCH 2/2] fix: hide log when using --json --- lib/common/mobile/mobile-core/devices-service.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/common/mobile/mobile-core/devices-service.ts b/lib/common/mobile/mobile-core/devices-service.ts index 05970600a7..b16f5b73e1 100644 --- a/lib/common/mobile/mobile-core/devices-service.ts +++ b/lib/common/mobile/mobile-core/devices-service.ts @@ -882,7 +882,9 @@ export class DevicesService return; } - this.$logger.info("Searching for devices..."); + if (!this.$options.json) { + this.$logger.info("Searching for devices..."); + } deviceInitOpts = deviceInitOpts || {}; this._data = deviceInitOpts;