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(name: string, commandType: T) { const id = this.get(createId(name, commandType)); if(!id) { return undefined; } return importModule(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 { 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(path)), ); } }