const deepMerge = require('deepmerge')

const defaultConfig = {
  timeout: 10 * 1000,
  messages: {
    start: 'Received termination request. Attempting gracefully exit...',
    error: 'An error occurred whilst attempting to gracefully exit.',
    timeout: 'Graceful shutdown period timeout lapsed, forcefully shutting down.',
  },
  catchUnhandled: true,
}

module.exports = function configureGracefulExit(config = {}) {
  const { onExit, messages, timeout, name, catchUnhandled } = deepMerge(defaultConfig, config)

  if (typeof onExit !== 'function') {
    console.error('You must provide an onExit "function" to configureGracefulExit')
  }

  const frmtMsg = msg => `[${name || process.pid}] ${msg}`

  let exiting = false

  const handleExit = (exitCode) => {
    if (exiting) {
      return
    }
    exiting = true
    try {
      Promise.resolve(onExit).then(() => process.exit(exitCode)).catch((err) => {
        console.error(frmtMsg(messages.error))
        console.error(err)
        process.exit(1)
      })

      setTimeout(() => {
        console.error(frmtMsg(messages.timeout))
        process.exit(1)
      }, timeout)
    } catch (err) {
      console.error(frmtMsg(messages.error))
      console.error(err)
    }
  }

  // Respond to any termination requests
  ;['SIGTERM', 'SIGINT'].forEach((signal) => {
    process.on(signal, () => handleExit(0))
  })

  // Catch any unhandled promise rejections
  if (catchUnhandled) {
    process.on('unhandledRejection', (err) => {
      console.error('An unhandled rejection occurred', err)
      handleExit(1)
    })
  }
}

@@ -320,16 +320,38 @@
 down.',%0A
+    exit: 'Exiting.',%0A
   %7D,%0A  c
@@ -841,17 +841,164 @@
 rue%0A
-    try %7B
+%0A    const exit = (code) =%3E %7B%0A      console.log(frmtMsg(messages.end))%0A      process.exit(code)%0A    %7D%0A%0A    try %7B%0A      console.log(frmtMsg(messages.start))%0A
 %0A   
@@ -1154,32 +1154,24 @@
 rr)%0A        
-process.
 exit(1)%0A    
@@ -1254,32 +1254,24 @@
 t))%0A        
-process.
 exit(1)%0A    
@@ -1370,24 +1370,38 @@
 .error(err)%0A
+      exit(1)%0A
     %7D%0A  %7D%0A%0A 

Add more messaging to the graceful exit helper.