-
Notifications
You must be signed in to change notification settings - Fork 1
/
diff.ts
53 lines (49 loc) · 1.91 KB
/
diff.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
52
53
import * as json from '../services/utils'
import { readFile } from '../services/utils'
// import BaseCommand from '../base/base-command'
import ui from '../services/ui'
import { Command, flags } from '@oclif/command'
import runConfig from '../services/run-config'
import semanticDiff from '../services/semantic-diff'
import renderers from '../renderers'
export default class Diff extends Command {
static description = 'compute a diff between two json or yaml files, normalizing them first'
static flags = {
help: flags.help({char: 'h'}),
debug: flags.boolean({char: 'd', default: false, description: 'show debug information'}),
semantic: flags.boolean({char: 's', default: false, description: 'perform a swagger semantic diff'}),
format: flags.string({
char: 'f', default: 'json', options: Object.keys(renderers),
description: 'input files format'
}),
output: flags.string({
char: 'o', default: 'yaml', description: 'output format. recommended to use it with semantic option',
options: Object.keys(renderers)
}),
ignore: flags.string({char: 'i', multiple: true, description: 'ignore node', dependsOn: ['semantic']})
}
static args = [
{
name: 'file1',
required: true,
description: 'first file'
},
{
name: 'file2',
required: true,
description: 'second file'
}
]
async run() {
const {flags, args} = this.parse(this.ctor)
runConfig.debug = flags.debug
const inputRenderer = renderers[flags.format as keyof typeof renderers]
if (flags.semantic) {
const diff = semanticDiff.diff(await readFile(args.file1, inputRenderer), await readFile(args.file2, inputRenderer), flags.ignore)
const outputRenderer = renderers[flags.output as keyof typeof renderers]
ui.print(outputRenderer.marshal(diff))
} else {
ui.printPatch(await json.computePatch(args.file1, args.file2, inputRenderer))
}
}
}