add `execa.sync()` (#7)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent d6e3220 · Browse files · 3 files·+33 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+33 added−12 removed
- other:+33 added−12 removed
Provenance
- Commit
9345d2c- Tree
1a55e37- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
d6e3220
3 files changed+33 added−12 removed
modified:index.js+19 added-11 removed
23 unmodified lines24252627282829303132339 unmodified lines4344454647484950515253545556575859605253545556575859616263642 unmodified lines67686970707171727374757635 unmodified lines11211311411511611711811912012112223 unmodified lines
opts = objectAssign({ maxBuffer: TEN_MEBIBYTE, stripEof: true, preferLocal: true preferLocal: true, encoding: 'utf8' }, parsed.options);
if (opts.preferLocal) { opts.env = objectAssign({}, opts.env || process.env);9 unmodified lines opts: opts };}
function handleOutput(opts, val) { if (opts.stripEof) { val = stripEof(val); }
return val;}
module.exports = function (cmd, args, opts) { var spawned;
var promise = new Promise(function (resolve, reject) { var parsed = handleArgs(cmd, args, opts);
var handle = function (val) { if (parsed.opts.stripEof) { val = stripEof(val); }
return val; };
spawned = childProcess.execFile(parsed.cmd, parsed.args, parsed.opts, function (err, stdout, stderr) { if (err) { err.stdout = stdout; err.stderr = stderr;2 unmodified lines return; }
resolve({ stdout: handle(stdout), stderr: handle(stderr) stdout: handleOutput(parsed.opts, stdout), stderr: handleOutput(parsed.opts, stderr) }); });
crossSpawnAsync._enoent.hookChildProcess(spawned, parsed);35 unmodified lines crossSpawnAsync._enoent.hookChildProcess(spawned, parsed);
return spawned;};
module.exports.sync = function (cmd, args, opts) { var parsed = handleArgs(cmd, args, opts); var out = childProcess.execFileSync(parsed.cmd, parsed.args, parsed.opts);
return handleOutput(parsed.opts, out);};modified:readme.md+9 added-1 removed
55 unmodified lines56 ## API57 58 ### execa(file, [arguments], [options])59 60-Execute a file directly.60+Execute a file.61 62 Same options as [`child_process.execFile`](https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback).63 64 Returns a promise for a result object with `stdout` and `stderr` properties. Additionally, the promise instance has a `pid` property (the ID of the child process) and a `kill` method (for sending signals to the child process).11 unmodified lines76 Spawn a file.77 78 Same API as [`child_process.spawn`](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options).79 80+### execa.sync(file, [arguments], [options])81+82+Execute a file synchronously.83+84+Same options as [`child_process.execFileSync`](https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback), except that the default encoding is `utf8` instead of `buffer`.85+86+Returns `stdout`.87+88 ### options89 90 Additional exposed options:91 19 unmodified linesmodified:test.js+5 added
38 unmodified lines3940414243444546474849505116 unmodified lines38 unmodified lines t.is(typeof m.spawn('echo').pid, 'number'); t.is((await getStream(m.spawn('echo', ['foo']).stdout)).trim(), 'foo');});
test('execa.sync()', t => { const stdout = m.sync('echo', ['foo']); t.is(stdout, 'foo');});
test('stripEof option', async t => { const {stdout} = await m('echo', ['foo'], {stripEof: false}); t.is(stdout, 'foo\n');});16 unmodified lines