From b2c5ff4d5a4fc715a8825ea97efe44db981b33de Mon Sep 17 00:00:00 2001 From: Martijn Walraven Date: Mon, 24 Aug 2015 10:47:22 +0200 Subject: [PATCH] Allow specifying a destination option for execFileSync/Async If specified, instead of capturing the output, the child process stdout will be piped to the destination stream. Although the standard spawn stdio option lets you pass in streams, these have to be connected to an open file descriptor. The destination option allows you to use any Writable, so it can be used with a Transform for instance. --- tools/utils/processes.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tools/utils/processes.js b/tools/utils/processes.js index bdc0c3d005..79d2185027 100644 --- a/tools/utils/processes.js +++ b/tools/utils/processes.js @@ -23,6 +23,8 @@ import child_process from 'child_process'; * @param {Array|String} [options.stdio] Child's stdio configuration. * (Default: 'pipe') Specifying anything else than 'pipe' will disallow * capture. + * @param {Writable} [options.destination] If specified, instead of capturing + * the output, the child process stdout will be piped to the destination stream. * @param {String} [options.waitForClose] Whether to wait for the child process * streams to close or to resolve the promise when the child process exits. * @returns {String} The stdout from the command @@ -44,6 +46,8 @@ export function execFileSync(command, args, options) { * @param {Array|String} [options.stdio] Child's stdio configuration. * (Default: 'pipe') Specifying anything else than 'pipe' will disallow * capture. + * @param {Writable} [options.destination] If specified, instead of capturing + * the output, the child process stdout will be piped to the destination stream. * @param {String} [options.waitForClose] Whether to wait for the child process * streams to close or to resolve the promise when the child process exits. * @returns {Promise} @@ -71,10 +75,14 @@ export function execFileAsync(command, args, let capturedStdout = ''; if (child.stdout) { - child.stdout.setEncoding('utf8'); - child.stdout.on('data', (data) => { - capturedStdout += data; - }); + if (options.destination) { + child.stdout.pipe(options.destination); + } else { + child.stdout.setEncoding('utf8'); + child.stdout.on('data', (data) => { + capturedStdout += data; + }); + } } let capturedStderr = '';