-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.js
65 lines (47 loc) · 1.3 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var Receiver = require('./lib/Receiver');
var Spawn = require('./lib/Spawn');
var Stream = require('./lib/Stream');
util.inherits(MultiView, EventEmitter);
function MultiView(opts) {
if (!(this instanceof MultiView)) {
return new MultiView(opts);
}
EventEmitter.call(this);
this.opts = opts || {};
this.receiver = null;
this.streams = [];
}
MultiView.prototype.spawn = function(command, args, opts) {
checkReceiver(this);
if (!Array.isArray(args) && typeof args === 'object') {
opts = args;
args = [];
} else {
opts = opts || {};
}
var stdout = opts.stdout;
var name = opts.name || command + (args ? ' ' + args.join(' ') : '');
var stream;
if (!opts.silent) {
stream = opts.stream || this.stream(name, !!opts.silent);
}
return new Spawn(stream, command, args, name, stdout);
};
MultiView.prototype.stream = function(name, silent) {
checkReceiver(this);
var _this = this;
var stream = new Stream(this, name, silent);
this.streams.push(stream);
stream.on('exit', function(code) {
_this.emit('exit', stream, code);
});
return stream;
};
function checkReceiver(mv) {
if (mv.receiver === null) {
mv.receiver = new Receiver(mv.opts);
}
}
module.exports = MultiView;