-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
57 lines (47 loc) · 1.11 KB
/
index.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
57
'use strict'
/**
* Don't allow multiple copies of this module
* because that would mess everything up.
*/
if (global.EXIT_THEN_1) {
console.error('Another copy of `exit-then` was found. Please `npm dedupe`.')
module.exports = global.EXIT_THEN_1
return
}
const Promise = require('native-or-bluebird')
const timeout = require('timeout-then')
const onExit = require('signal-exit')
const fns = module.exports = []
/**
* Make a global version based on version.
*/
global.EXIT_THEN_1 = {
fns: fns,
onexit: onexit
}
/**
* Custom minimum timeout interval.
*/
const EXIT_TIMEOUT = parseInt(process.env.EXIT_TIMEOUT, 0) || 0
if (EXIT_TIMEOUT) fns.push(function () {
return timeout(EXIT_TIMEOUT)
})
onExit(onexit)
process.on('uncaughtException', function (err) {
console.error(err.stack)
onexit(err)
})
let exited = false
function onexit(err) {
if (exited) return
exited = true
if (!(err instanceof Error)) err = null
Promise.all(fns.map(function (fn) {
return fn(err)
})).then(function () {
process.exit(0)
}).catch(function (err) {
console.error(err.stack)
process.exit(1)
})
}