|
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(); |
30 | 86 | if (input) { |
31 | | - var content = getHtml(program, input); |
| 87 | + var content = getOutput(input); |
32 | 88 |
|
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); |
37 | 93 | } else { |
38 | | - console.log(content); |
| 94 | + print(content); |
39 | 95 | } |
40 | 96 | } 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(); |
43 | 99 | } |
44 | 100 |
|
45 | 101 | process.exit(0); |
46 | 102 | } |
47 | 103 |
|
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; |
50 | 122 |
|
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'); |
52 | 126 |
|
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 | +} |
54 | 140 |
|
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 | +} |
56 | 146 |
|
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 | +} |
58 | 154 |
|
59 | | - exec("open /tmp/diff.html"); |
| 155 | +function prepareJSON(content) { |
| 156 | + return JSON.stringify(content); |
60 | 157 | } |
61 | 158 |
|
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 | +} |
68 | 162 |
|
69 | | - return childProcess.execSync(lineDiffCommand).toString('utf8'); |
70 | | - } |
| 163 | +function error(msg) { |
| 164 | + console.error(msg); |
71 | 165 | } |
72 | 166 |
|
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 | +} |
75 | 171 |
|
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 | +} |
79 | 176 |
|
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'); |
87 | 180 | } |
0 commit comments