Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
179 lines (166 loc) · 3.7 KB
/
Copy pathmain.js
File metadata and controls
179 lines (166 loc) · 3.7 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env node
/*
*
* Diff to HTML CLI (main.js)
* Author: rtfpessoa
*
*/
var cli = require('./cli.js').Diff2HtmlInterface;
var log = require('./logger.js').Logger;
var utils = require('./utils.js').Utils;
var yargs = require('yargs');
var argv = yargs.usage('Usage: diff2html [options] -- [diff args]')
.options({
's': {
alias: 'style',
describe: 'Output style',
nargs: 1,
type: 'string',
choices: ['line', 'side'],
default: 'line'
}
})
.options({
'su': {
alias: 'summary',
describe: 'Show files summary',
type: 'string',
choices: ['closed', 'open', 'hidden'],
default: 'closed'
}
})
.options({
'lm': {
alias: 'matching',
describe: 'Diff line matching type',
nargs: 1,
type: 'string',
choices: ['lines', 'words', 'none'],
default: 'words'
}
})
.options({
'lmt': {
alias: 'matchWordsThreshold',
describe: 'Diff line matching word threshold',
nargs: 1,
type: 'string',
default: '0.25'
}
})
.options({
'lmm': {
alias: 'matchingMaxComparisons',
describe: 'Diff line matching maximum line comparisons of a block of changes',
nargs: 1,
type: 'string',
default: '2500'
}
})
.options({
'f': {
alias: 'format',
describe: 'Output format',
nargs: 1,
type: 'string',
choices: ['html', 'json'],
default: 'html'
}
})
.options({
'd': {
alias: 'diff',
describe: 'Diff style',
nargs: 1,
type: 'string',
choices: ['word', 'char'],
default: 'word'
}
})
.options({
'i': {
alias: 'input',
describe: 'Diff input source',
nargs: 1,
type: 'string',
choices: ['file', 'command', 'stdin'],
default: 'command'
}
})
.options({
'o': {
alias: 'output',
describe: 'Output destination',
nargs: 1,
type: 'string',
choices: ['preview', 'stdout'],
default: 'preview'
}
})
.options({
'u': {
alias: 'diffy',
describe: 'Upload to diffy.org',
nargs: 1,
type: 'string',
choices: ['browser', 'pbcopy', 'print']
}
})
.options({
'F': {
alias: 'file',
describe: 'Send output to file (overrides output option)',
nargs: 1,
type: 'string'
}
})
.example('diff2html -s line -f html -d word -i command -o preview -- -M HEAD~1',
'diff last commit, line by line, word comparison between lines,' +
'previewed in the browser and input from git diff command')
.example('diff2html -i file -- my-file-diff.diff', 'reading the input from a file')
.example('diff2html -f json -o stdout -- -M HEAD~1', 'print json format to stdout')
.example('diff2html -F my-pretty-diff.html -- -M HEAD~1', 'print to file')
.version(function() {
return require('../package').version;
})
.help('h')
.alias('h', 'help')
.epilog('© 2014-2016 rtfpessoa\n' +
'For support, check out https://github.com/rtfpessoa/diff2html-cli')
.argv;
/*
* CLI code
*/
function noop(ignore) {
return ignore;
}
function onInput(err, input) {
if (err) {
log.error(err);
return;
}
if (!input) {
log.error('The input is empty. Try again.');
yargs.showHelp('log');
return;
}
if (argv.diffy) {
cli.postToDiffy(input, argv.diffy, noop);
return;
}
cli.getOutput(argv, input, onOutput);
}
function onOutput(err, output) {
if (err) {
log.error(err);
return;
}
if (argv.file) {
utils.writeFile(argv.file, output);
} else if (argv.output === 'preview') {
cli.preview(output, argv.format);
} else {
log.print(output);
}
}
cli.getInput(argv.input, argv._, onInput);