fix class based module loading

This commit is contained in:
Jacob Nguyen
2023-05-18 13:06:40 -05:00
parent 59a13765a5
commit e4a9c8d551
5 changed files with 12 additions and 12 deletions

View File

@@ -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<T> = Promise<Result<ImportPayload<T>, SernError>>;
function isClassModule(m: unknown): m is typeof CommandExecutable {
return m != undefined && Reflect.has(m, clazz);
}
export async function importModule<T>(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<T extends Module>(absPath: string): ModuleResult<T> {
let module = await importModule<T>(absPath);

View File

@@ -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';

View File

@@ -32,6 +32,7 @@ import { Args, SlashOptions } from '../../shared';
export interface CommandMeta {
fullPath: string;
id: string;
isClass: boolean
}
export type AnyDefinedModule = Processed<CommandModule | EventModule>;

View File

@@ -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<const Type extends CommandType = Command
abstract type: Type;
plugins: AnyCommandPlugin[] = [];
private static _instance: CommandModule;
static readonly [clazz] = true;
static getInstance() {
if (!CommandExecutable._instance) {
@@ -100,7 +98,7 @@ export abstract class CommandExecutable<const Type extends CommandType = Command
export abstract class EventExecutable<Type extends EventType> {
abstract type: Type;
plugins: AnyEventPlugin[] = [];
static readonly [clazz] = true;
private static _instance: EventModule;
static getInstance() {
if (!EventExecutable._instance) {

View File

@@ -85,6 +85,7 @@ function assignDefaults<T extends Module>(
module.name ??= Files.filename(absPath);
module.description ??= '...';
moduleManager.setMetadata(module, {
isClass: module.constructor.name === 'Function',
fullPath: absPath,
id: `${module.name}_${uniqueId(module.type)}`,
});