ninwa/ninwa.js

47 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-02-15 04:33:19 +01:00
const watchers = require('./libs/watchers.js');
const util = require('./libs/util.js');
2022-02-15 04:33:19 +01:00
const logger = require('./libs/logger.js');
const packageJSON = require('./package.json');
const INTERRUPTS = ['SIGINT', 'SIGTERM'];
main();
2022-02-15 04:33:19 +01:00
async function main() {
handleInterrupts();
try {
let configFile = await util.getFileInfo(process.argv[2] || __dirname + '/config.json');
2022-03-28 16:08:33 +02:00
global.config = require(configFile.path);
global.config.path = configFile.path;
} catch (err) {
console.error(err);
process.exit(1);
}
try {
2022-03-28 16:08:33 +02:00
logger.initialize(global.config.log.level, global.config.log.timestamp);
logger.info(packageJSON.name + ' ' + packageJSON.version + ' starting...');
2022-03-28 16:08:33 +02:00
await watchers.initialize(global.config.watchers);
await watchers.start();
} catch (err) {
2022-02-15 04:33:19 +01:00
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);
}