Skip to content

Commit 8446795

Browse files
authored
feat: improved --json output from devices (#5565)
BREAKING CHANGES: `ns devices --json` now prints a JSON object containing a devices key with an array of all devices. `ns devices --available-devices --json` now properly prints available devices as JSON. Migration steps: update how parsing of the output is handled to use the new schema ```ts { available?: any[]; devices: any[]; } ```
1 parent 81ed843 commit 8446795

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

lib/common/commands/device/list-devices.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ export class ListDevicesCommand implements ICommand {
2020
public allowedParameters = [this.$stringParameter];
2121

2222
public async execute(args: string[]): Promise<void> {
23+
const devices: {
24+
available?: any[];
25+
devices: any[];
26+
} = {
27+
devices: [],
28+
};
29+
2330
if (this.$options.availableDevices) {
2431
const platform = this.$mobileHelper.normalizePlatformName(args[0]);
2532
if (!platform && args[0]) {
@@ -38,10 +45,17 @@ export class ListDevicesCommand implements ICommand {
3845
const emulators = this.$emulatorHelper.getEmulatorsFromAvailableEmulatorsOutput(
3946
availableEmulatorsOutput
4047
);
41-
this.printEmulators("\nAvailable emulators", emulators);
48+
devices.available = emulators;
49+
50+
if (!this.$options.json) {
51+
this.printEmulators("\nAvailable emulators", emulators);
52+
}
53+
}
54+
55+
if (!this.$options.json) {
56+
this.$logger.info("\nConnected devices & emulators");
4257
}
4358

44-
this.$logger.info("\nConnected devices & emulators");
4559
let index = 1;
4660
await this.$devicesService.initialize({
4761
platform: args[0],
@@ -67,7 +81,7 @@ export class ListDevicesCommand implements ICommand {
6781
let action: (_device: Mobile.IDevice) => Promise<void>;
6882
if (this.$options.json) {
6983
action = async (device) => {
70-
this.$logger.info(JSON.stringify(device.deviceInfo));
84+
devices.devices.push(device.deviceInfo);
7185
};
7286
} else {
7387
action = async (device) => {
@@ -89,7 +103,11 @@ export class ListDevicesCommand implements ICommand {
89103
allowNoDevices: true,
90104
});
91105

92-
if (!this.$options.json && table.length) {
106+
if (this.$options.json) {
107+
return this.$logger.info(JSON.stringify(devices, null, 2));
108+
}
109+
110+
if (table.length) {
93111
this.$logger.info(table.toString());
94112
}
95113
}

lib/common/mobile/mobile-core/devices-service.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,9 @@ export class DevicesService
882882
return;
883883
}
884884

885-
this.$logger.info("Searching for devices...");
885+
if (!this.$options.json) {
886+
this.$logger.info("Searching for devices...");
887+
}
886888

887889
deviceInitOpts = deviceInitOpts || {};
888890
this._data = deviceInitOpts;

0 commit comments

Comments
 (0)