mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import { createId } from '../../../handler/id';
|
|
import { CoreModuleStore, ModuleManager } from '../../contracts';
|
|
import { importModule } from '../../module-loading';
|
|
import { CommandMeta, CommandModule, CommandModuleDefs, Module } from '../../types/modules';
|
|
import { CommandType } from '../enums';
|
|
/**
|
|
* @internal
|
|
* @since 2.0.0
|
|
* Version 4.0.0 will internalize this api. Please refrain from using DefaultModuleManager!
|
|
*/
|
|
export class DefaultModuleManager implements ModuleManager {
|
|
constructor(private moduleStore: CoreModuleStore) {}
|
|
|
|
getByNameCommandType<T extends CommandType>(name: string, commandType: T) {
|
|
const id = this.get(createId(name, commandType));
|
|
if(!id) {
|
|
return undefined;
|
|
}
|
|
return importModule<CommandModuleDefs[T]>(id);
|
|
}
|
|
|
|
setMetadata(m: Module, c: CommandMeta): void {
|
|
this.moduleStore.metadata.set(m, c);
|
|
}
|
|
|
|
getMetadata(m: Module): CommandMeta {
|
|
const maybeModule = this.moduleStore.metadata.get(m);
|
|
if (!maybeModule) {
|
|
throw Error('Could not find metadata in store for ' + maybeModule);
|
|
}
|
|
return maybeModule;
|
|
}
|
|
|
|
|
|
get(id: string) {
|
|
return this.moduleStore.commands.get(id);
|
|
}
|
|
set(id: string, path: string): void {
|
|
this.moduleStore.commands.set(id, path);
|
|
}
|
|
//not tested
|
|
getPublishableCommands(): Promise<CommandModule[]> {
|
|
const entries = this.moduleStore.commands.entries();
|
|
const publishable = 0b000000110;
|
|
return Promise.all(
|
|
Array.from(entries)
|
|
.filter(([id]) => !(Number.parseInt(id.at(-1)!) & publishable))
|
|
.map(([, path]) => importModule<CommandModule>(path)),
|
|
);
|
|
}
|
|
}
|