summaryrefslogtreecommitdiff
path: root/lib/child_process.js
diff options
context:
space:
mode:
authorcjihrig <cjihrig@gmail.com>2016-01-08 16:17:50 -0500
committercjihrig <cjihrig@gmail.com>2016-01-26 19:40:43 -0500
commitc3bb4b1aa5e907d489619fb43d233c3336bfc03d (patch)
treebc6f1f4ec214f4b72e9cbb2f368b39317af12a99 /lib/child_process.js
parent34daaa764fa83557ec41a99f59d1701739321c33 (diff)
downloadandroid-node-v8-c3bb4b1aa5e907d489619fb43d233c3336bfc03d.tar.gz
android-node-v8-c3bb4b1aa5e907d489619fb43d233c3336bfc03d.tar.bz2
android-node-v8-c3bb4b1aa5e907d489619fb43d233c3336bfc03d.zip
child_process: add shell option to spawn()
This commit adds a shell option, to spawn() and spawnSync(). This option allows child processes to be spawned with or without a shell. The option also allows a custom shell to be defined, for compatibility with exec()'s shell option. Fixes: https://github.com/nodejs/node/issues/1009 PR-URL: https://github.com/nodejs/node/pull/4598 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/child_process.js')
-rw-r--r--lib/child_process.js53
1 files changed, 29 insertions, 24 deletions
diff --git a/lib/child_process.js b/lib/child_process.js
index ee73562d24..e682aed5ae 100644
--- a/lib/child_process.js
+++ b/lib/child_process.js
@@ -71,7 +71,8 @@ exports._forkChild = function(fd) {
function normalizeExecArgs(command /*, options, callback*/) {
- var file, args, options, callback;
+ let options;
+ let callback;
if (typeof arguments[1] === 'function') {
options = undefined;
@@ -81,25 +82,12 @@ function normalizeExecArgs(command /*, options, callback*/) {
callback = arguments[2];
}
- if (process.platform === 'win32') {
- file = process.env.comspec || 'cmd.exe';
- args = ['/s', '/c', '"' + command + '"'];
- // Make a shallow copy before patching so we don't clobber the user's
- // options object.
- options = util._extend({}, options);
- options.windowsVerbatimArguments = true;
- } else {
- file = '/bin/sh';
- args = ['-c', command];
- }
-
- if (options && options.shell)
- file = options.shell;
+ // Make a shallow copy so we don't clobber the user's options object.
+ options = Object.assign({}, options);
+ options.shell = typeof options.shell === 'string' ? options.shell : true;
return {
- cmd: command,
- file: file,
- args: args,
+ file: command,
options: options,
callback: callback
};
@@ -109,7 +97,6 @@ function normalizeExecArgs(command /*, options, callback*/) {
exports.exec = function(command /*, options, callback*/) {
var opts = normalizeExecArgs.apply(null, arguments);
return exports.execFile(opts.file,
- opts.args,
opts.options,
opts.callback);
};
@@ -123,7 +110,8 @@ exports.execFile = function(file /*, args, options, callback*/) {
maxBuffer: 200 * 1024,
killSignal: 'SIGTERM',
cwd: null,
- env: null
+ env: null,
+ shell: false
};
// Parse the optional positional parameters.
@@ -153,6 +141,7 @@ exports.execFile = function(file /*, args, options, callback*/) {
env: options.env,
gid: options.gid,
uid: options.uid,
+ shell: options.shell,
windowsVerbatimArguments: !!options.windowsVerbatimArguments
});
@@ -331,7 +320,23 @@ function normalizeSpawnArguments(file /*, args, options*/) {
else if (options === null || typeof options !== 'object')
throw new TypeError('"options" argument must be an object');
- options = util._extend({}, options);
+ // Make a shallow copy so we don't clobber the user's options object.
+ options = Object.assign({}, options);
+
+ if (options.shell) {
+ const command = [file].concat(args).join(' ');
+
+ if (process.platform === 'win32') {
+ file = typeof options.shell === 'string' ? options.shell :
+ process.env.comspec || 'cmd.exe';
+ args = ['/s', '/c', '"' + command + '"'];
+ options.windowsVerbatimArguments = true;
+ } else {
+ file = typeof options.shell === 'string' ? options.shell : '/bin/sh';
+ args = ['-c', command];
+ }
+ }
+
args.unshift(file);
var env = options.env || process.env;
@@ -491,12 +496,12 @@ function execFileSync(/*command, args, options*/) {
exports.execFileSync = execFileSync;
-function execSync(/*command, options*/) {
+function execSync(command /*, options*/) {
var opts = normalizeExecArgs.apply(null, arguments);
var inheritStderr = opts.options ? !opts.options.stdio : true;
- var ret = spawnSync(opts.file, opts.args, opts.options);
- ret.cmd = opts.cmd;
+ var ret = spawnSync(opts.file, opts.options);
+ ret.cmd = command;
if (inheritStderr)
process.stderr.write(ret.stderr);