-
Notifications
You must be signed in to change notification settings - Fork 24
/
log-diff.js
executable file
·50 lines (46 loc) · 1.47 KB
/
log-diff.js
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
#!/usr/bin/env node
const fs = require('fs')
const program = require('commander')
program
.version('0.1.0')
.usage('[options] -c <file1>,<file2>')
.option('-c, --compareFiles <files>', 'Compare two json log files', val =>
val.split(',')
)
.option(
'-i, --ignoreProps [properties]',
'Properties to ignore',
val => val.split(','),
[]
)
.parse(process.argv)
program.ignoreProps.length &&
console.log('Ignoring properties:', program.ignoreProps.join(', '))
program.compareFiles.length &&
console.log('Comparing files:', program.compareFiles.join(', '))
// parse JSON from each line in each file
const results = program.compareFiles.map(filename =>
fs
.readFileSync(filename, 'utf8') // read contents of each passed filename
.split('\n') // split each line
.map(str => str.trim()) // trim whitespace
.filter(str => str.length) // not empty
.filter(str => /^\{.*\}$/.test(str)) // starts with '{' and ends with '}'
.map(JSON.parse)
)
// diff json
const jsondiffpatch = require('jsondiffpatch').create({
propertyFilter: name => !program.ignoreProps.includes(name)
})
const formatters = require('jsondiffpatch/src/formatters')
const file1 = results[0]
const file2 = results[1]
const delta = jsondiffpatch.diff(file1, file2)
const output = formatters.console.format(delta)
if (output.length) {
process.stderr.write(output + '\n')
process.exit(1)
} else {
console.log(`${program.compareFiles.join(' and ')} are identitcal`)
process.exit(0)
}