summary refs log tree commit diff
path: root/src/util/plugin/PluginConfig.ts
blob: b9c8aef02a2a840558a732e6583c9460ca8328b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import fs from "fs";
import { Environment } from "..";
import { PluginConfigEntity } from "../entities/PluginConfig";

// TODO: yaml instead of json
const overridePath = process.env.PLUGIN_CONFIG_PATH ?? "";

let config: any;
let pairs: PluginConfigEntity[];

// TODO: use events to inform about config updates
// Config keys are separated with _

export const PluginConfig = {
	init: async function init() {
		if (config) return config;
		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.PLUGIN_CONFIG_PATH)
			try {
				const overrideConfig = JSON.parse(fs.readFileSync(overridePath, { encoding: "utf8" }));
				config = overrideConfig.merge(config);
			} catch (error) {
				fs.writeFileSync(overridePath, JSON.stringify(config, null, 4));
			}

		return this.set(config);
	},
	get: function get() {
		if (!config) {
			if (Environment.isDebug)
				console.log(
					"Oops.. trying to get config without config existing... Returning defaults... (Is the database still initialising?)"
				);
			return {};
		}
		return config;
	},
	set: function set(val: Partial<any>) {
		if (!config || !val) return;
		config = val.merge(config);

		return applyConfig(config);
	}
};

function applyConfig(val: any) {
	async function apply(obj: any, key = ""): Promise<any> {
		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 PluginConfigEntity();

		pair.key = key;
		pair.value = obj;
		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.PLUGIN_CONFIG_PATH) {
		if (Environment.isDebug) console.log(`Writing config: ${process.env.PLUGIN_CONFIG_PATH}`);
		fs.writeFileSync(overridePath, JSON.stringify(val, null, 4));
	}

	return apply(val);
}

function pairsToConfig(pairs: PluginConfigEntity[]) {
	let value: any = {};

	pairs.forEach((p) => {
		const keys = p.key.split("_");
		let obj = value;
		let prev = "";
		let prevObj = obj;
		let i = 0;

		for (const key of keys) {
			if (!isNaN(Number(key)) && !prevObj[prev]?.length) prevObj[prev] = obj = [];
			if (i++ === keys.length - 1) obj[key] = p.value;
			else if (!obj[key]) obj[key] = {};

			prev = key;
			prevObj = obj;
			obj = obj[key];
		}
	});

	return value;
}