Skip to content

Commit

Permalink
Move onExit() cleanup code next to onExit() (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored and sindresorhus committed Jun 27, 2019
1 parent 0241d6e commit 7d51047
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
13 changes: 3 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ const childProcess = require('child_process');
const crossSpawn = require('cross-spawn');
const stripFinalNewline = require('strip-final-newline');
const npmRunPath = require('npm-run-path');
const pFinally = require('p-finally');
const makeError = require('./lib/error');
const normalizeStdio = require('./lib/stdio');
const {spawnedKill, spawnedCancel, setupTimeout, setExitHandler, cleanup} = require('./lib/kill');
const {spawnedKill, spawnedCancel, setupTimeout, setExitHandler} = require('./lib/kill');
const {handleInput, getSpawnedResult, makeAllStream, validateInputSync} = require('./lib/stream.js');
const {mergePromise, getSpawnedPromise} = require('./lib/promise.js');
const {joinCommand, parseCommand} = require('./lib/command.js');
Expand Down Expand Up @@ -95,20 +94,14 @@ const execa = (file, args, options) => {
}

const spawnedPromise = getSpawnedPromise(spawned);
const timedPromise = setupTimeout(spawned, parsed.options, spawnedPromise);
const processDone = setExitHandler(spawned, parsed.options, timedPromise);

const context = {isCanceled: false};

spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned));
spawned.cancel = spawnedCancel.bind(null, spawned, context);

const timedPromise = setupTimeout(spawned, parsed.options, spawnedPromise);
const removeExitHandler = setExitHandler(spawned, parsed.options);

// TODO: Use native "finally" syntax when targeting Node.js 10
const processDone = pFinally(timedPromise, () => {
cleanup(removeExitHandler);
});

const handlePromise = async () => {
const [{error, code, signal, timedOut}, stdoutResult, stderrResult, allResult] = await getSpawnedResult(spawned, parsed.options, processDone);
const stdout = handleOutput(parsed.options, stdoutResult);
Expand Down
14 changes: 5 additions & 9 deletions lib/kill.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,22 @@ const setupTimeout = (spawned, {timeout, killSignal = 'SIGTERM'}, spawnedPromise
};

// `cleanup` option handling
const setExitHandler = (spawned, {cleanup, detached}) => {
const setExitHandler = (spawned, {cleanup, detached}, timedPromise) => {
if (!cleanup || detached) {
return;
}

return onExit(() => {
const removeExitHandler = onExit(() => {
spawned.kill();
});
};

const cleanup = removeExitHandler => {
if (removeExitHandler !== undefined) {
removeExitHandler();
}
// TODO: Use native "finally" syntax when targeting Node.js 10
return pFinally(timedPromise, removeExitHandler);
};

module.exports = {
spawnedKill,
spawnedCancel,
setupTimeout,
setExitHandler,
cleanup
setExitHandler
};

0 comments on commit 7d51047

Please sign in to comment.