diff --git a/src/commands/command.ts b/src/commands/command.ts index e3e0c273..550e3493 100644 --- a/src/commands/command.ts +++ b/src/commands/command.ts @@ -428,7 +428,6 @@ export class CommandsLoader { async loadDirectory( path: string, options?: { - recursive?: boolean exportName?: string maxDepth?: number exts?: string[] diff --git a/src/commands/extension.ts b/src/commands/extension.ts index 8d5483b1..bffe77bc 100644 --- a/src/commands/extension.ts +++ b/src/commands/extension.ts @@ -2,6 +2,7 @@ import { Collection } from '../utils/collection.ts' import { Command } from './command.ts' import { CommandClient } from './client.ts' import type { ClientEvents } from '../gateway/handlers/mod.ts' +import { join, walk } from '../../deps.ts' // Breaking change if we change to unknown export type ExtensionEventCallback = (ext: Extension, ...args: any[]) => any @@ -152,6 +153,47 @@ export class ExtensionsManager { ext.load() } + /** + * Load extensions from a Directory. + * + * NOTE: Relative paths resolve from cwd + * + * @param path Path of the directory. + * @param options Options to configure loading. + */ + async loadDirectory( + path: string, + options?: { + exportName?: string + maxDepth?: number + exts?: string[] + onlyRead?: boolean + } + ): Promise { + const extensions: Extension[] = [] + + for await (const entry of walk(path, { + maxDepth: options?.maxDepth, + exts: options?.exts, + includeDirs: false + })) { + if (entry.isFile !== true) continue + const ext = ( + await import( + // eslint-disable-next-line @typescript-eslint/restrict-plus-operands + 'file:///' + + // eslint-disable-next-line @typescript-eslint/restrict-plus-operands + join(Deno.cwd(), entry.path) + ) + )[options?.exportName ?? 'default'] + + extensions.push(ext) + if (options?.onlyRead !== true) this.load(ext) + } + + return extensions + } + /** Unloads an Extension from Command Client */ unload(ext: Extension | string): boolean { const name = typeof ext === 'string' ? ext : ext.name