Skip to content

Commit a166250

Browse files
committed
dynamic module search paths
1 parent 53c5030 commit a166250

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

src/Classes/Client.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class BreadClient<Databases extends Record<string, IDatabase<any>> = Record<stri
4343

4444
logger: ILogger;
4545

46+
moduleSearchPaths: string[] = [];
4647
hooks: HooksType<Databases> = {};
4748

4849
#setupDone = false;
@@ -63,6 +64,9 @@ class BreadClient<Databases extends Record<string, IDatabase<any>> = Record<stri
6364

6465
this.logger = new logger(this.config.logging || {});
6566

67+
this.addModuleSearchPath(BreadClient.BuiltInCommandsPath);
68+
if (this.config.commandsPath) this.addModuleSearchPath(this.config.commandsPath);
69+
6670
for (const hookName of <Hooks[]>Object.keys(hooks)) {
6771
this.hooks[hookName] = {};
6872
const thisPhaseMap = <Record<string, never[]>>this.hooks[hookName];
@@ -75,6 +79,11 @@ class BreadClient<Databases extends Record<string, IDatabase<any>> = Record<stri
7579
if (config.token) this.rest.setToken(config.token);
7680
}
7781

82+
addModuleSearchPath(p: string): void {
83+
if (this.setupDone) throw new Error("Cannot add module search paths after setup");
84+
this.moduleSearchPaths.unshift(p);
85+
}
86+
7887
addHooks<H extends Hooks, P extends HookPhasesFor<H>>(hookName: H, phase: P, ...hooks: ((...args: Parameters<HookFn<H, P>>) => Promise<HOOK_CODES> | HOOK_CODES)[]): void {
7988
if (this.setupDone) throw new Error("Cannot add hooks after setup");
8089

@@ -120,10 +129,12 @@ class BreadClient<Databases extends Record<string, IDatabase<any>> = Record<stri
120129

121130
const modulesLog: string[] = [];
122131

123-
const moduleFiles = [
124-
...this.config.commandsPath ? (<(path: string, opts: object) => string[]>readdirSync)(this.config.commandsPath, { recursive: true }).filter((x) => /module\.jso?n?$/.test(x)).map((x) => path.join(<string>this.config.commandsPath, x)) : [],
125-
...(<(path: string, opts: object) => string[]>readdirSync)(BreadClient.BuiltInCommandsPath, { recursive: true }).filter((x) => /module\.jso?n?$/.test(x)).map((x) => path.join(BreadClient.BuiltInCommandsPath, x))
126-
];
132+
const moduleFiles: string[] = [];
133+
for (const searchPath of this.moduleSearchPaths) {
134+
const files = readdirSync(searchPath, { recursive: true, encoding: "utf8" });
135+
const mFiles = files.filter((x) => /module\.jso?n?$/.test(x)).map((x) => path.join(searchPath, x));
136+
moduleFiles.push(...mFiles);
137+
}
127138
for (const file of moduleFiles) {
128139
const module = {
129140
path: path.dirname(file),

0 commit comments

Comments
 (0)