Skip to content

Commit

Permalink
Merge pull request #373 from Blocksnmore/load-extensions
Browse files Browse the repository at this point in the history
Feat: Extension directory loading
  • Loading branch information
Helloyunho authored Dec 15, 2023
2 parents 07d04ea + a45bfb6 commit d570b9e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/commands/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,6 @@ export class CommandsLoader {
async loadDirectory(
path: string,
options?: {
recursive?: boolean
exportName?: string
maxDepth?: number
exts?: string[]
Expand Down
42 changes: 42 additions & 0 deletions src/commands/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Extension[]> {
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
Expand Down

0 comments on commit d570b9e

Please sign in to comment.