Skip to content

Commit

Permalink
Improve exit
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jun 5, 2022
1 parent ee1a19d commit 1293f61
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/bin/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const handleCliError = function (error, opts) {
const errorA = normalizeError(error)
const { silent, short, exitCode } = getOpts(errorA, opts)
printError(errorA, silent, short)
process.exitCode = exitCode
exitProcess(exitCode)
}

const getOpts = function (error, opts = {}) {
Expand Down Expand Up @@ -102,3 +102,15 @@ const printError = function (error, silent, short) {
const errorMessage = short ? error.message : error.stack
console.error(errorMessage)
}

// We use `process.exitCode` instead of `process.exit()` to let any pending
// tasks complete, with a timeout
const exitProcess = function (exitCode) {
process.exitCode = exitCode
setTimeout(() => {
// eslint-disable-next-line unicorn/no-process-exit, n/no-process-exit
process.exit(exitCode)
}, EXIT_TIMEOUT)
}

const EXIT_TIMEOUT = 5e3

0 comments on commit 1293f61

Please sign in to comment.