This repository was archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint.ts
51 lines (42 loc) · 1.5 KB
/
print.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import color from '@oclif/color'
import {YamlDiff} from './yaml'
import {stringify} from 'yaml'
interface logger {
log(message: string): void
}
const replacer = (key: string, value: unknown) => value === undefined ? null : value
export function printYamlDiff(self: logger, yamlDiff: YamlDiff): void {
for (const [objType, objs] of Object.entries(yamlDiff.added)) {
for (const [objName, obj] of Object.entries(objs)) {
stringifyYamlDiff(self, objType, objName, obj, line => {
return color.green('+ ' + line)
})
}
}
for (const [objType, objs] of Object.entries(yamlDiff.deleted)) {
for (const [objName, obj] of Object.entries(objs)) {
stringifyYamlDiff(self, objType, objName, obj, line => {
return color.red('- ' + line)
})
}
}
for (const [objType, objs] of Object.entries(yamlDiff.updated)) {
for (const [objName, obj] of Object.entries(objs)) {
stringifyYamlDiff(self, objType, objName, obj, line => {
return color.yellow('~ ' + line)
})
}
}
}
// eslint-disable-next-line max-params
function stringifyYamlDiff(self: logger, objType: string, objName: string, data: unknown, printer: (line: string) => string): void {
self.log(color.cyan(`@@ ${objType}:${objName} @@`))
const yamlRoleLines = stringify(data, replacer).split('\n')
const printRole = yamlRoleLines.map((line, i) => {
if (i === yamlRoleLines.length - 1) {
return ' ' + line
}
return printer(line)
}).join('\n')
self.log(printRole)
}