index.js
7.1 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Jake JavaScript build tool
* Copyright 2112 Matthew Eernisse (mde@fleegix.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
let util = require('util'); // Native Node util module
let spawn = require('child_process').spawn;
let EventEmitter = require('events').EventEmitter;
let logger = require('./logger');
let file = require('./file');
let Exec;
const _UUID_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
let parseArgs = function (argumentsObj) {
let args;
let arg;
let cmds;
let callback;
let opts = {
interactive: false,
printStdout: false,
printStderr: false,
breakOnError: true
};
args = Array.prototype.slice.call(argumentsObj);
cmds = args.shift();
// Arrayize if passed a single string command
if (typeof cmds == 'string') {
cmds = [cmds];
}
// Make a copy if it's an actual list
else {
cmds = cmds.slice();
}
// Get optional callback or opts
while((arg = args.shift())) {
if (typeof arg == 'function') {
callback = arg;
}
else if (typeof arg == 'object') {
opts = Object.assign(opts, arg);
}
}
// Backward-compat shim
if (typeof opts.stdout != 'undefined') {
opts.printStdout = opts.stdout;
delete opts.stdout;
}
if (typeof opts.stderr != 'undefined') {
opts.printStderr = opts.stderr;
delete opts.stderr;
}
return {
cmds: cmds,
opts: opts,
callback: callback
};
};
/**
@name jake
@namespace jake
*/
let utils = new (function () {
/**
@name jake.exec
@static
@function
@description Executes shell-commands asynchronously with an optional
final callback.
`
@param {String[]} cmds The list of shell-commands to execute
@param {Object} [opts]
@param {Boolean} [opts.printStdout=false] Print stdout from each command
@param {Boolean} [opts.printStderr=false] Print stderr from each command
@param {Boolean} [opts.breakOnError=true] Stop further execution on
the first error.
@param {Boolean} [opts.windowsVerbatimArguments=false] Don't translate
arguments on Windows.
@param {Function} [callback] Callback to run after executing the
commands
@example
let cmds = [
'echo "showing directories"'
, 'ls -al | grep ^d'
, 'echo "moving up a directory"'
, 'cd ../'
]
, callback = function () {
console.log('Finished running commands.');
}
jake.exec(cmds, {stdout: true}, callback);
*/
this.exec = function (a, b, c) {
let parsed = parseArgs(arguments);
let cmds = parsed.cmds;
let opts = parsed.opts;
let callback = parsed.callback;
let ex = new Exec(cmds, opts, callback);
ex.addListener('error', function (msg, code) {
if (opts.breakOnError) {
fail(msg, code);
}
});
ex.run();
return ex;
};
this.createExec = function (a, b, c) {
return new Exec(a, b, c);
};
// From Math.uuid.js, https://github.com/broofa/node-uuid
// Robert Kieffer (robert@broofa.com), MIT license
this.uuid = function (length, radix) {
var chars = _UUID_CHARS
, uuid = []
, r
, i;
radix = radix || chars.length;
if (length) {
// Compact form
i = -1;
while (++i < length) {
uuid[i] = chars[0 | Math.random()*radix];
}
} else {
// rfc4122, version 4 form
// rfc4122 requires these characters
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
uuid[14] = '4';
// Fill in random data. At i==19 set the high bits of clock sequence as
// per rfc4122, sec. 4.1.5
i = -1;
while (++i < 36) {
if (!uuid[i]) {
r = 0 | Math.random()*16;
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
}
}
}
return uuid.join('');
};
})();
Exec = function () {
let parsed = parseArgs(arguments);
let cmds = parsed.cmds;
let opts = parsed.opts;
let callback = parsed.callback;
this._cmds = cmds;
this._callback = callback;
this._config = opts;
};
util.inherits(Exec, EventEmitter);
Object.assign(Exec.prototype, new (function () {
let _run = function () {
let self = this;
let sh;
let cmd;
let args;
let next = this._cmds.shift();
let config = this._config;
let errData = '';
let shStdio;
let handleStdoutData = function (data) {
self.emit('stdout', data);
};
let handleStderrData = function (data) {
let d = data.toString();
self.emit('stderr', data);
// Accumulate the error-data so we can use it as the
// stack if the process exits with an error
errData += d;
};
// Keep running as long as there are commands in the array
if (next) {
let spawnOpts = {};
this.emit('cmdStart', next);
// Ganking part of Node's child_process.exec to get cmdline args parsed
if (process.platform == 'win32') {
cmd = 'cmd';
args = ['/c', next];
if (config.windowsVerbatimArguments) {
spawnOpts.windowsVerbatimArguments = true;
}
}
else {
cmd = '/bin/sh';
args = ['-c', next];
}
if (config.interactive) {
spawnOpts.stdio = 'inherit';
sh = spawn(cmd, args, spawnOpts);
}
else {
shStdio = [
process.stdin
];
if (config.printStdout) {
shStdio.push(process.stdout);
}
else {
shStdio.push('pipe');
}
if (config.printStderr) {
shStdio.push(process.stderr);
}
else {
shStdio.push('pipe');
}
spawnOpts.stdio = shStdio;
sh = spawn(cmd, args, spawnOpts);
if (!config.printStdout) {
sh.stdout.addListener('data', handleStdoutData);
}
if (!config.printStderr) {
sh.stderr.addListener('data', handleStderrData);
}
}
// Exit, handle err or run next
sh.on('exit', function (code) {
let msg;
if (code !== 0) {
msg = errData || 'Process exited with error.';
msg = msg.trim();
self.emit('error', msg, code);
}
if (code === 0 || !config.breakOnError) {
self.emit('cmdEnd', next);
setTimeout(function () { _run.call(self); }, 0);
}
});
}
else {
self.emit('end');
if (typeof self._callback == 'function') {
self._callback();
}
}
};
this.append = function (cmd) {
this._cmds.push(cmd);
};
this.run = function () {
_run.call(this);
};
})());
utils.Exec = Exec;
utils.file = file;
utils.logger = logger;
module.exports = utils;