forked from fastify/fastify-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfork.js
56 lines (46 loc) · 1.12 KB
/
fork.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict'
const chalk = require('chalk')
const { stop, runFastify } = require('../../start')
const {
GRACEFUL_SHUT,
READY,
TIMEOUT
} = require('./constants.js')
let fastify
function exit () {
if (this) { console.log(chalk.red(this.message)) }
process.exit(1)
}
process.on('message', function (event) {
if (event === GRACEFUL_SHUT) {
const message = chalk.red('[fastify-cli] process forced end')
setTimeout(exit.bind({ message }), TIMEOUT).unref()
if (fastify) {
fastify.close(() => {
process.exit(0)
})
} else {
process.exit(0)
}
}
})
process.on('uncaughtException', (err) => {
console.log(chalk.red(err))
const message = chalk.red('[fastify-cli] app crashed - waiting for file changes before starting...')
exit.bind({ message })()
})
const main = async () => {
fastify = await runFastify(process.argv.splice(2))
const type = process.env.childEvent
process.send({ type: type, err: null })
try {
await fastify.ready()
process.send({ type: READY })
} catch (err) {
stop(err)
}
}
main().catch((err) => {
console.error(err)
process.exit(1)
})