ready handler revamped so much cleaner

This commit is contained in:
Jacob Nguyen
2024-05-14 23:19:57 -05:00
parent 880311f08c
commit a7aea4be1a
20 changed files with 153 additions and 148 deletions

View File

@@ -1,15 +1,7 @@
import type { Result } from 'ts-results-es'
import { CommandType, EventType, Plugin } from '..';
import { AnyFunction } from '../types/utility';
import { Module } from '../types/core-modules';
export * from './functions';
export type _Module = {
meta: {
id: string,
absPath: string
}
name: string,
execute : Function
[key: PropertyKey]: unknown
}
export type VoidResult = Result<void, void>;

View File

@@ -21,6 +21,7 @@ export interface Emitter {
addListener(eventName: string | symbol, listener: AnyFunction): this;
removeListener(eventName: string | symbol, listener: AnyFunction): this;
emit(eventName: string | symbol, ...payload: any[]): boolean;
on(eventName: string | symbol, listener: AnyFunction): this
}

View File

@@ -75,6 +75,8 @@ async function composeRoot(
__add_container('@sern/logger', new __Services.DefaultLogging());
}
__add_container('@sern/errors', new __Services.DefaultErrorHandling());
__add_container('@sern/cron', {})
__add_container('@sern/modules', new Map())
//Build the container based on the callback provided by the user
conf.build(container as Container);
@@ -99,6 +101,7 @@ export async function makeDependencies (conf: ValidDependencyConfig) {
__add_container('@sern/logger', new __Services.DefaultLogging);
}
__add_container('@sern/errors', new __Services.DefaultErrorHandling());
__add_container('@sern/cron', {})
await useContainerRaw().ready();
} else {
await composeRoot(useContainerRaw(), conf);

View File

@@ -4,7 +4,7 @@ import * as __Services from '../structures/default-services';
* A semi-generic container that provides error handling, emitter, and module store.
* For the handler to operate correctly, The only user provided dependency needs to be @sern/client
*/
export function hasCallableMethod(obj: object, name: PropertyKey) {
function hasCallableMethod(obj: object, name: PropertyKey) {
//@ts-ignore
return Object.hasOwn(obj, name) && typeof obj[name] == 'function';
}

View File

@@ -3,7 +3,7 @@ import { existsSync } from 'fs';
import { readdir } from 'fs/promises';
import assert from 'node:assert';
import * as Id from './id'
import type { _Module } from './_internal';
import { Module } from '../types/core-modules';
export const parseCallsite = (site: string) => {
const pathobj = path.parse(site.replace(/file:\\?/, "")
@@ -38,11 +38,11 @@ export const shouldHandle = (pth: string, filenam: string) => {
export async function importModule<T>(absPath: string) {
let fileModule = await import(absPath);
let commandModule: _Module = fileModule.default;
let commandModule: Module = fileModule.default;
assert(commandModule , `No export @ ${absPath}. Forgot to ignore with "!"? (!${path.basename(absPath)})?`);
if ('default' in commandModule) {
commandModule = commandModule.default as _Module;
commandModule = commandModule.default as Module;
}
const p = path.parse(absPath)
commandModule.name ??= p.name; commandModule.description ??= "...";
@@ -51,21 +51,18 @@ export async function importModule<T>(absPath: string) {
id: Id.create(commandModule.name, commandModule.type),
absPath,
};
return { module: commandModule } as T;
return { module: commandModule as T };
}
export async function* readRecursive(dir: string): AsyncGenerator<string> {
const files = await readdir(dir, { withFileTypes: true, recursive: true });
const files = await readdir(dir, { recursive: true, withFileTypes: true });
for (const file of files) {
const fullPath = path.join(file.path, file.name);
const fullPath = path.join(file.parentPath, file.name);
if(!file.name.startsWith('!') && !file.isDirectory()) {
yield fullPath;
}
}
}
export const fmtFileName = (fileName: string) => path.parse(fileName).name;
export const filename = (p: string) => fmtFileName(path.basename(p));

View File

@@ -4,37 +4,35 @@ import type { AnyEventPlugin, } from '../types/core-plugin';
import type {
InputCommand,
InputEvent,
Module,
} from '../types/core-modules';
import { type _Module, partitionPlugins } from './_internal';
import { partitionPlugins } from './functions'
import type { Awaitable } from '../types/utility';
/**
* @since 1.0.0 The wrapper function to define command modules for sern
* @param mod
*/
export function commandModule(mod: InputCommand): _Module {
export function commandModule(mod: InputCommand): Module {
const [onEvent, plugins] = partitionPlugins(mod.plugins);
//@ts-ignore
return {
...mod,
onEvent,
plugins,
};
} as Module;
}
/**
* @since 1.0.0
* The wrapper function to define event modules for sern
* @param mod
*/
export function eventModule(mod: InputEvent): _Module {
export function eventModule(mod: InputEvent): Module {
const [onEvent, plugins] = partitionPlugins(mod.plugins);
//@ts-ignore
return {
...mod,
plugins,
onEvent,
};
} as Module;
}
/** Create event modules from discord.js client events,