*refactor to handle invalid plugins

This commit is contained in:
Derrick Hammer 2023-04-19 00:01:14 -04:00
parent 21887df639
commit a0504443e6
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 14 additions and 3 deletions

View File

@ -147,22 +147,33 @@ export class PluginAPIManager {
} }
let plugin: Plugin; let plugin: Plugin;
let pluginPath = paths.shift();
try { try {
plugin = require(paths.shift() as string) as Plugin; plugin = require(pluginPath as string) as Plugin;
} catch (e) { } catch (e) {
throw e; throw e;
} }
log.debug("Loaded plugin %s", moduleName); log.debug("Loaded plugin %s", moduleName);
return this.loadPluginInstance(plugin); const instance = await this.loadPluginInstance(plugin);
if (!instance) {
throw new Error(`Corrupt plugin found at ${pluginPath}`);
} }
public async loadPluginInstance(plugin: Plugin): Promise<Plugin> { return instance as Plugin;
}
public async loadPluginInstance(plugin: Plugin): Promise<Plugin | boolean> {
if ("default" in plugin) { if ("default" in plugin) {
plugin = plugin?.default as Plugin; plugin = plugin?.default as Plugin;
} }
if (!("name" in plugin)) {
return false;
}
plugin.name = sanitizeName(plugin.name); plugin.name = sanitizeName(plugin.name);
this.registeredPlugins.set(plugin.name, plugin); this.registeredPlugins.set(plugin.name, plugin);