Skip to content

Commit

Permalink
Separate cleanup option handling into its own function (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored and sindresorhus committed Jun 20, 2019
1 parent b979534 commit 85a54d5
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,17 @@ const setupTimeout = (spawned, {timeout, killSignal}, context) => {
}
};

// #115
const setExitHandler = (spawned, {cleanup, detached}, context) => {
if (!cleanup || detached) {
return;
}

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

const execa = (file, args, options) => {
const parsed = handleArgs(file, args, options);
const command = joinCommand(file, args);
Expand All @@ -356,14 +367,6 @@ const execa = (file, args, options) => {
const kill = spawned.kill.bind(spawned);
spawned.kill = spawnedKill.bind(null, kill);

// #115
let removeExitHandler;
if (parsed.options.cleanup && !parsed.options.detached) {
removeExitHandler = onExit(() => {
spawned.kill();
});
}

const context = {timedOut: false};
let isCanceled = false;

Expand All @@ -373,11 +376,12 @@ const execa = (file, args, options) => {
context.timeoutId = undefined;
}

if (removeExitHandler) {
removeExitHandler();
if (context.removeExitHandler) {
context.removeExitHandler();
}
};

setExitHandler(spawned, parsed.options, context);
setupTimeout(spawned, parsed.options, context);

// TODO: Use native "finally" syntax when targeting Node.js 10
Expand Down

0 comments on commit 85a54d5

Please sign in to comment.