exec.js
14 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// grunt-exec
// ==========
// * GitHub: https://github.com/jharding/grunt-exec
// * Original Copyright (c) 2012 Jake Harding
// * Copyright (c) 2017 grunt-exec
// * Licensed under the MIT license.
// grunt-exe 2.0.0+ simulates the convenience of child_process.exec with the capabilities of child_process.spawn
// this was done primarily to preserve colored output from applications such as npm
// a lot of work was done to simulate the original behavior of both child_process.exec and grunt-exec
// as such there may be unintended consequences so the major revision was bumped
// a breaking change was made to the 'maxBuffer kill process' scenario so it is treated as an error and provides more detail (--verbose)
// stdout and stderr buffering & maxBuffer constraints are removed entirely where possible
// new features: detached (boolean), argv0 (override the executable name passed to the application), shell (boolean or string)
// fd #s greater than 2 not yet supported (ipc piping) which is spawn-specific and very rarely required
// TODO: support stdout and stderr Buffer objects passed in
// TODO: stdin/stdout/stderr string as file name => open the file and read/write from it
module.exports = function(grunt) {
var cp = require('child_process')
, f = require('util').format
, _ = grunt.util._
, log = grunt.log
, verbose = grunt.verbose;
grunt.registerMultiTask('exec', 'Execute shell commands.', function() {
var callbackErrors = false;
var defaultOut = log.write;
var defaultError = log.error;
var defaultCallback = function(err, stdout, stderr) {
if (err) {
callbackErrors = true;
defaultError('Error executing child process: ' + err.toString());
}
};
var data = this.data
, execOptions = data.options !== undefined ? data.options : {}
, stdout = data.stdout !== undefined ? data.stdout : true
, stderr = data.stderr !== undefined ? data.stderr : true
, stdin = data.stdin !== undefined ? data.stdin : false
, stdio = data.stdio
, callback = _.isFunction(data.callback) ? data.callback : defaultCallback
, callbackArgs = data.callbackArgs !== undefined ? data.callbackArgs : []
, sync = data.sync !== undefined ? data.sync : false
, exitCodes = data.exitCode || data.exitCodes || 0
, command
, childProcess
, args = [].slice.call(arguments, 0)
, done = this.async();
// https://github.com/jharding/grunt-exec/pull/30
exitCodes = _.isArray(exitCodes) ? exitCodes : [exitCodes];
// allow for command to be specified in either
// 'command' or 'cmd' property, or as a string.
command = data.command || data.cmd || (_.isString(data) && data);
if (!command) {
defaultError('Missing command property.');
return done(false);
}
if (data.cwd && _.isFunction(data.cwd)) {
execOptions.cwd = data.cwd.apply(grunt, args);
} else if (data.cwd) {
execOptions.cwd = data.cwd;
}
// default to current process cwd
execOptions.cwd = execOptions.cwd || process.cwd();
// manually supported (spawn vs exec)
// 200*1024 is default maxBuffer of child_process.exec
// NOTE: must be < require('buffer').kMaxLength or a RangeError will be triggered
var maxBuffer = data.maxBuffer || execOptions.maxBuffer || (200*1024);
// timeout manually supportted (spawn vs exec)
execOptions.timeout = execOptions.timeout || data.timeout || 0;
// kill signal manually supportted (spawn vs exec)
execOptions.killSignal = execOptions.killSignal || data.killSignal || 'SIGTERM';
// support shell scripts like 'npm.cmd' by default (spawn vs exec)
var shell = (typeof data.shell === 'undefined') ? execOptions.shell : data.shell;
execOptions.shell = (typeof shell === 'string') ? shell : (shell === false ? false : true);
// kept in data.encoding in case it is set to 'buffer' for final callback
data.encoding = data.encoding || execOptions.encoding || 'utf8';
stdio = stdio || execOptions.stdio || undefined;
if (stdio === 'inherit') {
stdout = 'inherit';
stderr = 'inherit';
stdin = 'inherit';
} else if (stdio === 'pipe') {
stdout = 'pipe';
stderr = 'pipe';
stdin = 'pipe';
} else if (stdio === 'ignore') {
stdout = 'ignore';
stderr = 'ignore';
stdin = 'ignore';
}
if (_.isFunction(command)) {
command = command.apply(grunt, args);
}
if (!_.isString(command)) {
defaultError('Command property must be a string.');
return done(false);
}
verbose.subhead(command);
// manually parse args into array (spawn vs exec)
var splitArgs = function(command) {
// Regex Explanation Regex
// ---------------------------------------------------------------------
// 0-* spaces \s*
// followed by either:
// [NOT: a space, half quote, or double quote] 1-* times [^\s'"]+
// followed by either:
// [half quote or double quote] in the future (?=['"])
// or 1-* spaces \s+
// or end of string $
// or half quote [']
// followed by 0-*:
// [NOT: a backslash, or half quote] [^\\']
// or a backslash followed by any character \\.
// followed by a half quote [']
// or double quote ["]
// followed by 0-*:
// [NOT: a backslash, or double quote] [^\\"]
// or a backslash followed by any character \\.
// followed by a double quote ["]
// or end of string $
var pieces = command.match(/\s*([^\s'"]+(?:(?=['"])|\s+|$)|(?:(?:['](?:([^\\']|\\.)*)['])|(?:["](?:([^\\"]|\\.)*)["]))|$)/g);
var args = [];
var next = false;
for (var i = 0; i < pieces.length; i++) {
var piece = pieces[i];
if (piece.length > 0) {
if (next || args.length === 0 || piece.charAt(0) === ' ') {
args.push(piece.trim());
} else {
var last = args.length - 1;
args[last] = args[last] + piece.trim();
}
next = piece.endsWith(' ');
}
}
// NodeJS on Windows does not have this issue
if (process.platform !== 'win32') {
args = [args.join(' ')];
}
return args;
};
var args = splitArgs(command);
command = args[0];
if (args.length > 1) {
args = args.slice(1);
} else {
args = [];
}
// only save stdout and stderr if a custom callback is used
var bufferedOutput = callback !== defaultCallback;
// different stdio behavior (spawn vs exec)
var stdioOption = function(value, integerValue, inheritValue) {
return value === integerValue ? integerValue
: value === 'inherit' ? inheritValue
: bufferedOutput ? 'pipe' : value === 'pipe' || value === true || value === null || value === undefined ? 'pipe'
: 'ignore'; /* value === false || value === 'ignore' */
}
execOptions.stdio = [
stdioOption(stdin, 0, process.stdin),
stdioOption(stdout, 1, process.stdout),
stdioOption(stderr, 2, process.stderr)
];
var encoding = data.encoding;
var bufferedStdOut = bufferedOutput && execOptions.stdio[1] === 'pipe';
var bufferedStdErr = bufferedOutput && execOptions.stdio[2] === 'pipe';
var stdOutLength = 0;
var stdErrLength = 0;
var stdOutBuffers = [];
var stdErrBuffers = [];
if (bufferedOutput && !Buffer.isEncoding(encoding)) {
if (encoding === 'buffer') {
encoding = 'binary';
} else {
grunt.fail.fail('Encoding "' + encoding + '" is not a supported character encoding!');
done(false);
}
}
if (verbose) {
stdioDescriptions = execOptions.stdio.slice();
for (var i = 0; i < stdioDescriptions.length; i++) {
stdioDescription = stdioDescriptions[i];
if (stdioDescription === process.stdin) {
stdioDescriptions[i] = 'process.stdin';
} else if (stdioDescription === process.stdout) {
stdioDescriptions[i] = 'process.stdout';
} else if (stdioDescription === process.stderr) {
stdioDescriptions[i] = 'process.stderr';
}
}
verbose.writeln('buffer : ' + (bufferedOutput ?
(bufferedStdOut ? 'stdout=enabled' : 'stdout=disabled')
+ ';' +
(bufferedStdErr ? 'stderr=enabled' : 'stderr=disabled')
+ ';' +
'max size=' + maxBuffer
: 'disabled'));
verbose.writeln('timeout : ' + (execOptions.timeout === 0 ? 'infinite' : '' + execOptions.timeout + 'ms'));
verbose.writeln('killSig : ' + execOptions.killSignal);
verbose.writeln('shell : ' + execOptions.shell);
verbose.writeln('command : ' + command);
verbose.writeln('args : [' + args.join(',') + ']');
verbose.writeln('stdio : [' + stdioDescriptions.join(',') + ']');
verbose.writeln('cwd : ' + execOptions.cwd);
//verbose.writeln('env path : ' + process.env.PATH);
verbose.writeln('exitcodes:', exitCodes.join(','));
}
if (sync)
{
childProcess = cp.spawnSync(command, args, execOptions);
}
else {
childProcess = cp.spawn(command, args, execOptions);
}
if (verbose) {
verbose.writeln('pid : ' + childProcess.pid);
}
var killChild = function (reason) {
defaultError(reason);
process.kill(childProcess.pid, execOptions.killSignal);
//childProcess.kill(execOptions.killSignal);
done(false); // unlike exec, this will indicate an error - after all, it did kill the process
};
if (execOptions.timeout !== 0) {
var timeoutProcess = function() {
killChild('Timeout child process');
};
setInterval(timeoutProcess, execOptions.timeout);
}
var writeStdOutBuffer = function(d) {
var b = !Buffer.isBuffer(d) ? new Buffer(d.toString(encoding)) : d;
if (stdOutLength + b.length > maxBuffer) {
if (verbose) {
verbose.writeln("EXCEEDING MAX BUFFER: stdOut " + stdOutLength + " buffer " + b.length + " maxBuffer " + maxBuffer);
}
killChild("stdout maxBuffer exceeded");
} else {
stdOutLength += b.length;
stdOutBuffers.push(b);
}
// default piping behavior
if (stdout !== false && data.encoding !== 'buffer') {
defaultOut(d);
}
};
var writeStdErrBuffer = function(d) {
var b = !Buffer.isBuffer(d) ? new Buffer(d.toString(encoding)) : d;
if (stdErrLength + b.length > maxBuffer) {
if (verbose) {
verbose.writeln("EXCEEDING MAX BUFFER: stdErr " + stdErrLength + " buffer " + b.length + " maxBuffer " + maxBuffer);
}
killChild("stderr maxBuffer exceeded");
} else {
stdErrLength += b.length;
stdErrBuffers.push(b);
}
// default piping behavior
if (stderr !== false && data.encoding !== 'buffer') {
defaultError(d);
}
};
if (execOptions.stdio[1] === 'pipe') {
var pipeOut = bufferedStdOut ? writeStdOutBuffer : defaultOut;
// Asynchronous + Synchronous Support
if (sync) { pipeOut(childProcess.stdout); }
else { childProcess.stdout.on('data', function (d) { pipeOut(d); }); }
}
if (execOptions.stdio[2] === 'pipe') {
var pipeErr = bufferedStdErr ? writeStdErrBuffer : defaultError;
// Asynchronous + Synchronous Support
if (sync) { pipeOut(childProcess.stderr); }
else { childProcess.stderr.on('data', function (d) { pipeErr(d); }); }
}
// Catches failing to execute the command at all (eg spawn ENOENT),
// since in that case an 'exit' event will not be emitted.
// Asynchronous + Synchronous Support
if (sync) {
if (childProcess.error != null)
{
defaultError(f('Failed with: %s', error.message));
done(false);
}
}
else {
childProcess.on('error', function (err) {
defaultError(f('Failed with: %s', err));
done(false);
});
}
// Exit Function (used for process exit callback / exit function)
var exitFunc = function (code) {
if (callbackErrors) {
defaultError('Node returned an error for this child process');
return done(false);
}
var stdOutBuffer = undefined;
var stdErrBuffer = undefined;
if (bufferedStdOut) {
stdOutBuffer = new Buffer(stdOutLength);
var offset = 0;
for (var i = 0; i < stdOutBuffers.length; i++) {
var buf = stdOutBuffers[i];
buf.copy(stdOutBuffer, offset);
offset += buf.length;
}
if (data.encoding !== 'buffer') {
stdOutBuffer = stdOutBuffer.toString(encoding);
}
}
if (bufferedStdErr) {
stdErrBuffer = new Buffer(stdErrLength);
var offset = 0;
for (var i = 0; i < stdErrBuffers.length; i++) {
var buf = stdErrBuffers[i];
buf.copy(stdErrBuffer, offset);
offset += buf.length;
}
if (data.encoding !== 'buffer') {
stdErrBuffer = stdErrBuffer.toString(encoding);
}
}
if (exitCodes.indexOf(code) < 0) {
defaultError(f('Exited with code: %d.', code));
if (callback) {
var err = new Error(f('Process exited with code %d.', code));
err.code = code;
callback(err, stdOutBuffer, stdErrBuffer, callbackArgs);
}
return done(false);
}
verbose.ok(f('Exited with code: %d.', code));
if (callback) {
callback(null, stdOutBuffer, stdErrBuffer, callbackArgs);
}
done();
}
// Asynchronous + Synchronous Support
if (sync) {
exitFunc(childProcess.status);
}
else {
childProcess.on('exit', exitFunc);
}
});
};