const logger = require('./libs/logger.js'); const util = require('./libs/util.js'); const cli = require('./libs/cli.js'); const server = require('./libs/server.js'); const blinkstick = require('./libs/blinkstick.js'); const packageJSON = require('./package'); const INTERRUPTS = ['SIGINT', 'SIGTERM']; global.config = undefined; global.appName = packageJSON.name; global.appVersion = packageJSON.version; // handle interrupts handleInterrupts(); // start the application main() .catch(util.exit); async function main() { await setGlobalConfig(); logger.initialize(); logger.info(global.appName + ' ' + global.appVersion + ' starting...'); if (await cli.handleArguments()) { exit(); } await blinkstick.findBlinkstick(); logger.info(await server.start()); server.handleRequests(); } async function setGlobalConfig() { let config = __dirname + '/config.json'; let index = process.argv.indexOf(cli.ARG_CONFIG); if (index >= 0 && process.argv.length >= index + 1) { config = process.argv[index + 1]; } config = await util.fileExists(config); global.config = require(config.path); global.config.path = config.path; } function handleInterrupts() { for (let index = 0; index < INTERRUPTS.length; index++) { process.once(INTERRUPTS[index], (code) => { util.exit(undefined, code); }); } }