diff --git a/src/core/module-loading.ts b/src/core/module-loading.ts index e4875c5..2f389db 100644 --- a/src/core/module-loading.ts +++ b/src/core/module-loading.ts @@ -1,7 +1,6 @@ import path from 'node:path'; -import { existsSync } from 'node:fs'; +import { type Dirent, existsSync } from 'node:fs'; import { readdir } from 'fs/promises'; -import assert from 'node:assert'; import * as Id from './id' import { Module } from '../types/core-modules'; @@ -38,8 +37,9 @@ export async function importModule(absPath: string) { let fileModule = await import(absPath); let commandModule: Module = fileModule.default; - - assert(commandModule , `No default export @ ${absPath}`); + if(!commandModule) { + throw Error(`No default export @ ${absPath}`); + } if ('default' in commandModule) { commandModule = commandModule.default as Module; } @@ -53,16 +53,17 @@ export async function importModule(absPath: string) { } -export async function* readRecursive(dir: string): AsyncGenerator { +export async function* readRecursive(dir: string, directoryPlugins: string[] = []): AsyncGenerator<[string, string[]]> { const files = await readdir(dir, { withFileTypes: true }); + const plugins = files.find(file => file.isFile() && file.name.startsWith('!plugins')) as Dirent; for (const file of files) { const fullPath = path.posix.join(dir, file.name); if (file.isDirectory()) { if (!file.name.startsWith('!')) { - yield* readRecursive(fullPath); + yield* readRecursive(fullPath, [path.posix.join(dir, plugins?.name!), ...directoryPlugins]); } } else if (!file.name.startsWith('!')) { - yield "file:///"+path.resolve(fullPath); + yield ["file:///"+path.resolve(fullPath), directoryPlugins]; } } } diff --git a/src/handlers/ready.ts b/src/handlers/ready.ts index 662b1bd..4f1a370 100644 --- a/src/handlers/ready.ts +++ b/src/handlers/ready.ts @@ -17,7 +17,8 @@ export default async function(dir: string, deps : UnpackedDependencies) { // https://observablehq.com/@ehouais/multiple-promises-as-an-async-generator // possibly optimize to concurrently import modules - for await (const path of Files.readRecursive(dir)) { + for await (const [path, directoryPlugins] of Files.readRecursive(dir)) { + console.log(directoryPlugins) let { module } = await Files.importModule(path); const validType = module.type >= CommandType.Text && module.type <= CommandType.ChannelSelect; if(!validType) { diff --git a/src/handlers/user-defined-events.ts b/src/handlers/user-defined-events.ts index aea565a..2d2d3d2 100644 --- a/src/handlers/user-defined-events.ts +++ b/src/handlers/user-defined-events.ts @@ -20,7 +20,8 @@ const intoDispatcher = (deps: UnpackedDependencies) => export default async function(deps: UnpackedDependencies, eventDir: string) { const eventModules: EventModule[] = []; - for await (const path of Files.readRecursive(eventDir)) { + for await (const [path, _] of Files.readRecursive(eventDir)) { + console.log(path, _) let { module } = await Files.importModule(path); await callInitPlugins(module, deps) eventModules.push(module as EventModule);