Browse Source

Specify env differently in execFile

Callbacks should always be the last argument.
v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
7067a7155f
  1. 1
      doc/api.markdown
  2. 30
      lib/child_process.js
  3. 4
      test/simple/test-child-process-exec-env.js

1
doc/api.markdown

@ -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

30
lib/child_process.js

@ -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 (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];
}
if (arguments[2] && typeof arguments[2] == 'object') { // 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;

4
test/simple/test-child-process-exec-env.js

@ -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…
Cancel
Save