/
command.js
265 lines (230 loc) · 7.02 KB
/
command.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
var EventEmitter = require("events").EventEmitter;
var ChildProcess = require("child_process");
var execute = require("exec-sync");
/**
* @class
* @param {String} expression Expression of the command to create.
*/
var Command = (function() {
var constructor = function(expression) {
var self = this,
running = false,
output,
errorOutput,
exitCode,
emitter = new EventEmitter(),
childProcess;
/**
* @param {Function} callback Callback to invoke when the command completes.
* @returns {ChildProcess}
*/
this.run = function(callback) {
running = true;
callback && (typeof(callback) == "function") && this.once("completed", callback);
if(typeof(callback) != "function") {
try {
var result = execute(expression, true);
exitCode = 0;
output = result.stdout;
errorOutput = result.stderr;
}
catch(error) {
exitCode = -1;
errorOutput = error.trace;
}
finally {
running = false;
emitter.emit("completed", self);
}
return this;
}
else {
childProcess = ChildProcess.exec(expression, function(error) {
running = false;
exitCode = error ? error.code : 0;
output = arguments[1] || "";
errorOutput = arguments[2] || "";
emitter.emit("completed", self);
});
return childProcess;
}
};
/**
* Returns the error output gotten after this command has run.
*
* @returns {String}
*/
this.getExpression = function() {
return expression;
};
/**
* Tells whether this command is running or not.
*
* @returns {boolean}
*/
this.isRunning = function() {
return running;
};
/**
* Returns the exit code gotten after this command has run.
*
* @returns {Integer}
*/
this.getExitCode = function() {
return exitCode;
};
/**
* Returns the regular output gotten after this command has run.
*
* @returns {String}
*/
this.getOutput = function() {
return output;
};
/**
* Returns the error output gotten after this command has run.
*
* @returns {String}
*/
this.getErrorOutput = function() {
return errorOutput;
};
/**
* Returns the child process responsible for running this command.
*
* @param {String} event Name of the event to invoke the callback for.
* @param {Function} callback Callback to invoke when the specified event happens.
* @see EventEmitter#once
*/
this.getChildProcess = function() {
return childProcess;
};
/**
* Adds a listener to this command to be called once the specified event happens.
*
* @param {String} event Name of the event to invoke the callback for.
* @param {Function} callback Callback to invoke when the specified event happens.
* @returns {Function} The listener actually added.
* @see EventEmitter#once
*/
this.once = function(event, callback) {
callback && emitter.once(event, callback);
return callback;
};
};
constructor.prototype.toString = function() {
return this.getExpression();
};
/**
* Shortcut for creating then running a command.
* The first argument is passed to the Command constructor whilst the remaining ones are used to call the instance's "run" method.
*
* @param {String} expression Expression of the command to run.
* @returns {Command} Resulting Command object.
* @see Command
*/
constructor.run = function(expression, callback) {
var command = new constructor(expression);
command.run.apply(command, Array.prototype.slice.call(arguments, 1));
return command;
};
/**
* Shortcut for creating then running a batch of commands.
* The first argument is passed to the Batch constructor whilst the remaining ones are used to call the instance's "run" method.
*
* @param {String[]|Command[]} expressions Expressions, commands or a mix of those two to be added to the batch initially.
* @returns {Batch} Resulting Batch object.
* @see Batch
*/
constructor.batch = function(expressions, callback) {
var batch = new Batch(expressions);
batch.run.apply(batch, Array.prototype.slice.call(arguments, 1));
return batch;
};
return constructor;
})();
/**
* @class Batch of commands.
*
* @param {String[]|Command[]} expressions Expressions, commands or a mix of those two to be added to this batch initially.
* @see Batch#add
* @see Command
*/
var Batch = (function() {
var constructor = function(expressions) {
var self = this,
commands = [],
emitter = new EventEmitter(),
listener = function(command) {
if(!commands.some(function(command) { return command.isRunning(); })) {
emitter.emit("completed", self);
}
};
/**
* Adds a command to this batch.
* @param {String|Command} expression Expression or command to add to this batch.
* @returns {Command} The command which was actually added.
*/
this.add = function(expression) {
var command = (expression.constructor == Command) ? expression : new Command(expression);
commands.push(command);
return command;
};
/**
* Runs this batch of commands.
*
* @param {Function} callback Callback to invoke when the batch completes.
* @returns {ChildProcess[]}
*/
this.run = function(callback) {
var children = [],
iterativeCallback = (arguments.length > 1) ? callback : undefined,
callback = iterativeCallback ? arguments[1] : callback;
callback && this.once("completed", callback);
commands.forEach(function(command) {
var child = command.run(iterativeCallback);
children.push(child);
// Call "listener" once the command has completed in order to check if the batch itslef has completed.
command.once("completed", listener);
});
return children;
};
/**
* Returns the commands of this batch.
*
* @returns {Command[]} Array that contains the commands of this batch.
*/
this.getCommands = function() {
return commands.splice();
};
/**
* Returns the commands of this batch currently running.
*
* @returns {Command[]}
*/
this.getRunningCommands = function() {
return commands.filter(function(command) { return command.isRunning(); });
};
/**
* Adds a listener to this batch to be called once the specified event happens.
*
* @param {String} event Name of the event to invoke the callback for.
* @param {Function} callback Callback to invoke when the specified event happens.
* @returns {Function} The listener actually added.
* @see EventEmitter#once
*/
this.once = function(event, callback) {
callback && emitter.once("completed", callback);
return callback;
};
// Initially add the commands provided to the constructor.
expressions.forEach(function(expression) {
self.add(expression);
});
};
return constructor;
})();
module.exports = {
"Command": Command,
"Batch": Batch
};