From 7067a7155f26f595a6344c16f64ac1985e9e5403 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 19 Jul 2010 20:08:33 -0700 Subject: [PATCH] Specify env differently in execFile Callbacks should always be the last argument. --- doc/api.markdown | 1 + lib/child_process.js | 32 +++++++++++++++++----- test/simple/test-child-process-exec-env.js | 4 +-- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/doc/api.markdown b/doc/api.markdown index 7559e40a57..8c9b8dc25d 100644 --- a/doc/api.markdown +++ b/doc/api.markdown @@ -1048,6 +1048,7 @@ There is a second optional argument to specify several options. The default opti , timeout: 0 , maxBuffer: 200*1024 , killSignal: 'SIGKILL' + , env: null } If `timeout` is greater than 0, then it will kill the child process diff --git a/lib/child_process.js b/lib/child_process.js index 3996c954c1..43308a200f 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -10,7 +10,7 @@ var spawn = exports.spawn = function (path, args, env, customFds) { return child; }; -exports.exec = function (command /*, options, callback, env */) { +exports.exec = function (command /*, options, callback */) { if (arguments.length < 3) { return exports.execFile("/bin/sh", ["-c", command], arguments[1]); } 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' , timeout: 0 , maxBuffer: 200*1024 , killSignal: 'SIGKILL' + , env: null }; + var args, optionArg, callback; + + // 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]; + } - var callback = (arguments.length == 5 ? arguments[3] : arguments[arguments.length-1]); - - if (arguments[2] && typeof arguments[2] == 'object') { + // Merge optionArg into options + if (optionArg) { var keys = Object.keys(options); for (var i = 0; i < keys.length; 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 stderr = ""; var killed = false; diff --git a/test/simple/test-child-process-exec-env.js b/test/simple/test-child-process-exec-env.js index 58d6d90148..d4cd989a31 100644 --- a/test/simple/test-child-process-exec-env.js +++ b/test/simple/test-child-process-exec-env.js @@ -5,7 +5,7 @@ success_count = 0; error_count = 0; response = ""; -child = exec('/usr/bin/env', [], function (err, stdout, stderr) { +child = exec('/usr/bin/env', { env: {'HELLO' : 'WORLD'}}, function (err, stdout, stderr) { if (err) { error_count++; console.log('error!: ' + err.code); @@ -16,7 +16,7 @@ child = exec('/usr/bin/env', [], function (err, stdout, stderr) { success_count++; assert.equal(true, stdout != ""); } -}, {'HELLO' : 'WORLD'}); +}); child.stdout.setEncoding('utf8');