mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
cleanup tests, codegen, and importing handler
This commit is contained in:
@@ -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 }) => {
|
||||
|
||||
}
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
|
||||
|
||||
|
||||
21
src/sern.ts
21
src/sern.ts
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { assertType, describe, it } from 'vitest';
|
||||
|
||||
import { __Services } from '../../src/core/structures';
|
||||
import * as __Services from '../../src/core/structures/default-services';
|
||||
import * as Contracts from '../../src/core/interfaces';
|
||||
|
||||
describe('default contracts', () => {
|
||||
|
||||
@@ -2,7 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { CoreContainer } from '../../src/core/ioc/container';
|
||||
import { EventEmitter } from 'events';
|
||||
import { Disposable, Emitter, Init, Logging } from '../../src/core/interfaces';
|
||||
import { __Services } from '../../src/core/structures'
|
||||
import * as __Services from '../../src/core/structures/default-services'
|
||||
import { CoreDependencies } from '../../src/types/ioc';
|
||||
|
||||
describe('ioc container', () => {
|
||||
|
||||
@@ -5,7 +5,7 @@ import { Presence } from '../../src';
|
||||
// Example test suite for the module function
|
||||
describe('module function', () => {
|
||||
it('should return a valid configuration', () => {
|
||||
const config: Presence.Config<['dependency1', 'dependency2']> = Presence.module({
|
||||
const config = Presence.module({
|
||||
inject: ['dependency1', 'dependency2'],
|
||||
execute: vi.fn(),
|
||||
});
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { SpyInstance, afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { CoreContainer } from '../../src/core/ioc/container';
|
||||
import { __Services } from '../../src/core/structures/';
|
||||
import * as __Services from '../../src/core/structures/default-services';
|
||||
import { faker } from '@faker-js/faker';
|
||||
import { commandModule, CommandType } from '../../src';
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { beforeEach, describe, expect, vi, it } from 'vitest';
|
||||
import { eventDispatcher } from '../../src/handlers/dispatchers';
|
||||
import { eventDispatcher } from '../../src/handlers/event-utils';
|
||||
import { faker } from '@faker-js/faker';
|
||||
import { Module } from '../../src/types/core-modules';
|
||||
import { Processed } from '../../src/handlers/types';
|
||||
import { Processed } from '../../src/types/core-modules';
|
||||
import { CommandType } from '../../src/core/structures/enums';
|
||||
import { EventEmitter } from 'events';
|
||||
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import * as Id from '../../src/core/id';
|
||||
import { faker } from '@faker-js/faker';
|
||||
import { CommandModule, CommandType, commandModule } from '../../src';
|
||||
|
||||
function createRandomCommandModules() {
|
||||
const randomCommandType = [
|
||||
CommandType.Text,
|
||||
CommandType.Both,
|
||||
CommandType.CtxMsg,
|
||||
CommandType.CtxUser,
|
||||
CommandType.Modal,
|
||||
CommandType.ChannelSelect,
|
||||
CommandType.RoleSelect,
|
||||
CommandType.UserSelect,
|
||||
CommandType.StringSelect,
|
||||
CommandType.Button,
|
||||
];
|
||||
return commandModule({
|
||||
type: faker.helpers.uniqueArray(randomCommandType, 1)[0],
|
||||
description: faker.string.alpha(),
|
||||
name: faker.string.alpha(),
|
||||
execute: () => {},
|
||||
});
|
||||
}
|
||||
function createMetadata(c: CommandModule) {
|
||||
return {
|
||||
fullPath: faker.system.filePath(),
|
||||
id: Id.create(c.name, c.type),
|
||||
isClass: Boolean(Math.floor(Math.random())),
|
||||
};
|
||||
}
|
||||
const appBitField = 0b000000001111;
|
||||
|
||||
describe('id resolution', () => {
|
||||
it('should resolve application commands correctly', () => {
|
||||
const modules = faker.helpers.multiple(createRandomCommandModules, {
|
||||
count: 20,
|
||||
});
|
||||
const metadata = modules.map(createMetadata);
|
||||
metadata.forEach((meta, idx) => {
|
||||
const associatedModule = modules[idx];
|
||||
const uid = Id.create(associatedModule.name!, associatedModule.type!);
|
||||
expect(meta.id).toBe(uid);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -4,7 +4,7 @@
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"strictNullChecks": true,
|
||||
"moduleResolution": "node",
|
||||
"moduleResolution": "node16",
|
||||
"skipLibCheck": true,
|
||||
"declaration": true,
|
||||
"preserveSymlinks": true,
|
||||
@@ -12,8 +12,9 @@
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"isolatedModules": true,
|
||||
"outDir": "dist",
|
||||
"module": "commonjs",
|
||||
"target": "esnext"
|
||||
"module": "node16",
|
||||
"target": "esnext",
|
||||
"sourceMap": true
|
||||
},
|
||||
"exclude": ["node_modules", "dist"],
|
||||
"include": ["./src", "./src/**/*.d.ts"]
|
||||
|
||||
Reference in New Issue
Block a user