Attach pid and kill() to returned promise (#11)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Attach pid and kill() to returned promise * Drop usage of "test.only"
authored by Luke Horvat · committed by Sindre Sorhus · parent d6f0a42 · Browse files · 3 files·+17 added−5 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+17 added−5 removed
- other:+17 added−5 removed
Provenance
- Commit
9da5484- Tree
b1eba0e- Author
Luke Horvat <lukehorvat@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
d6f0a42
3 files changed+17 added−5 removed
modified:index.js+8 added-2 removed
5 unmodified lines67891010111213141529 unmodified lines454647484849505152538 unmodified lines6263646566676869707172737422 unmodified lines5 unmodified linesvar npmRunPath = require('npm-run-path');var TEN_MEBIBYTE = 1024 * 1024 * 10;
module.exports = function (cmd, args, opts) { var spawned; return new Promise(function (resolve, reject) { var promise = new Promise(function (resolve, reject) { var parsed;
if (opts && opts.__winShell === true) { delete opts.__winShell;29 unmodified lines path: opts.env.PATH }); }
var spawned = childProcess.execFile(parsed.command, parsed.args, opts, function (err, stdout, stderr) { spawned = childProcess.execFile(parsed.command, parsed.args, opts, function (err, stdout, stderr) { if (err) { err.stdout = stdout; err.stderr = stderr; err.message += stdout;8 unmodified lines });
crossSpawnAsync._enoent.hookChildProcess(spawned, parsed); });
promise.kill = spawned.kill.bind(spawned); promise.pid = spawned.pid;
return promise;};
module.exports.shell = function (cmd, opts) { var file;22 unmodified linesmodified:readme.md+2 added-2 removed
59 unmodified lines6061626364646566676869707172727374757626 unmodified lines59 unmodified linesExecute a file directly.
Same options as [`child_process.execFile`](https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback).
Returns a promise for a result object with `stdout` and `stderr` properties.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).
### execa.shell(command, [options])
Execute a command through the system shell. Prefer `execa()` whenever possible, as it's both faster and safer.
Same options as [`child_process.exec`](https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback).
Returns a promise for a result object with `stdout` and `stderr` properties.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).
### execa.spawn(file, [arguments], [options])
Spawn a file.26 unmodified linesmodified:test.js+7 added-1 removed
19 unmodified lines20 t.is(typeof err.stdout, 'string');21 }22 });23 24-test.only('include stdout in errors for improved debugging', async t => {24+test('include stdout in errors for improved debugging', async t => {25 try {26 await m('./fixtures/error-message.js');27 } catch (err) {28 t.true(err.message.indexOf('stdout') !== -1);22 unmodified lines51 process.env.PATH = '';52 await t.throws(m('cat-names', {preferLocal: false}), /spawn cat-names ENOENT/);53 process.env.PATH = _path;54 });55+56+test('execa() returns a promise with kill() and pid', t => {57+ const promise = m('echo', ['foo']);58+ t.is(typeof promise.kill, 'function');59+ t.is(typeof promise.pid, 'number');60+});