Skip to content
Myr

Refactor `kill()` (#272)

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 6c0a400 · Browse files · 1 file+19 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+19 added−14 removed

  • other:+19 added−14 removed

1 file+19 added−14 removed

modified:index.js+19 added-14 removed
211 unmodified lines
212213 return [file, ...args].join(' ');214}215216function spawnedKill(kill, signal = 'SIGTERM', options = {}) {217 const killResult = kill(signal);218 setKillTimeout(kill, signal, options, killResult);219 return killResult;220}221222function setKillTimeout(kill, signal, options, killResult) {223 if (!shouldForceKill(signal, options, killResult)) {224 return;225 }226227 const forceKillAfter = Number.isInteger(options.forceKillAfter) ?228 options.forceKillAfter :229 5000;230 setTimeout(() => kill('SIGKILL'), forceKillAfter).unref();231}232233function shouldForceKill(signal, options, killResult) {234 return ((typeof signal === 'string' &&235 signal.toUpperCase() === 'SIGTERM') ||236 signal === os.constants.signals.SIGTERM) &&
18 unmodified lines
255 killed: false256 }));257 }258242 const originalKill = spawned.kill.bind(spawned);243 spawned.kill = (signal = 'SIGTERM', options = {}) => {244 const killResult = originalKill(signal);245 if (shouldForceKill(signal, options, killResult)) {246 const forceKillAfter = Number.isInteger(options.forceKillAfter) ?247 options.forceKillAfter :248 5000;249 setTimeout(() => {250 originalKill('SIGKILL');251 }, forceKillAfter).unref();252 }259 const kill = spawned.kill.bind(spawned);260 spawned.kill = spawnedKill.bind(null, kill);261254 return killResult;255 };256262 // #115263 let removeExitHandler;264 if (parsed.options.cleanup && !parsed.options.detached) {265 removeExitHandler = onExit(() => {
217 unmodified lines