2018-10-25 07:43:42 +00:00
|
|
|
import consola from 'consola'
|
2019-02-06 19:23:42 +00:00
|
|
|
import exit from 'exit'
|
|
|
|
import { fatalBox } from './utils/formatting'
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
let _setup = false
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export default function setup ({ dev }) {
|
2018-10-25 07:43:42 +00:00
|
|
|
// Apply default NODE_ENV if not provided
|
|
|
|
if (!process.env.NODE_ENV) {
|
|
|
|
process.env.NODE_ENV = dev ? 'development' : 'production'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_setup) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_setup = true
|
|
|
|
|
|
|
|
// Global error handler
|
|
|
|
/* istanbul ignore next */
|
|
|
|
process.on('unhandledRejection', (err) => {
|
|
|
|
consola.error(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Exit process on fatal errors
|
|
|
|
/* istanbul ignore next */
|
2018-11-01 03:50:07 +00:00
|
|
|
consola.addReporter({
|
2019-07-10 10:45:49 +00:00
|
|
|
log (logObj) {
|
2018-10-25 07:43:42 +00:00
|
|
|
if (logObj.type === 'fatal') {
|
2019-02-06 19:23:42 +00:00
|
|
|
const errorMessage = String(logObj.args[0])
|
|
|
|
process.stderr.write(fatalBox(errorMessage))
|
|
|
|
exit(1)
|
2018-10-25 07:43:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2018-11-08 09:15:56 +00:00
|
|
|
|
|
|
|
// Wrap all console logs with consola for better DX
|
|
|
|
consola.wrapConsole()
|
2018-10-25 07:43:42 +00:00
|
|
|
}
|