ninwa/ninwa.js

49 lines
1.3 KiB
JavaScript

const watchers = require('./libs/watchers.js');
const util = require('./libs/util.js');
const logger = require('./libs/logger.js');
const packageJSON = require('./package.json');
const INTERRUPTS = ['SIGINT', 'SIGTERM'];
global.appName = packageJSON.name;
global.appVersion = packageJSON.version;
global.config = process.argv[2] || __dirname + '/config.json';
handleInterrupts();
util.fileExists(config)
.catch((err) => {
logger.error('given config file \'' + config + '\' does not exist');
logger.error(err);
exit(1);
})
.then((result) => {
global.config = require(result.path);
global.config.path = result.path;
})
.then(logger.initialize)
.then(() => {
logger.info(appName + ' ' + appVersion + ' starting...');
})
.then(watchers.initialize)
.then(watchers.start)
.catch((err) => {
logger.error(err);
exit(1);
});
function exit(code) {
code = code || 0;
logger.info(appName + ' ' + appVersion + ' exiting with code \'' + code + '\'...');
process.exit(code);
}
function handleInterrupts() {
for (var index = 0; index < INTERRUPTS.length; index++) {
process.once(INTERRUPTS[index], (code) => {
watchers.stop()
.then(exit(code))
.catch(exit(code));
});
}
}