Failed Conditions
Push — master ( 01204b...ec55bf )
by Yo
01:43
created

index.js   A

Complexity

Total Complexity 10
Complexity/F 1.67

Size

Lines of Code 59
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 4
dl 0
loc 59
rs 10
wmc 10
mnd 1
bc 8
fnc 6
bpm 1.3333
cpm 1.6666
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A ➔ ??? 0 7 3
1
"use strict";
2
3
if (['dev', 'prod'].indexOf(process.env.NODE_ENV)) {
4
    console.warn(`Undefined node environment "${process.env.NODE_ENV}", fallback to dev !`);
5
    process.env.NODE_ENV = 'dev';
6
}
7
8
const onDeath = require('death')({
9
    SIGTERM: false,
10
    SIGQUIT: false
11
});
12
13
const logger = require('./lib/logger');
14
const taskLoggerFactory = require('./lib/logger/taskLogger');
15
const server = require('./lib/server');
16
const pkg = require('./package.json');
17
const NestedError = require('nested-error-stacks');
18
19
const appName = pkg.name;
20
const taskLogger = taskLoggerFactory(appName);
21
22
onDeath(signal => {
23
    let exitCode = 0;
24
    if (signal === 'SIGTERM' || signal === 'SIGQUIT') {
25
        exitCode = 1;
26
    }
27
    cleanAndExit(exitCode);
28
});
29
30
process.on('unhandledRejection', (reason, p) => {
31
    console.warn('Unhandled Rejection at: Promise', p, 'reason:', reason);
32
33
    return cleanAndExit(1);
34
});
35
36
const cleanAndExit = (exitCode = 0) => {
37
    taskLogger.stopping();
38
    const _exit = error => {
39
        if (error instanceof Error) {
40
            console.error(error);
41
        }
42
        taskLogger.stopped();
43
        process.exit(exitCode);
44
    };
45
46
    return server.stop()
47
        .then(_exit, _exit);
48
};
49
50
taskLogger.starting();
51
52
server.start()
53
    .then(() => taskLogger.started())
54
    .catch(error => {
55
        const newError = new NestedError(`Exit ${appName} after an error at initialisation`, error);
56
        logger.error(newError.stack);
57
58
        return Promise.reject(cleanAndExit(1));
59
    });
60