1/** Add functions that run before the process exits. Returns a function for removing the listeners. */ 2export function installExitHooks(listener: NodeJS.SignalsListener): () => void { 3 const killSignals: ['SIGINT', 'SIGTERM'] = ['SIGINT', 'SIGTERM']; 4 for (const signal of killSignals) { 5 process.on(signal, listener); 6 } 7 return () => { 8 if (!listener) { 9 return; 10 } 11 for (const signal of killSignals) { 12 process.off(signal, listener); 13 } 14 // Allow the listener to be GC'd 15 listener = null; 16 }; 17} 18