-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
executable file
·51 lines (43 loc) · 1.26 KB
/
index.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
#!/usr/bin/env node
// @ts-check
'use strict';
const
path = require('path'),
fs = require('fs'),
util = require('util'),
spawn = require('child_process').spawn,
exec = require('child_process').exec,
program = require('commander');
// set program info
program
.version('0.0.7')
.usage('[options] [<commit>] [--] [<path>...]')
.option('--cached', 'show diff of staging files')
.parse(process.argv);
// judge options
const options = ['--patience'];
if (program.cached) {
options.push('--cached');
}
// init output file
const realPath = fs.realpathSync(__dirname);
const output = fs.createWriteStream(`${realPath}/dest/diff.js`)
output.write('var lineDiffExample="');
// git diff
const giff = spawn('git', ['diff'].concat(program.args).concat(options));
giff.stdout.on('data', function (data) {
// encode to JSON to get escaping
const encoded = JSON.stringify(data.toString());
// extract the encoded string's contents
const contents = encoded.slice(1, -1);
output.write(contents);
});
giff.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
giff.on('exit', function (code) {
output.end('";', function () {
console.log(`${realPath}/index.html`);
exec(`which open && open ${realPath}/index.html`);
});
});