Move `onExit()` cleanup code next to `onExit()` (#337)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by Sindre Sorhus · parent 0241d6e · Browse files · 2 files·+8 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+8 added−19 removed
- other:+8 added−19 removed
Provenance
- Commit
7d51047- Tree
28737f6- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
0241d6e
2 files changed+8 added−19 removed
modified:lib/kill.js+5 added-9 removed
81 unmodified lines8283848586868788899091919293949496979899959697989910010110210610710310481 unmodified lines return Promise.race([timeoutPromise, safeSpawnedPromise]);};
// `cleanup` option handlingconst 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};modified:index.js+3 added-10 removed
2 unmodified lines3 const childProcess = require('child_process');4 const crossSpawn = require('cross-spawn');5 const stripFinalNewline = require('strip-final-newline');6 const npmRunPath = require('npm-run-path');7-const pFinally = require('p-finally');7 const makeError = require('./lib/error');8 const normalizeStdio = require('./lib/stdio');10-const {spawnedKill, spawnedCancel, setupTimeout, setExitHandler, cleanup} = require('./lib/kill');9+const {spawnedKill, spawnedCancel, setupTimeout, setExitHandler} = require('./lib/kill');10 const {handleInput, getSpawnedResult, makeAllStream, validateInputSync} = require('./lib/stream.js');11 const {mergePromise, getSpawnedPromise} = require('./lib/promise.js');12 const {joinCommand, parseCommand} = require('./lib/command.js');13 79 unmodified lines93 return mergePromise(dummySpawned, errorPromise);94 }95 96 const spawnedPromise = getSpawnedPromise(spawned);97+ const timedPromise = setupTimeout(spawned, parsed.options, spawnedPromise);98+ const processDone = setExitHandler(spawned, parsed.options, timedPromise);99 100 const context = {isCanceled: false};101 102 spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned));103 spawned.cancel = spawnedCancel.bind(null, spawned, context);104 104- const timedPromise = setupTimeout(spawned, parsed.options, spawnedPromise);105- const removeExitHandler = setExitHandler(spawned, parsed.options);106-107- // TODO: Use native "finally" syntax when targeting Node.js 10108- const processDone = pFinally(timedPromise, () => {109- cleanup(removeExitHandler);110- });111-105 const handlePromise = async () => {106 const [{error, code, signal, timedOut}, stdoutResult, stderrResult, allResult] = await getSpawnedResult(spawned, parsed.options, processDone);107 const stdout = handleOutput(parsed.options, stdoutResult);108 const stderr = handleOutput(parsed.options, stderrResult);143 unmodified lines