From 8769c7625cafd14e6f304601cc99a195e833d38b Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Sat, 13 Aug 2022 17:16:23 +0200 Subject: Fix config table, add plugin events, implement onPreMessageEvent, add http error data field, migrations --- src/util/plugin/PluginConfig.ts | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'src/util/plugin/PluginConfig.ts') diff --git a/src/util/plugin/PluginConfig.ts b/src/util/plugin/PluginConfig.ts index c7a7db87..883bca7c 100644 --- a/src/util/plugin/PluginConfig.ts +++ b/src/util/plugin/PluginConfig.ts @@ -1,26 +1,26 @@ -import { ConfigEntity } from "../entities/Config"; import fs from "fs"; import { OrmUtils, Environment } from ".."; +import { PluginConfigEntity } from "util/entities/PluginConfig"; // TODO: yaml instead of json const overridePath = process.env.PLUGIN_CONFIG_PATH ?? ""; let config: any; -let pairs: ConfigEntity[]; +let pairs: PluginConfigEntity[]; // TODO: use events to inform about config updates // Config keys are separated with _ -export const Config = { +export const PluginConfig = { init: async function init() { if (config) return config; - console.log('[Config] Loading configuration...') - pairs = await ConfigEntity.find(); + console.log('[PluginConfig] Loading configuration...') + pairs = await PluginConfigEntity.find(); config = pairsToConfig(pairs); //config = (config || {}).merge(new ConfigValue()); //config = OrmUtils.mergeDeep(new ConfigValue(), config) - if(process.env.CONFIG_PATH) + if(process.env.PLUGIN_CONFIG_PATH) try { const overrideConfig = JSON.parse(fs.readFileSync(overridePath, { encoding: "utf8" })); config = overrideConfig.merge(config); @@ -48,26 +48,32 @@ export const Config = { function applyConfig(val: any) { async function apply(obj: any, key = ""): Promise { - if (typeof obj === "object" && obj !== null) + if (typeof obj === "object" && obj !== null && !(obj instanceof Date)) return Promise.all(Object.keys(obj).map((k) => apply(obj[k], key ? `${key}_${k}` : k))); let pair = pairs.find((x) => x.key === key); - if (!pair) pair = new ConfigEntity(); + if (!pair) pair = new PluginConfigEntity(); pair.key = key; pair.value = obj; - return pair.save(); + if(!pair.key || pair.key == null) { + console.log(`[PluginConfig] WARN: Empty key`) + console.log(pair); + if(Environment.isDebug) debugger; + } + else + return pair.save(); } - if(process.env.CONFIG_PATH) { + if(process.env.PLUGIN_CONFIG_PATH) { if(Environment.isDebug) - console.log(`Writing config: ${process.env.CONFIG_PATH}`) + console.log(`Writing config: ${process.env.PLUGIN_CONFIG_PATH}`) fs.writeFileSync(overridePath, JSON.stringify(val, null, 4)); } return apply(val); } -function pairsToConfig(pairs: ConfigEntity[]) { +function pairsToConfig(pairs: PluginConfigEntity[]) { let value: any = {}; pairs.forEach((p) => { -- cgit 1.5.1