Created
January 8, 2020 08:07
-
-
Save darkxanter/30222456d9d34629ea063bcff95fca69 to your computer and use it in GitHub Desktop.
Node js block script exit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| process.stdin.resume(); //so the program will not close instantly | |
| const exitHandler = (options: { cleanup?: boolean; exit?: boolean }) => (exitCode?: any) => { | |
| if (options.cleanup) console.log('clean'); | |
| if (exitCode || exitCode === 0) console.log(exitCode); | |
| if (options.exit) process.exit(); | |
| }; | |
| //do something when app is closing | |
| process.on('exit', exitHandler({ cleanup: true })); | |
| ['SIGINT', 'SIGTERM', 'SIGUSR1', 'SIGUSR2', 'uncaughtException'] | |
| .forEach((it: any) => process.on(it, exitHandler({ exit: true }))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment