Skip to content

Commit ebd4263

Browse files
committed
use yargs to parse the cli arguments
* use latest yargs version to parse args * add command default when running * add more example commands * fix problem with passing arguments to diff (solved by --)
1 parent 314a65e commit ebd4263

File tree

3 files changed

+163
-69
lines changed

3 files changed

+163
-69
lines changed

dist/template.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
-->
1111

1212
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.6/styles/github.min.css">
13-
<link rel="stylesheet" type="text/css" href="{{css}}">
13+
<style>
14+
{{css}}
15+
</style>
1416

1517
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.6/highlight.min.js"></script>
1618
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.6/languages/scala.min.js"></script>

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "diff2html-cli",
3-
"version": "0.2.4-1",
3+
"version": "1.0.0-1",
44

55
"homepage": "https://www.github.com/rtfpessoa/diff2html-cli",
66
"description": "Fast Diff to colorized HTML",
@@ -37,7 +37,7 @@
3737
},
3838

3939
"engines": {
40-
"node": ">=0.12.2"
40+
"node": ">=0.12.0"
4141
},
4242

4343
"preferGlobal": "true",
@@ -53,9 +53,8 @@
5353
"main": "./src/main.js",
5454

5555
"dependencies": {
56-
"commander": "2.8.1",
56+
"yargs": "3.17.*",
5757
"extend": "3.0.0",
58-
"pkginfo": "0.3.0",
5958
"diff2html": "*"
6059
},
6160

src/main.js

Lines changed: 157 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,180 @@
1-
require('pkginfo')(module, 'version');
2-
3-
var fs = require('fs');
4-
var program = require('commander');
5-
var appVersion = module.exports.version;
6-
7-
program.version(appVersion);
8-
program.usage('[git-diff options]');
9-
10-
program
11-
.option('-i, --input [file]', 'Diff input file.')
12-
.option('-o, --output [file]', 'Output to file path. Defaults to stdout.')
13-
.option('-p, --preview', 'Open preview in the browser.')
14-
.option('-l, --line', 'Line by Line diff.')
15-
.option('-s, --side', 'Side by Side diff.')
16-
.option('-w, --word', 'Word by Word highlight.')
17-
.option('-c, --char', 'Char by Char highlight.')
18-
.option('-j, --json', 'Export diff in json format.');
19-
20-
program.on('--help', function () {
21-
console.log('For support, check out https://github.com/rtfpessoa/diff2html-nodejs');
22-
});
23-
24-
program.parse(process.argv);
25-
26-
main(program);
27-
28-
function main(program) {
29-
var input = getInput(program);
1+
/*
2+
*
3+
* diff2html CLI (main.js)
4+
* Author: rtfpessoa
5+
*
6+
*/
7+
8+
var argv = require('yargs')
9+
.usage('Usage: diff2html [options]')
10+
.options({
11+
's': {
12+
alias: 'style',
13+
describe: 'Output style',
14+
nargs: 1,
15+
type: 'string',
16+
choices: ['line', 'side'],
17+
default: 'line'
18+
}
19+
})
20+
.options({
21+
'f': {
22+
alias: 'format',
23+
describe: 'Output format',
24+
nargs: 1,
25+
type: 'string',
26+
choices: ['html', 'json'],
27+
default: 'html'
28+
}
29+
})
30+
.options({
31+
'd': {
32+
alias: 'diff',
33+
describe: 'Diff style',
34+
nargs: 1,
35+
type: 'string',
36+
choices: ['word', 'char'],
37+
default: 'word'
38+
}
39+
})
40+
.options({
41+
'i': {
42+
alias: 'input',
43+
describe: 'Diff input source',
44+
nargs: 1,
45+
type: 'string',
46+
choices: ['file', 'command'],
47+
default: 'command'
48+
}
49+
})
50+
.options({
51+
'o': {
52+
alias: 'output',
53+
describe: 'Output destination',
54+
nargs: 1,
55+
type: 'string',
56+
choices: ['preview', 'stdout'],
57+
default: 'preview'
58+
}
59+
})
60+
.options({
61+
'F': {
62+
alias: 'file',
63+
describe: 'Send output to file (overrides output option)',
64+
nargs: 1,
65+
type: 'string'
66+
}
67+
})
68+
.example('diff2html -s line -f html -d word -i command -o preview -- -M HEAD~1',
69+
'diff last commit, line by line, word comparison between lines, previewed in the browser and input from git diff command')
70+
.example('diff2html -i file -- my-file-diff.diff', 'reading the input from a file')
71+
.example('diff2html -f json -o stdout -- -M HEAD~1', 'print json format to stdout')
72+
.example('diff2html -F my-pretty-diff.html -- -M HEAD~1', 'print to file')
73+
.version(function () {
74+
return require('../package').version;
75+
})
76+
.help('h')
77+
.alias('h', 'help')
78+
.epilog('Copyright 2015\n' +
79+
'For support, check out https://github.com/rtfpessoa/diff2html-cli')
80+
.argv;
81+
82+
main();
83+
84+
function main() {
85+
var input = getInput();
3086
if (input) {
31-
var content = getHtml(program, input);
87+
var content = getOutput(input);
3288

33-
if (program.output) {
34-
fs.writeFileSync(program.output, content);
35-
} else if (!program.json && program.preview) {
36-
preview(content)
89+
if (argv.F) {
90+
writeFile(argv.F, content);
91+
} else if (argv.output == 'preview') {
92+
preview(content);
3793
} else {
38-
console.log(content);
94+
print(content);
3995
}
4096
} else {
41-
console.error("The diff is empty. Try another command.");
42-
program.help();
97+
error("The input is empty. Try again.");
98+
argv.help();
4399
}
44100

45101
process.exit(0);
46102
}
47103

48-
function preview(diffHTML) {
49-
var exec = require("child_process").exec;
104+
function getInput() {
105+
if (argv.input == 'file') {
106+
return readFile(argv._[0]);
107+
} else {
108+
var gitArgs;
109+
if (argv._.length && argv._[0]) {
110+
gitArgs = argv._.join(" ");
111+
} else {
112+
gitArgs = "-M HEAD~1"
113+
}
114+
115+
var diffCommand = 'git diff ' + gitArgs;
116+
return runCmd(diffCommand);
117+
}
118+
}
119+
120+
function getOutput(input) {
121+
var diff2Html = require('diff2html').Diff2Html;
50122

51-
var template = fs.readFileSync(__dirname + "/../dist/template.html", "utf8");
123+
var config = {};
124+
config.wordByWord = (argv.diff === 'word');
125+
config.charByChar = (argv.diff === 'char');
52126

53-
var cssDir = __dirname + "/../dist/diff2html.css";
127+
if (argv.format === 'html') {
128+
var htmlContent;
129+
if (argv.style === 'side') {
130+
htmlContent = diff2Html.getPrettySideBySideHtmlFromDiff(input, config);
131+
} else {
132+
htmlContent = diff2Html.getPrettyHtmlFromDiff(input, config);
133+
}
134+
return prepareHTML(htmlContent);
135+
} else {
136+
var jsonContent = diff2Html.getJsonFromDiff(input, config);
137+
return prepareJSON(jsonContent);
138+
}
139+
}
54140

55-
var template = template.replace("{{css}}", cssDir).replace("{{diff}}", diffHTML);
141+
function preview(content) {
142+
var filePath = "/tmp/diff." + argv.format;
143+
writeFile(filePath, content);
144+
runCmd("open " + filePath);
145+
}
56146

57-
fs.writeFileSync("/tmp/diff.html", template);
147+
function prepareHTML(content) {
148+
var template = readFile(__dirname + "/../dist/template.html", "utf8");
149+
var css = readFile(__dirname + "/../dist/diff2html.css", "utf8");
150+
return template
151+
.replace("{{css}}", css)
152+
.replace("{{diff}}", content);
153+
}
58154

59-
exec("open /tmp/diff.html");
155+
function prepareJSON(content) {
156+
return JSON.stringify(content);
60157
}
61158

62-
function getInput(program) {
63-
if (program.input) {
64-
return fs.readFileSync(program.input, "utf8");
65-
} else {
66-
var childProcess = require('child_process');
67-
var lineDiffCommand = 'git diff ' + program.args;
159+
function print(line) {
160+
console.log(line);
161+
}
68162

69-
return childProcess.execSync(lineDiffCommand).toString('utf8');
70-
}
163+
function error(msg) {
164+
console.error(msg);
71165
}
72166

73-
function getHtml(program, input) {
74-
var diff2Html = require('diff2html').Diff2Html;
167+
function readFile(filePath) {
168+
var fs = require('fs');
169+
return fs.readFileSync(filePath, "utf8");
170+
}
75171

76-
var config = {};
77-
config.wordByWord = program.word;
78-
config.charByChar = program.char;
172+
function writeFile(filePath, content) {
173+
var fs = require('fs');
174+
fs.writeFileSync(filePath, content);
175+
}
79176

80-
if (program.side) {
81-
return diff2Html.getPrettySideBySideHtmlFromDiff(input, config);
82-
} else if (program.json) {
83-
return JSON.stringify(diff2Html.getJsonFromDiff(input, config));
84-
} else {
85-
return diff2Html.getPrettyHtmlFromDiff(input, config);
86-
}
177+
function runCmd(cmd) {
178+
var childProcess = require('child_process');
179+
return childProcess.execSync(cmd).toString('utf8');
87180
}

0 commit comments

Comments
 (0)