summary refs log tree commit diff
path: root/src/util/plugin/PluginLoader.ts
blob: e69cb4998ce0c39ff548761e6787da3b8584d578 (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
import path from "path";
import fs from "fs";
import { Plugin, PluginManifest } from "./";

const root = process.env.PLUGIN_LOCATION || "dist/plugins";

let pluginsLoaded = false;
export class PluginLoader {
	public static loadPlugins() {
		console.log(`Plugin root directory: ${path.resolve(root)}`);
		const dirs = fs.readdirSync(root).filter((x) => {
			try {
				fs.readdirSync(path.join(root, x));
				return true;
			} catch (e) {
				return false;
			}
		});
		console.log(dirs);
		dirs.forEach(async (x) => {
			let modPath = path.resolve(path.join(root, x));
			console.log(`Trying to load plugin: ${modPath}`);
			const manifest = require(path.join(modPath, "plugin.json")) as PluginManifest;
			console.log(
				`Plugin info: ${manifest.name} (${manifest.id}), written by ${manifest.authors}, available at ${manifest.repository}`
			);
			const module_ = require(path.join(modPath, manifest.index)) as Plugin;
			try {
				await module_.init();
				module_.emit("loaded");
			} catch (error) {
				module_.emit("error", error);
			}
		});

		//
		//module_.pluginPath =
	}
}