mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
readd vitest and Asset fn
This commit is contained in:
@@ -19,8 +19,8 @@
|
||||
"build:prod": "tsc",
|
||||
"prepare": "tsc",
|
||||
"pretty": "prettier --write .",
|
||||
"tdd": "npx --yes vitest",
|
||||
"test": "npx --yes vitest --run",
|
||||
"tdd": "vitest",
|
||||
"test": "vitest --run",
|
||||
"analyze-imports": "npx depcruise src --include-only \"^src\" --output-type dot | dot -T svg > dependency-graph.svg"
|
||||
},
|
||||
"keywords": [
|
||||
@@ -48,7 +48,8 @@
|
||||
"@typescript-eslint/parser": "5.59.1",
|
||||
"discord.js": "^14.x.x",
|
||||
"eslint": "8.39.0",
|
||||
"typescript": "5.0.2"
|
||||
"typescript": "5.0.2",
|
||||
"vitest": "^1.6.0"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"parser": "@typescript-eslint/parser",
|
||||
|
||||
@@ -191,6 +191,7 @@ export function createResultResolver<Output>(config: {
|
||||
|
||||
export async function callInitPlugins(module: Module, deps: Dependencies, emit?: boolean ) {
|
||||
let _module = module;
|
||||
const emitter = deps['@sern/emitter'];
|
||||
for(const plugin of _module.plugins ?? []) {
|
||||
const res = await plugin.execute({
|
||||
module, absPath: _module.meta.absPath,
|
||||
@@ -202,9 +203,10 @@ export async function callInitPlugins(module: Module, deps: Dependencies, emit?:
|
||||
});
|
||||
if(res.isErr()) {
|
||||
if(emit) {
|
||||
deps['@sern/emitter']?.emit('module.register', resultPayload('failure', module, SernError.PluginFailure));
|
||||
emitter?.emit('module.register',
|
||||
resultPayload('failure', module, res.error ?? SernError.PluginFailure));
|
||||
}
|
||||
throw Error("Plugin failed with controller.stop()");
|
||||
throw Error(res.error ?? SernError.PluginFailure);
|
||||
}
|
||||
}
|
||||
return _module
|
||||
@@ -218,7 +220,7 @@ async function callPlugins({ args, module, deps, params }: ExecutePayload) {
|
||||
return result;
|
||||
}
|
||||
if(typeof result.value === 'object' && result.value !== null) {
|
||||
state = { ...result.value, ...state };
|
||||
state = { ...state, ...result.value, };
|
||||
}
|
||||
}
|
||||
return Ok(state);
|
||||
|
||||
18
src/index.ts
18
src/index.ts
@@ -1,3 +1,5 @@
|
||||
import fs from 'node:fs/promises'
|
||||
import path from 'node:path'
|
||||
export * as Sern from './sern';
|
||||
|
||||
export type {
|
||||
@@ -24,10 +26,10 @@ export type {
|
||||
SernOptionsData,
|
||||
SernSubCommandData,
|
||||
SernSubCommandGroupData,
|
||||
SDT
|
||||
} from './types/core-modules';
|
||||
|
||||
export type {
|
||||
Controller,
|
||||
PluginResult,
|
||||
InitPlugin,
|
||||
ControlPlugin,
|
||||
@@ -47,7 +49,21 @@ export {
|
||||
|
||||
export * from './core/presences'
|
||||
export * from './core/interfaces'
|
||||
import type { controller } from './core/create-plugins';
|
||||
export type Controller = typeof controller
|
||||
export * from './core/create-plugins';
|
||||
export { CommandType, PluginType, PayloadType, EventType } from './core/structures/enums';
|
||||
export { Context } from './core/structures/context';
|
||||
export * from './core/ioc';
|
||||
|
||||
|
||||
export async function Asset(p: string) {
|
||||
const assetsDir = path.resolve('assets');
|
||||
if (path.isAbsolute(p)) {
|
||||
const relativePath = path.relative(assetsDir, "assets"+p);
|
||||
return fs.readFile(path.join(assetsDir, relativePath), 'utf8');
|
||||
}
|
||||
return fs.readFile(path.join(assetsDir, p), 'utf8');
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* Plugins are reminiscent of middleware in express.
|
||||
*/
|
||||
|
||||
import type { Err, Ok, Result } from 'ts-results-es';
|
||||
import type { Result } from 'ts-results-es';
|
||||
import type {
|
||||
Module,
|
||||
Processed,
|
||||
@@ -32,7 +32,7 @@ import type {
|
||||
UserSelectMenuInteraction,
|
||||
} from 'discord.js';
|
||||
|
||||
export type PluginResult = Awaitable<Result<unknown, unknown>>;
|
||||
export type PluginResult = Awaitable<Result<Record<string,unknown>|undefined, string|undefined>>;
|
||||
|
||||
export interface InitArgs<T extends Processed<Module> = Processed<Module>> {
|
||||
module: T;
|
||||
@@ -40,10 +40,6 @@ export interface InitArgs<T extends Processed<Module> = Processed<Module>> {
|
||||
deps: Dependencies
|
||||
updateModule: (module: Partial<T>) => T
|
||||
}
|
||||
export interface Controller {
|
||||
next: () => Ok<unknown>;
|
||||
stop: () => Err<string|undefined>;
|
||||
}
|
||||
export interface Plugin<Args extends any[] = any[]> {
|
||||
type: PluginType;
|
||||
execute: (...args: Args) => PluginResult;
|
||||
|
||||
Reference in New Issue
Block a user