mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
fix class based module loading
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -32,6 +32,7 @@ import { Args, SlashOptions } from '../../shared';
|
||||
export interface CommandMeta {
|
||||
fullPath: string;
|
||||
id: string;
|
||||
isClass: boolean
|
||||
}
|
||||
|
||||
export type AnyDefinedModule = Processed<CommandModule | EventModule>;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)}`,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user