| const { log, output, META } = require('proc-log')
|
| const { errorMessage, getExitCodeFromError } = require('../utils/error-message.js')
|
|
|
| class ExitHandler {
|
| #npm = null
|
| #process = null
|
| #exited = false
|
| #exitErrorMessage = false
|
|
|
| #noNpmError = false
|
|
|
| get #hasNpm () {
|
| return !!this.#npm
|
| }
|
|
|
| get #loaded () {
|
| return !!this.#npm?.loaded
|
| }
|
|
|
| get #showExitErrorMessage () {
|
| if (!this.#loaded) {
|
| return false
|
| }
|
| if (!this.#exited) {
|
| return true
|
| }
|
| return this.#exitErrorMessage
|
| }
|
|
|
| get #notLoadedOrExited () {
|
| return !this.#loaded && !this.#exited
|
| }
|
|
|
| setNpm (npm) {
|
| this.#npm = npm
|
| }
|
|
|
| constructor ({ process }) {
|
| this.#process = process
|
| this.#process.on('exit', this.#handleProcessExitAndReset)
|
| }
|
|
|
| registerUncaughtHandlers () {
|
| this.#process.on('uncaughtException', this.#handleExit)
|
| this.#process.on('unhandledRejection', this.#handleExit)
|
| }
|
|
|
| exit (err) {
|
| this.#handleExit(err)
|
| }
|
|
|
| #handleProcessExitAndReset = (code) => {
|
| this.#handleProcessExit(code)
|
|
|
|
|
| this.#process.off('exit', this.#handleProcessExitAndReset)
|
| this.#process.off('uncaughtException', this.#handleExit)
|
| this.#process.off('unhandledRejection', this.#handleExit)
|
| if (this.#loaded) {
|
| this.#npm.unload()
|
| }
|
| this.#npm = null
|
| this.#exited = false
|
| this.#exitErrorMessage = false
|
| }
|
|
|
| #handleProcessExit (code) {
|
| const numCode = Number(code) || 0
|
|
|
| const exitCode = this.#exited ? numCode : (numCode || 1)
|
| this.#process.exitCode = exitCode
|
|
|
| if (this.#notLoadedOrExited) {
|
|
|
| this.#logConsoleError(new Error(`Process exited unexpectedly with code: ${exitCode}`))
|
| return
|
| }
|
|
|
| if (this.#logNoNpmError()) {
|
| return
|
| }
|
|
|
| const os = require('node:os')
|
| log.verbose('cwd', this.#process.cwd())
|
| log.verbose('os', `${os.type()} ${os.release()}`)
|
| log.verbose('node', this.#process.version)
|
| log.verbose('npm ', `v${this.#npm.version}`)
|
|
|
|
|
| if (typeof this.#npm.updateNotification === 'string') {
|
| log.notice('', this.#npm.updateNotification, { [META]: true, force: true })
|
| }
|
|
|
| if (!this.#exited) {
|
| log.error('', 'Exit handler never called!')
|
| log.error('', 'This is an error with npm itself. Please report this error at:')
|
| log.error('', ' <https://github.com/npm/cli/issues>')
|
| if (this.#npm.silent) {
|
| output.error('')
|
| }
|
| }
|
|
|
| log.verbose('exit', exitCode)
|
|
|
| if (exitCode) {
|
| log.verbose('code', exitCode)
|
| } else {
|
| log.info('ok')
|
| }
|
|
|
| if (this.#showExitErrorMessage) {
|
| log.error('', this.#npm.exitErrorMessage())
|
| }
|
| }
|
|
|
| #logConsoleError (err) {
|
|
|
|
|
| const { summary, detail } = errorMessage(err, this.#npm)
|
| const formatted = [...new Set([...summary, ...detail].flat().filter(Boolean))].join('\n')
|
|
|
|
|
| console.error(formatted === err.message ? err.stack : formatted)
|
| }
|
|
|
| #logNoNpmError (err) {
|
| if (this.#hasNpm) {
|
| return false
|
| }
|
|
|
| if (!this.#noNpmError) {
|
| this.#noNpmError = true
|
| this.#logConsoleError(
|
| new Error(`Exit prior to setting npm in exit handler`, err ? { cause: err } : {})
|
| )
|
| }
|
| return true
|
| }
|
|
|
| #handleExit = (err) => {
|
| this.#exited = true
|
|
|
|
|
| if (this.#logNoNpmError(err)) {
|
| return this.#process.exit(this.#process.exitCode || getExitCodeFromError(err) || 1)
|
| }
|
|
|
|
|
| if (!this.#loaded) {
|
| this.#logConsoleError(new Error('Exit prior to config file resolving', { cause: err }))
|
| return this.#process.exit(this.#process.exitCode || getExitCodeFromError(err) || 1)
|
| }
|
|
|
| this.#exitErrorMessage = err?.suppressError === true ? false : !!err
|
|
|
|
|
|
|
| const exitCode = err?.exitCode ?? this.#process.exitCode ?? (err ? 1 : undefined)
|
|
|
|
|
|
|
| this.#process.stderr.write('', () => this.#process.stdout.write('', () => {
|
| this.#process.exit(exitCode)
|
| }))
|
| }
|
| }
|
|
|
| module.exports = ExitHandler
|
|
|