-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
71 lines (59 loc) · 1.55 KB
/
cli.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#! /usr/bin/env node
const { splitDiffStrings, splitPatch } = require(`./index`)
const cli = require(`commander`)
const { readFileSync } = require('fs')
cli
.version(`0.1.0`)
.usage(`[options] <file1> <file2>`)
.option('--color', "Force color output ON")
.option('--no-color', "Force color output OFF")
.option('--columns [columns]', "Specify width output override.")
.option('--show-large-hunks', "Include large patch sections (hunks) in the output. DANGER: if it's huge, it'll take a long time!")
.on(`--help`, () => {
console.log(``)
console.log(` Example:`)
console.log(``)
console.log(` $ splitdiff file1 file2`)
console.log(` $ git --no-pager diff file1 file2 | splitdiff [options]`)
console.log(``)
})
.parse(process.argv)
try {
if (cli.args.length < 2) {
process.stdin.setEncoding('utf8')
let data = ''
process.stdin.on('readable', () => {
const chunk = process.stdin.read()
if (chunk !== null) {
data += chunk
}
})
process.stdin.on('end', () => {
if(data !== '') {
let opts = {
showLargeHunks: cli.showLargeHunks || false
}
if(cli.columns){
opts.columns = cli.columns
}
console.log(splitPatch(data, opts))
}
})
}
else {
console.log(
splitDiffStrings(
cli.args[0] + `\n\n` + readFileSync(cli.args[0], { encoding: `utf8` }),
cli.args[1] + `\n\n` + readFileSync(cli.args[1], { encoding: `utf8` }),
{
diffType: `diffLines`,
lineNumbers: true,
lineOffset: 2
}
)
)
}
} catch (error) {
console.log(`splitdiff:`, error)
process.exit(1)
}