ninwa/ninwa.js

48 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'];
let config;
main();
async function main() {
handleInterrupts();
try {
let configFile = await util.getFileInfo(process.argv[2] || __dirname + '/config.json');
config = require(configFile.path);
config.path = config.path;
} catch (err) {
console.error(err);
process.exit(1);
}
try {
logger.initialize(config.log.level, config.log.timestamp);
logger.info(packageJSON.name + ' ' + packageJSON.version + ' starting...');
await watchers.initialize(config.watchers);
await watchers.start();
} catch (err) {
logger.error(err);
exit(1);
}
}
function handleInterrupts() {
for (var index = 0; index < INTERRUPTS.length; index++) {
process.on(INTERRUPTS[index], (code) => {
exit(code);
watchers.stop()
.then(exit(code))
.catch(exit(code));
});
}
}
function exit(code) {
code = code || 0;
logger.info(packageJSON.name + ' ' + packageJSON.version + ' exiting with code \'' + code + '\'...');
process.exit(code);
}