cleanup tests, codegen, and importing handler

This commit is contained in:
jacob
2024-05-02 17:27:57 -05:00
parent d3245c8a0c
commit d7ebdb2edc
10 changed files with 30 additions and 103 deletions

View File

@@ -1,40 +1,4 @@
import type { Interaction } from 'discord.js';
import { mergeMap, merge, concatMap } from 'rxjs';
import { PayloadType } from './core/structures/enums';
import { shouldHandle } from './core/module-loading'
import {
isAutocomplete,
isCommand,
isMessageComponent,
isModal,
sharedEventStream,
SernError,
filterTap,
resultPayload,
type _Module,
} from './core/_internal';
import { createInteractionHandler, executeModule, makeModuleExecutor } from './handlers/event-utils';
import type { Emitter, ErrorHandling, Logging } from './core/interfaces'
function interactionHandler(client: Emitter,
emitter: Emitter,
log: Logging,
err: ErrorHandling,
modules: Map<string, _Module>) {
const interactionStream$ = sharedEventStream<Interaction>(client, 'interactionCreate');
const handle = createInteractionHandler(interactionStream$, modules);
const interactionHandler$ = merge(handle(isMessageComponent),
handle(isAutocomplete),
handle(isCommand),
handle(isModal));
return interactionHandler$
.pipe(filterTap(e => emitter.emit('warning', resultPayload(PayloadType.Warning, undefined, e))),
concatMap(makeModuleExecutor(module =>
emitter.emit('module.activate', resultPayload(PayloadType.Failure, module, SernError.PluginFailure)))),
mergeMap(payload => executeModule(emitter, log, err, payload)));
}
export const __start = (entryPoint: string, wrapper: { defaultPrefix?: string }) => {
}

View File

@@ -3,10 +3,10 @@ import assert from 'assert';
import { existsSync } from 'fs';
export const parseCallsite = (fpath: string) => {
const pathobj = path.parse(fpath.replace(/file:\\?/, "")
.split(path.sep)
.join(path.posix.sep))
export const parseCallsite = (site: string) => {
const pathobj = path.parse(site.replace(/file:\\?/, "")
.split(path.sep)
.join(path.posix.sep))
return { name: pathobj.name,
absPath : path.posix.format(pathobj) }
}
@@ -16,7 +16,7 @@ export const shouldHandle = (pth: string, filenam: string) => {
let newPath = path.join(path.dirname(pth), file_name)
.replace(/file:\\?/, "");
return { exists: existsSync(newPath),
path: 'file:///'+newPath };
path: 'file://'+newPath };
}

View File

@@ -1,8 +1,7 @@
import callsites from 'callsites';
import * as Files from './core/module-loading';
import { Services } from './core/ioc';
import type { DependencyList } from './types/ioc';
interface Wrapper {
commands?: string;
@@ -10,6 +9,16 @@ interface Wrapper {
events?: string;
}
const __start = (entryPoint: string,
wrapper: { defaultPrefix?: string },
dependencies: DependencyList) => {
console.log(entryPoint)
import(entryPoint)
.then(({ __commands, __events=new Map() }) => {
console.log(__commands, __events)
})
.catch(err => dependencies[2]?.error({ message: err }));
}
/**
* @since 1.0.0
* @param wrapper Options to pass into sern.
@@ -28,10 +37,10 @@ export function init(wrapper: Wrapper) {
const initCallsite = callsites()[1].getFileName();
const handlerModule = Files.shouldHandle(initCallsite!, "handler");
console.log(handlerModule)
if(!handlerModule.exists) {
throw Error("Cannot locate handler file. Did you run sern build?");
throw Error("Could not find handler module, did you run sern build?")
}
import(handlerModule.path)
.then(({ start }) => start(initCallsite, wrapper))
.catch(err => dependencies[2].error({ message: err }))
__start(handlerModule.path, wrapper, dependencies);
}