forked from nodecg/nodecg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (63 loc) · 1.95 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict';
process.title = 'NodeCG';
global.exitOnUncaught = true;
const path = require('path');
const cwd = process.cwd();
global.isZeitPkg = isZeitPkg();
/**
* Lange: As of July 2019, the only way I know to programmatically
* determine if code is running inside a Zeit pkg is to check if
* the first directory in the __dirname is called `snapshot`.
*
* There's definitely a possibility for false positives here,
* but I have yet to find a better alternative.
*/
function isZeitPkg() {
const parts = __dirname.split(path.sep);
return parts[1].toLowerCase() === 'snapshot';
}
if (global.isZeitPkg) {
console.info('[nodecg] Detected that NodeCG is running inside a ZEIT pkg (https://github.com/zeit/pkg)');
} else if (cwd !== __dirname) {
console.warn('[nodecg] process.cwd is %s, expected %s', cwd, __dirname);
process.chdir(__dirname);
console.info('[nodecg] Changed process.cwd to %s', __dirname);
}
if (!process.env.NODECG_ROOT) {
process.env.NODECG_ROOT = process.cwd();
}
const semver = require('semver');
const exitHook = require('exit-hook');
const {engines} = require('./package.json');
const nodeVersion = process.versions.node;
if (!semver.satisfies(nodeVersion, engines.node)) {
console.error(`ERROR: NodeCG requires Node.js ${engines.node}`);
console.error(` Your Node.js version: v${nodeVersion}`);
process.exit(1);
}
process.on('uncaughtException', err => {
if (!global.sentryEnabled) {
if (global.exitOnUncaught) {
console.error('UNCAUGHT EXCEPTION! NodeCG will now exit.');
} else {
console.error('UNCAUGHT EXCEPTION!');
}
console.error(err);
if (global.exitOnUncaught) {
process.exit(1);
}
}
});
process.on('unhandledRejection', err => {
if (!global.sentryEnabled) {
console.error('UNHANDLED PROMISE REJECTION!');
console.error(err);
}
});
const server = require('./lib/server')
.on('error', () => process.exit(1))
.on('stopped', () => process.exit(0));
exitHook(() => {
server.stop();
});
server.start();