-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
52 lines (36 loc) · 858 Bytes
/
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
52
var Stream = require('stream')
var spawn = require('child_process').spawn
function execStream (cmd, args, options) {
var AB = new Stream.Duplex
var A = new Stream.PassThrough
var B = new Stream.PassThrough
// console.log = function () {}
AB._write = function (chunk, encoding, cb) {
return A.write(chunk, encoding, cb)
}
AB.on('finish', function () {
A.end()
})
AB._read = function (n) {
}
B.on('readable', function() {
AB.push(B.read())
})
B.on('end', function () {
AB.push(null)
})
B.on('error', function(err) {
AB.emit('error', err)
})
A.on('error', function(err) {
AB.emit('error', err)
})
var proc = spawn(cmd, args, options)
A.pipe(proc.stdin)
proc.stdout.pipe(B)
proc.on('error', function(err) {
AB.emit('error', err)
})
return AB
}
module.exports = execStream