Skip to content

feat: improved --json output from devices #5565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions lib/common/commands/device/list-devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ export class ListDevicesCommand implements ICommand {
public allowedParameters = [this.$stringParameter];

public async execute(args: string[]): Promise<void> {
const devices: {
available?: any[];
devices: any[];
} = {
devices: [],
};

if (this.$options.availableDevices) {
const platform = this.$mobileHelper.normalizePlatformName(args[0]);
if (!platform && args[0]) {
Expand All @@ -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],
Expand All @@ -67,7 +81,7 @@ export class ListDevicesCommand implements ICommand {
let action: (_device: Mobile.IDevice) => Promise<void>;
if (this.$options.json) {
action = async (device) => {
this.$logger.info(JSON.stringify(device.deviceInfo));
devices.devices.push(device.deviceInfo);
};
} else {
action = async (device) => {
Expand All @@ -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());
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/common/mobile/mobile-core/devices-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down