cleanup.js 428 B

12345678910111213141516
  1. import {addAbortListener} from 'node:events';
  2. import {onExit} from 'signal-exit';
  3. // If the `cleanup` option is used, call `subprocess.kill()` when the parent process exits
  4. export const cleanupOnExit = (subprocess, {cleanup, detached}, {signal}) => {
  5. if (!cleanup || detached) {
  6. return;
  7. }
  8. const removeExitHandler = onExit(() => {
  9. subprocess.kill();
  10. });
  11. addAbortListener(signal, () => {
  12. removeExitHandler();
  13. });
  14. };