From 7d510474296bbbbd5a5bb86197faf73dde8aeb40 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Thu, 27 Jun 2019 11:49:02 -0700 Subject: [PATCH] Move `onExit()` cleanup code next to `onExit()` (#337) --- index.js | 13 +++---------- lib/kill.js | 14 +++++--------- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index 2ae9c5f3ea..3e3b125720 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -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); diff --git a/lib/kill.js b/lib/kill.js index aa084e3fb2..c23d6a5ad5 100644 --- a/lib/kill.js +++ b/lib/kill.js @@ -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 };