| 2 | const { errorMessage, getExitCodeFromError } = require('../utils/error-message.js') |
| 3 | |
| 4 | class ExitHandler { |
| 5 | #npm = null |
| 6 | #process = null |
| 7 | #exited = false |
| 8 | #exitErrorMessage = false |
| 9 | |
| 10 | #noNpmError = false |
| 11 | |
| 12 | get #hasNpm () { |
| 13 | return !!this.#npm |
| 14 | } |
| 15 | |
| 16 | get #loaded () { |
| 17 | return !!this.#npm?.loaded |
| 18 | } |
| 19 | |
| 20 | get #showExitErrorMessage () { |
| 21 | if (!this.#loaded) { |
| 22 | return false |
| 23 | } |
| 24 | if (!this.#exited) { |
| 25 | return true |
| 26 | } |
| 27 | return this.#exitErrorMessage |
| 28 | } |
| 29 | |
| 30 | get #notLoadedOrExited () { |
| 31 | return !this.#loaded && !this.#exited |
| 32 | } |
| 33 | |
| 34 | setNpm (npm) { |
| 35 | this.#npm = npm |
| 36 | } |
| 37 | |
| 38 | constructor ({ process }) { |
| 39 | this.#process = process |
| 40 | this.#process.on('exit', this.#handleProcessExitAndReset) |
| 41 | } |
| 42 | |
| 43 | registerUncaughtHandlers () { |
| 44 | this.#process.on('uncaughtException', this.#handleExit) |
| 45 | this.#process.on('unhandledRejection', this.#handleExit) |
| 46 | } |
| 47 | |
| 48 | exit (err) { |
| 49 | this.#handleExit(err) |
| 50 | } |
| 51 | |
| 52 | #handleProcessExitAndReset = (code) => { |
| 53 | this.#handleProcessExit(code) |
| 54 | |
| 55 | // Reset all the state. This is only relevant for tests since in reality the process fully exits here. |
| 56 | this.#process.off('exit', this.#handleProcessExitAndReset) |
| 57 | this.#process.off('uncaughtException', this.#handleExit) |
| 58 | this.#process.off('unhandledRejection', this.#handleExit) |
| 59 | if (this.#loaded) { |
| 60 | this.#npm.unload() |
| 61 | } |
nothing calls this directly
no test coverage detected