Skip to content

Commit

Permalink
feat: improved --json output from devices (#5565)
Browse files Browse the repository at this point in the history
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[];
}
```
  • Loading branch information
rigor789 committed Feb 15, 2022
1 parent 81ed843 commit 8446795
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
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

0 comments on commit 8446795

Please sign in to comment.