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 7 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 = 'command was canceled') => processDone.cancel(reason);

// 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
6 changes: 6 additions & 0 deletions readme.md
Expand Up @@ -51,6 +51,12 @@ const execa = require('execa');
const {stdout} = await execa.shell('echo unicorns');
//=> 'unicorns'

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

// Catching an error
try {
Expand Down
28 changes: 28 additions & 0 deletions test.js
Expand Up @@ -7,6 +7,7 @@ import getStream from 'get-stream';
import isRunning from 'is-running';
import delay from 'delay';
import tempfile from 'tempfile';
import {CancelError} from 'p-cancelable';
import execa from '.';

process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;
Expand Down Expand Up @@ -569,3 +570,30 @@ if (Promise.prototype.finally) {
t.is(result.message, 'called');
});
}

test('cancel method kills the spawned process', t => {
const spawned = execa('ls');
ehmicky marked this conversation as resolved.
Show resolved Hide resolved
spawned.catch(() => { }); // To handle the CancelError
spawned.cancel();
t.true(spawned.killed);
ehmicky marked this conversation as resolved.
Show resolved Hide resolved
});

test('invoking cancel method throws CancelError', async t => {
const spawned = execa('ls');
spawned.cancel();
await t.throwsAsync(spawned, CancelError);
});
ehmicky marked this conversation as resolved.
Show resolved Hide resolved

test('default CancelError message is "command was canceled"', async t => {
const spawned = execa('ls');
spawned.cancel();
const error = await t.throwsAsync(spawned);
ehmicky marked this conversation as resolved.
Show resolved Hide resolved
t.is(error.message, 'command was canceled');
});

test('CancelError message is equal to reason argument passed to cancel method', async t => {
const spawned = execa('ls');
spawned.cancel('reason for canceling');
const error = await t.throwsAsync(spawned);
t.is(error.message, 'reason for canceling');
});