Skip to content

Commit f3b02b6

Browse files
committed
add support for printing output as JSON for diff commands
1 parent f4a2cc5 commit f3b02b6

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/util/diff.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { flags } from "@oclif/command"
22
import chalk from "chalk"
33

4+
const JSON_INDENT = 2
5+
46
export enum DiffTypes {
57
ALL = 'all',
68
ADDED = 'added',
79
REMOVED = 'removed',
10+
JSON = 'json',
811
}
912

1013
export interface DiffList {
@@ -30,11 +33,24 @@ export const DIFF_FLAGS = {
3033
}),
3134
}
3235

36+
function stringify(object: object) {
37+
return JSON.stringify(
38+
object,
39+
(_key, value) => value instanceof Map ? Object.fromEntries(value) : value,
40+
JSON_INDENT,
41+
)
42+
}
43+
3344
export function printDiffList(
3445
diffs: DiffList[],
3546
type: DiffTypes,
3647
callback: (message?: string) => void,
3748
) {
49+
if (type === DiffTypes.JSON) {
50+
callback(stringify(diffs))
51+
return
52+
}
53+
3854
let showAdded = [DiffTypes.ALL, DiffTypes.ADDED].includes(type)
3955
let showRemoved = [DiffTypes.ALL, DiffTypes.REMOVED].includes(type)
4056

@@ -57,6 +73,11 @@ export function printDiffMap(
5773
type: DiffTypes,
5874
callback: (message?: string) => void,
5975
) {
76+
if (type === DiffTypes.JSON) {
77+
callback(stringify(diffs))
78+
return
79+
}
80+
6081
let showAddedAndModified = [DiffTypes.ALL, DiffTypes.ADDED].includes(type)
6182
let showRemoved = [DiffTypes.ALL, DiffTypes.REMOVED].includes(type)
6283

0 commit comments

Comments
 (0)