Skip to content

Commit

Permalink
readd vitest and Asset fn
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed May 27, 2024
1 parent fb418c0 commit 792015a
Show file tree
Hide file tree
Showing 5 changed files with 1,811 additions and 69 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions src/handlers/event-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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);
Expand Down
18 changes: 17 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import fs from 'node:fs/promises'
import path from 'node:path'
export * as Sern from './sern';

export type {
Expand All @@ -24,10 +26,10 @@ export type {
SernOptionsData,
SernSubCommandData,
SernSubCommandGroupData,
SDT
} from './types/core-modules';

export type {
Controller,
PluginResult,
InitPlugin,
ControlPlugin,
Expand All @@ -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');
}


8 changes: 2 additions & 6 deletions src/types/core-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -32,18 +32,14 @@ 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;
absPath: string;
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;
Expand Down
Loading

0 comments on commit 792015a

Please sign in to comment.