Issues (208)

index.js (1 issue)

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) => {
24
    let canExit = false;
25
    const waitAndExit = arg => {
26
27
        if (arg instanceof Error) {
28
            exitCode = 1;
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;
42
        }
43
        return alfred.sleep()
44
    };
45
    const updateCleanState = arg => {
46
        if (arg instanceof Error) {
47
            exitCode = 1;
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);
64
        logger.error(error.stack);
65
        cleanAndExit(1);
66
    });
67