Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the spawned process cancelable #189

Merged
merged 19 commits into from Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion index.js
Expand Up @@ -8,6 +8,7 @@ const npmRunPath = require('npm-run-path');
const isStream = require('is-stream');
const _getStream = require('get-stream');
const pFinally = require('p-finally');
const PCancelable = require('p-cancelable');
const onExit = require('signal-exit');
const errname = require('./lib/errname');
const stdio = require('./lib/stdio');
Expand Down Expand Up @@ -234,7 +235,7 @@ module.exports = (command, args, options) => {
}, parsed.options.timeout);
}

const processDone = new Promise(resolve => {
const processDone = new PCancelable((resolve, reject, onCancel) => {
spawned.on('exit', (code, signal) => {
cleanup();
resolve({code, signal});
Expand All @@ -251,6 +252,10 @@ module.exports = (command, args, options) => {
resolve({error});
});
}

onCancel(() => {
spawned.kill();
});
});

function destroy() {
Expand Down Expand Up @@ -311,6 +316,7 @@ module.exports = (command, args, options) => {
// eslint-disable-next-line promise/prefer-await-to-then
spawned.then = (onFulfilled, onRejected) => handlePromise().then(onFulfilled, onRejected);
spawned.catch = onRejected => handlePromise().catch(onRejected);
spawned.cancel = reason => processDone.cancel(reason);
ehmicky marked this conversation as resolved.
Show resolved Hide resolved

// TOOD: Remove the `if`-guard when targeting Node.js 10
if (Promise.prototype.finally) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -41,6 +41,7 @@
"get-stream": "^4.0.0",
"is-stream": "^1.1.0",
"npm-run-path": "^2.0.0",
"p-cancelable": "^1.1.0",
ehmicky marked this conversation as resolved.
Show resolved Hide resolved
"p-finally": "^1.0.0",
"signal-exit": "^3.0.0",
"strip-final-newline": "^2.0.0"
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Expand Up @@ -51,6 +51,13 @@ const execa = require('execa');
const {stdout} = await execa.shell('echo unicorns');
//=> 'unicorns'

// Cancelling a spawned process
const spawned = execa("echo unicorns");
spawned.catch( error => {
ehmicky marked this conversation as resolved.
Show resolved Hide resolved
// This would be equal to the reason provided for canceling
ehmicky marked this conversation as resolved.
Show resolved Hide resolved
console.log(error.message);
}); // To handle the CancelError
ammarbinfaisal1 marked this conversation as resolved.
Show resolved Hide resolved
spawned.cancel("reason for canceling");
ehmicky marked this conversation as resolved.
Show resolved Hide resolved

// Catching an error
try {
Expand Down
13 changes: 13 additions & 0 deletions test.js
Expand Up @@ -569,3 +569,16 @@ if (Promise.prototype.finally) {
t.is(result.message, 'called');
});
}

test('the spawned process is cancelable', t => {
const spawned = execa('ls');
ehmicky marked this conversation as resolved.
Show resolved Hide resolved
spawned.catch(error => {
ehmicky marked this conversation as resolved.
Show resolved Hide resolved
if (error.message === 'reason' && error.isCanceled) {
return;
}

throw error;
}); // To handle the CancelError
spawned.cancel('reason');
ehmicky marked this conversation as resolved.
Show resolved Hide resolved
t.true(spawned.killed);
ehmicky marked this conversation as resolved.
Show resolved Hide resolved
});