forked from bertspaan/node-diff-utility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (40 loc) · 1.07 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
var H = require('highland');
var events = require('events');
var split = require('split2');
var diff2js = require('./diff2js');
var spawn = require('child_process').spawn;
function callS(diffTool, f1, f2) {
var cmd = spawn(diffTool, [f1, f2]);
return H(H(cmd.stdout)
.splitBy('\n')
.pipe(diff2js()));
}
function call(diffTool, f1, f2, opts) {
// returns a stream when asked to do so
if (opts && opts.stream) {
return callS(diffTool, f1, f2, opts);
}
var diff = new events.EventEmitter();
var command = spawn(diffTool, [f1, f2]);
command.stdout
.pipe(split())
.pipe(diff2js())
.on('data', function(line) {
diff.emit('diff', line);
})
.on('end', function() {
diff.emit('end');
});
command.on('stderr', function(error) {
diff.emit('error', error);
});
command.on('exit', function(code) {
if (code == 2) {
// diff error:
// http://stackoverflow.com/questions/6971284/what-are-the-error-exit-values-for-diff
diff.emit('error', 'diff error');
}
});
return diff;
}
module.exports = call;