From 8ab8cc21ba9da5bd9059f08fefb8bb71860f345b Mon Sep 17 00:00:00 2001 From: himself65 Date: Sun, 21 Apr 2019 09:12:20 +0800 Subject: [PATCH] lib: improve option shell on child_process --- doc/api/child_process.md | 61 +++++++++++++------ lib/child_process.js | 25 ++++++-- ...t-child-process-exec-any-shells-windows.js | 1 + ...ild-process-spawnsync-validation-errors.js | 2 +- 4 files changed, 64 insertions(+), 25 deletions(-) diff --git a/doc/api/child_process.md b/doc/api/child_process.md index 58306dd2f0163b..159a9bb9f05673 100644 --- a/doc/api/child_process.md +++ b/doc/api/child_process.md @@ -152,9 +152,11 @@ changes: **Default:** `null`. * `env` {Object} Environment key-value pairs. **Default:** `process.env`. * `encoding` {string} **Default:** `'utf8'` - * `shell` {string} Shell to execute the command with. See - [Shell Requirements][] and [Default Windows Shell][]. **Default:** - `'/bin/sh'` on Unix, `process.env.ComSpec` on Windows. + * `shell` {boolean|string|Array} If `true`, runs `command` inside of a shell. + Uses `'/bin/sh'` on Unix, and `process.env.ComSpec` on Windows. A different + shell can be specified as a string. Or specify a shell with arguments. + See [Shell Requirements][] and [Default Windows Shell][]. + **Default:** `false` (no shell). * `timeout` {number} **Default:** `0` * `maxBuffer` {number} Largest amount of data in bytes allowed on stdout or stderr. If exceeded, the child process is terminated and any output is @@ -241,6 +243,20 @@ async function lsExample() { lsExample(); ``` +If `shell` is an `Array`, shell will be spawned with argument followed. + +```js +const assert = require('assert'); +const { exec } = require('child_process'); +// https://docs.microsoft.com/windows-server/administration/windows-commands/cmd +// exec cmd with no argument will displays +// the version and copyright information of the operating system. +exec('echo foo bar', { shell: ['cmd'] }, (error, stdout) => { + assert(error == null); + assert(!stdout.includes('foo') && !stdout.includes('bar')); +}); +``` + ### `child_process.execFile(file[, args][, options][, callback])`