diff --git a/.vscode/launch.json b/.vscode/launch.json index 68c6e51..5f9654c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,8 @@ "skipFiles": [ "/**" ], - "program": "${workspaceFolder}/ninwa.js" + "program": "${workspaceFolder}/ninwa.js", + "args": [] } ] } \ No newline at end of file diff --git a/libs/logger.js b/libs/logger.js index d875147..10d16d2 100644 --- a/libs/logger.js +++ b/libs/logger.js @@ -1,4 +1,3 @@ -const config = require('../config.json'); const moment = require('moment'); // constants @@ -11,12 +10,26 @@ const LOGLEVEL_INFO = 1; const LOGLEVEL_WARNING = 2; const LOGLEVEL_ERROR = 3; -const loglevel = getLogLevel(); -const timestamp = getTimestamp(); +var loglevel = getLogLevel(); +var timestamp = getTimestamp(); + +function initialize() { + return new Promise((resolve, reject) => { + if (global.config == undefined) { + reject('could not initialize logger, config is undefined'); + } + loglevel = getLogLevel(); + timestamp = getTimestamp(); + resolve(); + }); +} // get the loglevel function getLogLevel() { - switch (config.log.level) { + if (global.config?.log?.level == undefined) { + return LOGLEVEL_INFO; + } + switch (global.config.log.level) { case LOG_PREFIX_DEBUG: case LOGLEVEL_DEBUG: return LOGLEVEL_DEBUG; @@ -36,8 +49,8 @@ function getLogLevel() { // get the timestamp format function getTimestamp() { - if (config != undefined && config.log != undefined && config.log.format != undefined) { - return config.log.timestamp; + if (global.config?.log?.format != undefined) { + return global.config.log.timestamp; } return "DD.MM.YYYY HH:mm:ss:SS"; } @@ -108,6 +121,7 @@ function trace(message, prefix) { // exports module.exports = { + initialize, info, warn, debug, diff --git a/libs/watchers.js b/libs/watchers.js index 0ffd5c6..bf5ab92 100644 --- a/libs/watchers.js +++ b/libs/watchers.js @@ -3,18 +3,17 @@ const Watcher = require('./watcher.js'); const watchers = []; -function initialize(config) { +function initialize() { return new Promise(function (resolve, reject) { - if (config == undefined || config.path == undefined) { - reject('no config defined'); + if (global.config == undefined) { + reject('could not initialize watchers, no config defined'); } - config = require(config.path); - if (config.watchers == undefined || config.watchers.length == 0) { - reject('no watchers in config defined'); + if (global.config.watchers == undefined || global.config.watchers.length == 0) { + reject('no watchers in config \'' + global.config.path + '\' defined'); } var tmp = [] - for (var index = 0; index < config.watchers.length; index++) { - tmp.push(new Watcher(config.watchers[index], watcherCallback)); + for (var index = 0; index < global.config.watchers.length; index++) { + tmp.push(new Watcher(global.config.watchers[index], watcherCallback)); } Promise.all(tmp.map(check)) .then(resolve) diff --git a/ninwa.js b/ninwa.js index af313b2..b911ea5 100644 --- a/ninwa.js +++ b/ninwa.js @@ -5,13 +5,24 @@ const packageJSON = require('./package.json'); const INTERRUPTS = ['SIGINT', 'SIGTERM']; -logger.info(packageJSON.name + ' ' + packageJSON.version + ' starting...'); +global.config = process.argv[2] || __dirname + '/config.json'; handleInterrupts(); -const config = process.argv[2] || __dirname + '/config.json'; - 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(packageJSON.name + ' ' + packageJSON.version + ' starting...'); + }) .then(watchers.initialize) .then(watchers.start) .catch((err) => {