From 03936eb2ea1d1af7cada04d77bb8345d63a5e20f Mon Sep 17 00:00:00 2001 From: Arcs <73959934+HighArcs@users.noreply.github.com> Date: Tue, 13 Sep 2022 13:46:06 -0500 Subject: [PATCH] feat: allow constructable modules (#133) * Update readFile.ts * Update userDefinedEventsHandling.ts * Update readyHandler.ts * fix: ts error Co-authored-by: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Co-authored-by: xxDeveloper <77380166+Murtatrxx@users.noreply.github.com> --- src/handler/events/readyHandler.ts | 2 +- src/handler/events/userDefinedEventsHandling.ts | 2 +- src/handler/utilities/readFile.ts | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/handler/events/readyHandler.ts b/src/handler/events/readyHandler.ts index 1a5318e..83e02dc 100644 --- a/src/handler/events/readyHandler.ts +++ b/src/handler/events/readyHandler.ts @@ -87,7 +87,7 @@ export default class ReadyHandler extends EventsHandler<{ type: PluginType.Command; }[]; }) { - if (mod.plugins.length === 0) { + if ((mod.plugins || []).length === 0) { return of({ mod, pluginRes: [] }); } // modules with no event plugins are ignored in the previous diff --git a/src/handler/events/userDefinedEventsHandling.ts b/src/handler/events/userDefinedEventsHandling.ts index fa02698..5293520 100644 --- a/src/handler/events/userDefinedEventsHandling.ts +++ b/src/handler/events/userDefinedEventsHandling.ts @@ -25,7 +25,7 @@ export function processCommandPlugins( wrapper: Wrapper, payload: { mod: T; absPath: string }, ) { - return payload.mod.plugins.map(plug => ({ + return (payload.mod.plugins || []).map(plug => ({ ...plug, name: plug?.name ?? 'Unnamed Plugin', description: plug?.description ?? '...', diff --git a/src/handler/utilities/readFile.ts b/src/handler/utilities/readFile.ts index 1f12eee..47497bf 100644 --- a/src/handler/utilities/readFile.ts +++ b/src/handler/utilities/readFile.ts @@ -73,6 +73,11 @@ export function buildData(commandDir: string): Observable< } catch { mod = (await import(`file:///` + absPath)).default; } + + try { + mod = new (mod as unknown as new (...args: unknown[]) => T)(); + } catch {} + if (mod !== undefined) { return Ok({ mod, absPath }); } else return Err(SernError.UndefinedModule);