summary refs log tree commit diff
path: root/src/util/plugin/PluginConfig.ts
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2022-08-13 17:16:23 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2022-09-04 10:48:54 +0200
commit8769c7625cafd14e6f304601cc99a195e833d38b (patch)
tree94d37b1ef271490a032cde0605e1b1fb0fe01e0c /src/util/plugin/PluginConfig.ts
parentPlugins finally load! (diff)
downloadserver-8769c7625cafd14e6f304601cc99a195e833d38b.tar.xz
Fix config table, add plugin events, implement onPreMessageEvent, add
http error data field, migrations
Diffstat (limited to 'src/util/plugin/PluginConfig.ts')
-rw-r--r--src/util/plugin/PluginConfig.ts30
1 files changed, 18 insertions, 12 deletions
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<any> { - 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) => {