From e4a9c8d5517481594f5962bd201ff353b689746b Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Thu, 18 May 2023 13:06:40 -0500 Subject: [PATCH] fix class based module loading --- src/core/module-loading.ts | 17 +++++++++-------- src/core/structures/services/module-manager.ts | 1 - src/core/types/modules.ts | 1 + src/handler/commands.ts | 4 +--- src/handler/events/generic.ts | 1 + 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/core/module-loading.ts b/src/core/module-loading.ts index 63d1843..b76b839 100644 --- a/src/core/module-loading.ts +++ b/src/core/module-loading.ts @@ -1,26 +1,27 @@ import { SernError } from './structures/errors'; -import { type Result, Err, Ok } from 'ts-results-es'; +import { Result, Err, Ok } from 'ts-results-es'; import { Module } from './types/modules'; import { type Observable, from, mergeMap, ObservableInput } from 'rxjs'; import { readdir, stat } from 'fs/promises'; import { basename, extname, join, resolve } from 'path'; import { ImportPayload } from '../handler/types'; -import { CommandExecutable, clazz } from '../handler/commands'; export type ModuleResult = Promise, SernError>>; -function isClassModule(m: unknown): m is typeof CommandExecutable { - return m != undefined && Reflect.has(m, clazz); -} export async function importModule(absPath: string) { + // prettier-ignore let module = - /// #if MODE === 'esm' - import(absPath).then(i => i.default); // eslint-disable-line + /// #if MODE === 'esm' + import(absPath).then(i => i.default); // eslint-disable-line /// #elif MODE === 'cjs' require(absPath).default; // eslint-disable-line /// #endif - return module.then(m => (isClassModule(m) ? m.getInstance() : m)) as T; + return module.then(m => + Result + .wrap(() => m.getInstance()) + .unwrapOr(m) + ) as T; } export async function defaultModuleLoader(absPath: string): ModuleResult { let module = await importModule(absPath); diff --git a/src/core/structures/services/module-manager.ts b/src/core/structures/services/module-manager.ts index f9ccd81..53dfcf2 100644 --- a/src/core/structures/services/module-manager.ts +++ b/src/core/structures/services/module-manager.ts @@ -1,4 +1,3 @@ -import { clazz } from '../../../handler/commands'; import { CoreModuleStore, ModuleManager } from '../../contracts'; import { importModule } from '../../module-loading'; import { CommandMeta, CommandModule, Module } from '../../types/modules'; diff --git a/src/core/types/modules.ts b/src/core/types/modules.ts index a62dc3b..9b58eda 100644 --- a/src/core/types/modules.ts +++ b/src/core/types/modules.ts @@ -32,6 +32,7 @@ import { Args, SlashOptions } from '../../shared'; export interface CommandMeta { fullPath: string; id: string; + isClass: boolean } export type AnyDefinedModule = Processed; diff --git a/src/handler/commands.ts b/src/handler/commands.ts index 9f10acc..4a09c7d 100644 --- a/src/handler/commands.ts +++ b/src/handler/commands.ts @@ -18,7 +18,6 @@ import { import { partitionPlugins } from '../core/functions'; import { Awaitable } from '../shared'; -export const clazz = Symbol('@sern/class'); /** * @since 1.0.0 The wrapper function to define command modules for sern * @param mod @@ -79,7 +78,6 @@ export abstract class CommandExecutable { abstract type: Type; plugins: AnyEventPlugin[] = []; - static readonly [clazz] = true; + private static _instance: EventModule; static getInstance() { if (!EventExecutable._instance) { diff --git a/src/handler/events/generic.ts b/src/handler/events/generic.ts index 207a5e9..a39a220 100644 --- a/src/handler/events/generic.ts +++ b/src/handler/events/generic.ts @@ -85,6 +85,7 @@ function assignDefaults( module.name ??= Files.filename(absPath); module.description ??= '...'; moduleManager.setMetadata(module, { + isClass: module.constructor.name === 'Function', fullPath: absPath, id: `${module.name}_${uniqueId(module.type)}`, });