Failed Conditions
Push — master ( c3e11c...1d8d9e )
by Yo
02:09 queued 37s
created

index.js   A

Complexity

Total Complexity 12
Complexity/F 1.71

Size

Lines of Code 66
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 2
dl 0
loc 66
rs 10
wmc 12
mnd 1
bc 11
fnc 7
bpm 1.5713
cpm 1.7142
noi 6

1 Function

Rating   Name   Duplication   Size   Complexity  
B ➔ ??? 0 32 1
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 logger = require('./lib/logger');
9
const server = require('./lib/server');
10
const Alfred = require('./lib/Alfred');
11
const Client = require('./lib/Client');
12
const PluginManager = require('./lib/PluginManager');
13
const pkg = require('./package.json');
14
const NestedError = require('nested-error-stacks');
15
16
const appName = pkg.name;
17
18
const alfred = new Alfred(
19
    new Client(),
20
    new PluginManager([])
21
);
22
23
const cleanAndExit = (exitCode = 0) => {
0 ignored issues
show
Unused Code introduced by
The parameter exitCode is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
24
    let canExit = false;
25
    const waitAndExit = arg => {
26
27
        if (arg instanceof Error) {
28
            exitCode = 1;
0 ignored issues
show
Unused Code introduced by
The variable exitCode seems to be never used. Consider removing it.
Loading history...
29
        }
30
31
        if (canExit === false) {
32
            setTimeout(waitAndExit, 1000);
33
        } else {
34
            process.exit();
0 ignored issues
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
35
        }
36
    };
37
38
    const stopAlfred = arg => {
39
40
        if (arg instanceof Error) {
41
            exitCode = 1;
0 ignored issues
show
Unused Code introduced by
The variable exitCode seems to be never used. Consider removing it.
Loading history...
42
        }
43
        return alfred.sleep()
44
    };
45
    const updateCleanState = arg => {
46
        if (arg instanceof Error) {
47
            exitCode = 1;
0 ignored issues
show
Unused Code introduced by
The variable exitCode seems to be never used. Consider removing it.
Loading history...
48
        }
49
        canExit = true;
50
    };
51
    server.stop()
52
        .then(stopAlfred, stopAlfred)
53
        .then(updateCleanState, updateCleanState)
54
};
55
56
process.on('SIGINT', cleanAndExit);
57
58
logger.starting(appName);
59
return alfred.wakeUp()
60
    .then(() => server.start())
61
    .then(() => logger.started(appName))
62
    .catch(error => {
63
        const newError = new NestedError(`Exit ${appName} after an error at initialisation`, error);
0 ignored issues
show
Unused Code introduced by
The constant newError seems to be never used. Consider removing it.
Loading history...
64
        logger.error(error.stack);
65
        cleanAndExit(1);
66
    });
67