Format JSON output of cli commands to be human readable

This commit is contained in:
Richard Hua 2015-11-09 15:07:03 -08:00
Родитель 66bf731332
Коммит c61bc3b4c5
2 изменённых файлов: 45 добавлений и 15 удалений

Просмотреть файл

@ -558,7 +558,7 @@ function printDeploymentList(command: cli.IDeploymentListCommand, deployments: D
}
dataSource.push(strippedDeployment);
});
log(JSON.stringify(dataSource));
printJson(dataSource);
} else if (command.format === "table") {
var headers = ["Name", "Deployment Key", "Package Metadata"];
printTable(headers,
@ -582,6 +582,10 @@ function printDeploymentList(command: cli.IDeploymentListCommand, deployments: D
}
}
function printJson(object: any) {
log(JSON.stringify(object, /*replacer=*/ null, /*spacing=*/ 2));
}
function printList<T extends { id: string; name: string; }>(format: string, items: T[]): void {
if (format === "json") {
var dataSource: any[] = [];
@ -590,7 +594,7 @@ function printList<T extends { id: string; name: string; }>(format: string, item
dataSource.push({ "name": item.name, "id": item.id });
});
log(JSON.stringify(dataSource));
printJson(dataSource);
} else if (format === "table") {
printTable(["Name", "ID"], (dataSource: any[]): void => {
items.forEach((item: T): void => {
@ -602,13 +606,7 @@ function printList<T extends { id: string; name: string; }>(format: string, item
function printAccessKeys(format: string, keys: AccessKey[]): void {
if (format === "json") {
var dataSource: any[] = [];
keys.forEach((key: AccessKey): void => {
dataSource.push(key);
});
log(JSON.stringify(dataSource));
printJson(keys);
} else if (format === "table") {
printTable(["Key", "Time Created", "Created From", "Description"], (dataSource: any[]): void => {
keys.forEach((key: AccessKey): void => {

Просмотреть файл

@ -7,6 +7,11 @@ import * as cli from "../definitions/cli";
import * as cmdexec from "../script/command-executor";
import * as os from "os";
function assertJsonDescribesObject(json: string, object: Object) {
// Make sure JSON is indented correctly
assert.equal(json, JSON.stringify(object, /*replacer=*/ null, /*spacing=*/ 2));
}
export class SdkStub {
public addAccessKey(machine: string, description?: string): Promise<codePush.AccessKey> {
return Q(<codePush.AccessKey>{
@ -156,9 +161,17 @@ describe("CLI", () => {
assert.equal(log.args[0].length, 1);
var actual: string = log.args[0][0];
var expected = "[{\"id\":\"7\",\"name\":\"8\",\"createdTime\":0,\"createdBy\":\"" + os.hostname() + "\",\"description\":\"Test Description\"}]";
var expected = [
{
id: "7",
name: "8",
createdTime: 0,
createdBy: os.hostname(),
description: "Test Description"
}
];
assert.equal(actual, expected);
assertJsonDescribesObject(actual, expected);
done();
});
});
@ -240,9 +253,12 @@ describe("CLI", () => {
assert.equal(log.args[0].length, 1);
var actual: string = log.args[0][0];
var expected = "[{\"name\":\"a\",\"id\":\"1\"},{\"name\":\"b\",\"id\":\"2\"}]";
var expected = [
{ name: "a", id: "1" },
{ name: "b", id: "2" }
];
assert.equal(actual, expected);
assertJsonDescribesObject(actual, expected);
done();
});
});
@ -338,9 +354,25 @@ describe("CLI", () => {
assert.equal(log.args[0].length, 1);
var actual: string = log.args[0][0];
var expected = "[{\"name\":\"Production\",\"deploymentKey\":\"6\"},{\"name\":\"Staging\",\"deploymentKey\":\"6\",\"package\":{\"appVersion\":\"1.0.0\",\"isMandatory\":true,\"packageHash\":\"jkl\",\"uploadTime\":" + JSON.stringify(new Date(1000)) + ",\"description\":\"fgh\"}}]";
var expected = [
{
name: "Production",
deploymentKey: "6"
},
{
name: "Staging",
deploymentKey: "6",
package: {
appVersion: "1.0.0",
isMandatory: true,
packageHash: "jkl",
uploadTime: "1970-01-01T00:00:01.000Z",
description: "fgh"
}
}
];
assert.equal(actual, expected);
assertJsonDescribesObject(actual, expected);
done();
});
});