Skip to content

Commit

Permalink
Separate cleanup into its own function (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored and sindresorhus committed Jun 20, 2019
1 parent a9fa13f commit c2787e5
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,16 @@ const setExitHandler = (spawned, {cleanup, detached}, context) => {
});
};

const cleanup = ({timeoutId, removeExitHandler}) => {
if (timeoutId !== undefined) {
clearTimeout(timeoutId);
}

if (removeExitHandler !== undefined) {
removeExitHandler();
}
};

const execa = (file, args, options) => {
const parsed = handleArgs(file, args, options);
const command = joinCommand(file, args);
Expand All @@ -370,22 +380,13 @@ const execa = (file, args, options) => {
const context = {timedOut: false};
let isCanceled = false;

const cleanup = () => {
if (context.timeoutId !== undefined) {
clearTimeout(context.timeoutId);
context.timeoutId = undefined;
}

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
const processDone = pFinally(handleSpawned(spawned, context), cleanup);
const processDone = pFinally(handleSpawned(spawned, context), () => {
cleanup(context);
});

const handlePromise = async () => {
const [result, stdout, stderr, all] = await getPromiseResult(spawned, parsed.options, processDone);
Expand Down

0 comments on commit c2787e5

Please # to comment.