Specify env differently in execFile
Callbacks should always be the last argument.
This commit is contained in:
parent
352b8c3ad5
commit
7067a7155f
@ -1048,6 +1048,7 @@ There is a second optional argument to specify several options. The default opti
|
|||||||
, timeout: 0
|
, timeout: 0
|
||||||
, maxBuffer: 200*1024
|
, maxBuffer: 200*1024
|
||||||
, killSignal: 'SIGKILL'
|
, killSignal: 'SIGKILL'
|
||||||
|
, env: null
|
||||||
}
|
}
|
||||||
|
|
||||||
If `timeout` is greater than 0, then it will kill the child process
|
If `timeout` is greater than 0, then it will kill the child process
|
||||||
|
@ -10,7 +10,7 @@ var spawn = exports.spawn = function (path, args, env, customFds) {
|
|||||||
return child;
|
return child;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.exec = function (command /*, options, callback, env */) {
|
exports.exec = function (command /*, options, callback */) {
|
||||||
if (arguments.length < 3) {
|
if (arguments.length < 3) {
|
||||||
return exports.execFile("/bin/sh", ["-c", command], arguments[1]);
|
return exports.execFile("/bin/sh", ["-c", command], arguments[1]);
|
||||||
} else if (arguments.length < 4) {
|
} else if (arguments.length < 4) {
|
||||||
@ -20,24 +20,42 @@ exports.exec = function (command /*, options, callback, env */) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.execFile = function (file, args /*, options, callback, env */) {
|
|
||||||
|
// execFile("something.sh", { env: ENV }, funciton() { })
|
||||||
|
|
||||||
|
exports.execFile = function (file /* args, options, callback */) {
|
||||||
var options = { encoding: 'utf8'
|
var options = { encoding: 'utf8'
|
||||||
, timeout: 0
|
, timeout: 0
|
||||||
, maxBuffer: 200*1024
|
, maxBuffer: 200*1024
|
||||||
, killSignal: 'SIGKILL'
|
, killSignal: 'SIGKILL'
|
||||||
|
, env: null
|
||||||
};
|
};
|
||||||
|
var args, optionArg, callback;
|
||||||
|
|
||||||
var callback = (arguments.length == 5 ? arguments[3] : arguments[arguments.length-1]);
|
// Parse the parameters.
|
||||||
|
|
||||||
if (arguments[2] && typeof arguments[2] == 'object') {
|
if (typeof arguments[arguments.length-1] === "function") {
|
||||||
|
callback = arguments[arguments.length-1];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(arguments[1])) {
|
||||||
|
args = arguments[1];
|
||||||
|
if (typeof arguments[2] === 'object') optionArg = arguments[2];
|
||||||
|
} else {
|
||||||
|
args = [];
|
||||||
|
if (typeof arguments[1] === 'object') optionArg = arguments[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge optionArg into options
|
||||||
|
if (optionArg) {
|
||||||
var keys = Object.keys(options);
|
var keys = Object.keys(options);
|
||||||
for (var i = 0; i < keys.length; i++) {
|
for (var i = 0; i < keys.length; i++) {
|
||||||
var k = keys[i];
|
var k = keys[i];
|
||||||
if (arguments[2][k] !== undefined) options[k] = arguments[2][k];
|
if (optionArg[k] !== undefined) options[k] = optionArg[k];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var child = arguments[4] ? spawn(file, args, arguments[4]) : spawn(file, args);
|
var child = spawn(file, args, options.env);
|
||||||
var stdout = "";
|
var stdout = "";
|
||||||
var stderr = "";
|
var stderr = "";
|
||||||
var killed = false;
|
var killed = false;
|
||||||
|
@ -5,7 +5,7 @@ success_count = 0;
|
|||||||
error_count = 0;
|
error_count = 0;
|
||||||
response = "";
|
response = "";
|
||||||
|
|
||||||
child = exec('/usr/bin/env', [], function (err, stdout, stderr) {
|
child = exec('/usr/bin/env', { env: {'HELLO' : 'WORLD'}}, function (err, stdout, stderr) {
|
||||||
if (err) {
|
if (err) {
|
||||||
error_count++;
|
error_count++;
|
||||||
console.log('error!: ' + err.code);
|
console.log('error!: ' + err.code);
|
||||||
@ -16,7 +16,7 @@ child = exec('/usr/bin/env', [], function (err, stdout, stderr) {
|
|||||||
success_count++;
|
success_count++;
|
||||||
assert.equal(true, stdout != "");
|
assert.equal(true, stdout != "");
|
||||||
}
|
}
|
||||||
}, {'HELLO' : 'WORLD'});
|
});
|
||||||
|
|
||||||
child.stdout.setEncoding('utf8');
|
child.stdout.setEncoding('utf8');
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user