-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
196 lines (174 loc) · 5.69 KB
/
index.js
File metadata and controls
196 lines (174 loc) · 5.69 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var util = require('util');
var spawn = require('child_process').spawn;
var EventEmitter = require('events').EventEmitter;
var split2 = require('split2');
var through2 = require('through2');
/**
* Constructor function.
*/
var Proboscis = function() {
this.runCommand = this.runCommand.bind(this);
this.runConfiguredProcesses = this.runConfiguredProcesses.bind(this);
this.createEventStream = this.createEventStream.bind(this);
this.getChildren = this.getChildren.bind(this);
this.rawStream = through2();
this.rawStream.setMaxListeners(0);
this.eventStream = through2.obj();
this.eventStream.setMaxListeners(0);
this.processes = {};
this.processConfigs = {};
this.closeStreamWithLastProcess = true;
this.setMaxListeners(0);
};
util.inherits(Proboscis, EventEmitter);
// The hash of running child processes.
Proboscis.prototype.processes = {};
// The hash of running streams.
Proboscis.prototype.processStreams = {};
// The hash of process configurations.
Proboscis.prototype.processConfigs = {};
// The unified raw event stream of output (stdout and stderr) from all child
// processes.
Proboscis.prototype.rawStream = null;
// The unified event stream of all running subprocesses.
// Each message is a hash with message content, command, and stream.
Proboscis.prototype.eventStream = null;
// Whether to close the event stream with the final process.
Proboscis.prototype.closeStreamWithLastProcess = true;
/**
* Run a command as a child process.
*
* @param {string} name A unique name for this command.
* Useful for differentiating two instances of the same executable.
* @param {string} command The command to run as a subprocess.
* @param {Array} args Optional arguments to be passed to the command.
* @param {Object} options Optional hash passed through to child_process.
* @param {Function} done Optional callback to run when the child process exits.
*/
Proboscis.prototype.runCommand = function(name, command, args, done) {
// Convert args to an array so that it's easier to work with.
arguments = Array.prototype.slice.call(arguments, 0);
if (typeof arguments[arguments.length - 1] === 'function') {
done = arguments.pop();
}
name = arguments.shift();
var child = spawn.apply(null, arguments);
this.processes[name] = child;
this.addProcess(name, command, args);
// Add stdout and stderr to our unified raw stream.
child.stdout.pipe(this.rawStream);
child.stderr.pipe(this.rawStream);
// Bind for event processing for our done callback if we have any.
if (done) {
child.on('error', done);
child.on('exit', function(code) {
var error = null;
if (code !== 0 && code !== false) {
var string = 'Execution of command %s named %s exited with code %s';
error = new Error(util.format(string, command, name, code));
}
done(error);
});
}
child.stdout
.pipe(split2())
.pipe(this.createEventStream(name, command, 'stdout'))
.pipe(this.eventStream, {end: this.closeStreamWithLastProcess});
child.stderr
.pipe(split2())
.pipe(this.createEventStream(name, command, 'stderr'))
.pipe(this.eventStream, {end: this.closeStreamWithLastProcess});
};
/**
* Return the hash of running child processes.
*/
Proboscis.prototype.getChildren = function() {
return this.processes;
};
/**
* Return the currint process configurations.
*/
Proboscis.prototype.getConfig = function(name) {
if (name !== undefined) {
if (this.processConfigs.hasOwnProperty(name)) {
return this.processConfigs[name];
}
return null;
}
return this.processConfigs;
};
/**
* Add a process configuration.
*/
Proboscis.prototype.addProcess = function(name, command, args, autoStart) {
var start = autoStart || true;
this.processConfigs[name] = {
name: name,
command: command,
args: args,
start: start
};
};
/**
* Run all currently configured processes that are configured to start.
*/
Proboscis.prototype.runConfiguredProcesses = function() {
var i = null;
var config = null;
for (i in this.processConfigs) {
config = this.processConfigs[i];
if (config.start) {
this.runCommand(config.name, config.command, config.args);
}
}
};
/**
* Get a throughstream that wraps all data passed through.
*
* @param {string} name A unique name for this event stream.
* @param {string} command The name of the command.
* @param {string} streamName The name of this stream (e.g. stdout or stderr).
* @return {stream} A throughstream that wraps string input.
*/
Proboscis.prototype.createEventStream = function(name, command, streamName) {
var _this = this;
this.processStreams[name + ':' + streamName] = through2.obj(function(data, enc, cb) {
if (data == '') {
return cb();
}
data = {
name: name,
command: command,
message: data,
stream: streamName,
time: new Date().getTime()
};
this.push(data);
cb();
},
// Don't end our event stream until all of the child processes have exited.
function() {
delete _this.processStreams[name + ':' + streamName];
if (Object.keys(_this.processStreams).length === 0) {
this.emit('end');
}
if (!_this.processStreams[name + ':stdout'] && !_this.processStreams[name + ':stderr']) {
delete _this.processes[name];
_this.emitEndEvent(name);
}
});
return this.processStreams[name + ':' + streamName];
};
/**
* Emits the end event once there are no running child processes.
*
* @param {string} name The name of the process that has closed.
*/
Proboscis.prototype.emitEndEvent = function(name) {
this.emit('processClosed', name);
this.emit('processClosed:' + name, name);
if (Object.keys(this.processes).length === 0) {
this.emit('allProcessesClosed');
}
};
module.exports = Proboscis;