Skip to content
Myr

Use spawn instead of execFile (#27)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* [WIP]: just use spawn instead of execFile childProcess.execFile pays no attention `opts.stdio`. `execa.spawn` allows you to set `opts.stdio` options, but it doesn't return a promise. I think we should forgoe using `childProcess.execFile` and just use spawn. We can reimplement much of `execFile`s utility ourself. Rather than having two separate methods `spawn` and `execFile` - I think we should work towards a single method. What options the user sets for `opts.stdio` or `opts.stdin`, etc; Those should determine whether or not the stream is buffered and made part of the promise result, or if they are just streamed (or sent to an Observable), or ignored. This simplifies the API, and offers potentially far more control. Want to use streams/observables for `stdout` because you know it will contain lots of data and you don't want to buffer, but it would nice to just have `stderr` buffered for you to print in case of an error? That would be doable this way, and really separates it from child_process. childProcess.execFile is really just a wrapper around spawn anyways. It's main utility is that in concats `stdio` and `stdout`. Which is pretty easy for us to achieve on our own using `get-stream` This reimplements nearly all of the functionality of execFile. Remaining items to be feature complete with execFile: - [ ] timeout support (should be easy to add back). - [ ] errname resolution. See TODO note on that. I'm not sure we want to use `process.bind('uv')`. I don't really know what it does. - [ ] some stream cleanup code (see childProces.execFile's internal `kill` method. It calls `stream.destroy()`. I am not sure how that will affect `get-stream`) * use regex test so I can see what went wrong on Windows * remove leading `./` - If this fixes windows, we should figure out why. * Why? Why? Why Windows??? Why does this make the tests pass? Could you possibly make the error output any more cryptic? I don't understand you Windows! We are not friends anymore. * deprecate execa.spawn * tweaks * more tweaks * update deprecation message

authored by James Talmage · committed by James Talmage · parent c726096 · Browse files · 4 files+83 added−34 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+83 added−34 removed

  • other:+83 added−34 removed

4 files+83 added−34 removed

modified:fixtures/error-message.js+1 added-1 removed
modified:index.js+74 added-30 removed
1'use strict';2var childProcess = require('child_process');3var util = require('util');4var crossSpawnAsync = require('cross-spawn-async');5var stripEof = require('strip-eof');6var objectAssign = require('object-assign');7var npmRunPath = require('npm-run-path');8var isStream = require('is-stream');9var _getStream = require('get-stream');10var pathKey = require('path-key')();11var TEN_MEBIBYTE = 1024 * 1024 * 10;1213function handleArgs(cmd, args, opts) {
48 unmodified lines
62 }63}6465function handleOutput(opts, val) {64 if (opts.stripEof) {66 if (val && opts.stripEof) {67 val = stripEof(val);68 }6970 return val;
21 unmodified lines
9293 return fn(file, args, opts);94}9596function getStream(stream, encoding) {97 if (!stream) {98 return null;99 }100101 if (encoding) {102 return _getStream(stream, {encoding: encoding});103 }104 return _getStream.buffer(stream);105}106107function processDone(spawned) {108 return new Promise(function (resolve) {109 spawned.on('exit', function (code, signal) {110 resolve({code: code, signal: signal});111 });112113 spawned.on('error', function (err) {114 resolve({err: err});115 });116 });117}118119module.exports = function (cmd, args, opts) {95 var spawned;120 var parsed = handleArgs(cmd, args, opts);121 var encoding = parsed.opts.encoding;122 var spawned = childProcess.spawn(parsed.cmd, parsed.args, parsed.opts);12397 var promise = new Promise(function (resolve, reject) {98 var parsed = handleArgs(cmd, args, opts);124 var promise = Promise.all([125 processDone(spawned),126 getStream(spawned.stdout, encoding),127 getStream(spawned.stderr, encoding)128 ]).then(function (arr) {129 var result = arr[0];130 var stdout = arr[1];131 var stderr = arr[2];132100 spawned = childProcess.execFile(parsed.cmd, parsed.args, parsed.opts, function (err, stdout, stderr) {101 if (err) {102 err.stdout = stdout;103 err.stderr = stderr;104 err.message += stdout;105 reject(err);106 return;133 var err = result.err;134 var code = result.code;135 var signal = result.signal;136137 if (err || code !== 0 || signal !== null) {138 var joinedCmd = cmd;139140 if (Array.isArray(args) && args.length) {141 joinedCmd += ' ' + args.join(' ');142 }143109 resolve({110 stdout: handleOutput(parsed.opts, stdout),111 stderr: handleOutput(parsed.opts, stderr)112 });113 });144 if (!err) {145 err = new Error('Command failed: ' + joinedCmd + '\n' + stderr + stdout);146115 crossSpawnAsync._enoent.hookChildProcess(spawned, parsed);147 // TODO: missing some timeout logic for killed148 // https://github.com/nodejs/node/blob/master/lib/child_process.js#L203149 // err.killed = spawned.killed || killed;150 err.killed = spawned.killed;151117 handleInput(spawned, parsed.opts);152 // TODO: child_process applies the following logic for resolving the code:153 // var uv = process.bind('uv');154 // ex.code = code < 0 ? uv.errname(code) : code;155 err.code = code;156 }157158 err.stdout = stdout;159 err.stderr = stderr;160 throw err;161 }162163 return {164 stdout: handleOutput(parsed.opts, stdout),165 stderr: handleOutput(parsed.opts, stderr)166 };167 });168169 crossSpawnAsync._enoent.hookChildProcess(spawned, parsed);170171 handleInput(spawned, parsed.opts);172173 spawned.then = promise.then.bind(promise);174 spawned.catch = promise.catch.bind(promise);175176 return spawned;
16 unmodified lines
193module.exports.shell = function (cmd, opts) {194 return handleShell(module.exports, cmd, opts);195};196144module.exports.spawn = function (cmd, args, opts) {145 var parsed = handleArgs(cmd, args, opts);146 var spawned = childProcess.spawn(parsed.cmd, parsed.args, parsed.opts);197module.exports.spawn = util.deprecate(module.exports, 'execa.spawn() is deprecated. Use execa() instead.');198148 crossSpawnAsync._enoent.hookChildProcess(spawned, parsed);149150 return spawned;151};152199module.exports.sync = function (cmd, args, opts) {200 var parsed = handleArgs(cmd, args, opts);201202 if (isStream(parsed.opts.input)) {
1 unmodified line
204 }205206 var result = childProcess.spawnSync(parsed.cmd, parsed.args, parsed.opts);207162 if (parsed.opts.stripEof) {163 result.stdout = stripEof(result.stdout);164 result.stderr = stripEof(result.stderr);165 }208 result.stdout = handleOutput(parsed.opts, result.stdout);209 result.stderr = handleOutput(parsed.opts, result.stderr);210211 return result;212};213
3 unmodified lines
modified:package.json+1 added-1 removed
modified:test.js+7 added-2 removed