forked from shelljs/shelljs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.js
88 lines (76 loc) · 2.59 KB
/
cmd.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
var common = require('./common');
var execa = require('execa');
var DEFAULT_MAXBUFFER_SIZE = 20 * 1024 * 1024;
var COMMAND_NOT_FOUND_ERROR_CODE = 127;
common.register('cmd', _cmd, {
cmdOptions: null,
globStart: 1,
canReceivePipe: true,
wrapOutput: true,
});
function commandNotFound(execaResult) {
if (process.platform === 'win32') {
var str = 'is not recognized as an internal or external command';
return execaResult.code && execaResult.stderr.includes(str);
} else {
return execaResult.code &&
execaResult.stdout === null && execaResult.stderr === null;
}
}
function _cmd(options, command, commandArgs, userOptions) {
if (!command) {
common.error('Must specify a non-empty string as a command');
}
// `options` will usually not have a value: it's added by our commandline flag
// parsing engine.
commandArgs = [].slice.call(arguments, 2);
// `userOptions` may or may not be provided. We need to check the last
// argument. If it's an object, assume it's meant to be passed as
// userOptions (since ShellStrings are already flattened to strings).
if (commandArgs.length === 0) {
userOptions = {};
} else {
var lastArg = commandArgs.pop();
if (common.isObject(lastArg)) {
userOptions = lastArg;
} else {
userOptions = {};
commandArgs.push(lastArg);
}
}
var pipe = common.readFromPipe();
// Some of our defaults differ from execa's defaults. These can be overridden
// by the user.
var defaultOptions = {
maxBuffer: DEFAULT_MAXBUFFER_SIZE,
stripEof: false, // Preserve trailing newlines for consistency with unix.
reject: false, // Use ShellJS's error handling system.
};
// For other options, we forbid the user from overriding them (either for
// correctness or security).
var requiredOptions = {
input: pipe,
shell: false,
};
var execaOptions =
Object.assign(defaultOptions, userOptions, requiredOptions);
var result = execa.sync(command, commandArgs, execaOptions);
var stdout;
var stderr;
var code;
if (commandNotFound(result)) {
// This can happen if `command` is not an executable binary, or possibly
// under other conditions.
stdout = '';
stderr = "'" + command + "': command not found";
code = COMMAND_NOT_FOUND_ERROR_CODE;
} else {
stdout = result.stdout.toString();
stderr = result.stderr.toString();
code = result.code;
}
// Pass `continue: true` so we can specify a value for stdout.
if (code) common.error(stderr, code, { silent: true, continue: true });
return new common.ShellString(stdout, stderr, code);
}
module.exports = _cmd;