From 95d5375cc52eb8ec297a6a456f3eb734039aa834 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kr=C3=BCger?= Date: Mon, 9 Jan 2023 23:54:13 +0100 Subject: [PATCH] automatic reload for aliases This automatically reloads the aliases file using watchFile Also prints the error in the log if it can't be loaded and creates an empty aliases file if it doesn't exist already --- modules/receiver/aliases.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/modules/receiver/aliases.js b/modules/receiver/aliases.js index 7693913..3e10bee 100644 --- a/modules/receiver/aliases.js +++ b/modules/receiver/aliases.js @@ -32,13 +32,25 @@ module.exports = function(receiverId, configData, api) { let aliases = {} - try { - aliases = hjson.parse(fs.readFileSync(config.file, 'utf8')) - } catch (err) { - console.warn('alias file "' + config.file + '" doesn\'t exist, using empty') + if (!fs.existsSync(config.file)) { + console.log('creating empty %o', config.file) + fs.writeFileSync(config.file, '{}') } - _.forEach(aliases, function(n, k) { - api.receiverCallback(k, n, receiverId) - }) + function doLoad() { + console.log('reloading %o', config.file) + try { + aliases = hjson.parse(fs.readFileSync(config.file, 'utf8')) + } catch (err) { + console.warn('couldn\'t read %o - %o - using empty or previous', config.file, String(err)) + } + + _.forEach(aliases, function(n, k) { + api.receiverCallback(k, n, receiverId) + }) + } + + doLoad() + + fs.watchFile(config.file, doLoad) }