summary refs log tree commit diff
path: root/src/util/connections/ConnectionLoader.ts
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-04-02 11:30:31 +1000
committerGitHub <noreply@github.com>2023-04-02 11:30:31 +1000
commit86ac90b1e4e83cb1f55c45055d9ab3a488fe67bd (patch)
tree83c97c8d7464bbfb0b1924597f1dde8c69528ba9 /src/util/connections/ConnectionLoader.ts
parentRemove ALL fosscord mentions (diff)
parentLess spammy user connection logs (diff)
downloadserver-86ac90b1e4e83cb1f55c45055d9ab3a488fe67bd.tar.xz
Merge pull request #1009 from Puyodead1/refactor/dev/connections
Connections Part 1
Diffstat (limited to 'src/util/connections/ConnectionLoader.ts')
-rw-r--r--src/util/connections/ConnectionLoader.ts68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/util/connections/ConnectionLoader.ts b/src/util/connections/ConnectionLoader.ts
new file mode 100644
index 00000000..b32f77cd
--- /dev/null
+++ b/src/util/connections/ConnectionLoader.ts
@@ -0,0 +1,68 @@
+import fs from "fs";
+import path from "path";
+import Connection from "./Connection";
+import { ConnectionConfig } from "./ConnectionConfig";
+import { ConnectionStore } from "./ConnectionStore";
+
+const root = "dist/connections";
+const connectionsLoaded = false;
+
+export class ConnectionLoader {
+	public static async loadConnections() {
+		if (connectionsLoaded) return;
+		ConnectionConfig.init();
+		const dirs = fs.readdirSync(root).filter((x) => {
+			try {
+				fs.readdirSync(path.join(root, x));
+				return true;
+			} catch (e) {
+				return false;
+			}
+		});
+
+		dirs.forEach(async (x) => {
+			const modPath = path.resolve(path.join(root, x));
+			const mod = new (require(modPath).default)() as Connection;
+			ConnectionStore.connections.set(mod.id, mod);
+
+			mod.init();
+			// console.log(`[Connections] Loaded connection '${mod.id}'`);
+		});
+	}
+
+	// eslint-disable-next-line @typescript-eslint/no-explicit-any
+	public static getConnectionConfig(id: string, defaults?: any): any {
+		let cfg = ConnectionConfig.get()[id];
+		if (defaults) {
+			if (cfg) cfg = Object.assign({}, defaults, cfg);
+			else {
+				cfg = defaults;
+				this.setConnectionConfig(id, cfg);
+			}
+		}
+
+		if (cfg?.enabled) console.log(`[Connections] ${id} enabled`);
+
+		// if (!cfg)
+		// 	console.log(
+		// 		`[ConnectionConfig/WARN] Getting connection settings for '${id}' returned null! (Did you forget to add settings?)`,
+		// 	);
+		return cfg;
+	}
+
+	public static async setConnectionConfig(
+		id: string,
+		// eslint-disable-next-line @typescript-eslint/no-explicit-any
+		config: Partial<any>,
+	): Promise<void> {
+		if (!config)
+			console.warn(`[Connections/WARN] ${id} tried to set config=null!`);
+
+		await ConnectionConfig.set({
+			[id]: Object.assign(
+				config,
+				ConnectionLoader.getConnectionConfig(id) || {},
+			),
+		});
+	}
+}