import path from "node:path"; import type { CommandRegistry } from "./commandRegistry"; import { isCommandCtor } from "@/utils"; import { Logger } from "tslog"; const logger = new Logger({ type: "pretty", name: "loadCommands", }); export async function loadCommands( registry: CommandRegistry, prefix: string, pattern = "src/commands/**/*.ts", ): Promise { const glob = new Bun.Glob(pattern); for await (const file of glob.scan(".")) { const absPath = path.resolve(file); logger.debug(`Loading "${absPath}"`); // Import without caching. const mod = await import(Bun.pathToFileURL(absPath).href); for (const exported of Object.values(mod)) { if (isCommandCtor(exported)) { registry.register(prefix, absPath, exported); } } } }