Skip to content

Commit cb4aabc

Browse files
Add option to explicitly include components
1 parent 1c3f4d1 commit cb4aabc

5 files changed

Lines changed: 51 additions & 18 deletions

File tree

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,23 @@ Secrets and other bot info must be configured in the `config.json` file. An exam
6767
"id": "<bot id>",
6868
"guild": "<guild id>",
6969
"token": "<discord api token>",
70-
"mom": "<your user id>", // to be pinged when things go wrong
70+
"mom": "<your user id>", // to be pinged when things go wrong
7171
"mongo": {
7272
"user": "wheatley",
7373
"password": "<mongo password>",
74-
"host": "127.0.0.1", // optional
75-
"port": 27017 // optional
74+
"host": "127.0.0.1", // optional
75+
"port": 27017 // optional
7676
},
77-
"freestanding": false, // optional,
78-
"exclude": [
79-
// optional
80-
"components/shortcuts.js",
81-
"modules/tccpp/components/april1/**"
82-
]
77+
"freestanding": false, // optional,
78+
"components": { // optional
79+
"exclude": [ // optional, specify patterns of components to exclude
80+
"components/shortcuts.js",
81+
"modules/tccpp/components/**"
82+
],
83+
"include": [ // optional, explicitly specify components to load
84+
"modules/tccpp/components/cppref.js"
85+
]
86+
}
8387
}
8488
```
8589

config.default.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"port": 27017
1111
},
1212
"freestanding": true,
13-
"exclude": [
14-
"modules/tccpp/components/april1/**"
15-
]
13+
"components": {
14+
"exclude": [
15+
"modules/tccpp/components/april1/**"
16+
]
17+
}
1618
}

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"gray-matter": "^4.0.3",
2121
"moment": "^2.30.1",
2222
"mongodb": "^6.16.0",
23+
"path-scurry": "^2.0.0",
2324
"prom-client": "^15.1.3",
2425
"stack-trace": "^1.0.0-pre2",
2526
"typescript": "^5.8.3",

src/wheatley.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import PromClient from "prom-client";
88
import { colors, MINUTE } from "./common.js";
99
import { unwrap } from "./utils/misc.js";
1010
import { to_string, is_string } from "./utils/strings.js";
11-
import { globIterate } from "glob";
11+
import { globIterateSync } from "glob";
12+
import { PathScurry } from "path-scurry";
1213
import { M } from "./utils/debugging-and-logging.js";
1314
import { BotComponent } from "./bot-component.js";
1415

@@ -53,7 +54,10 @@ type core_config = {
5354
mom?: string;
5455
mongo?: wheatley_database_credentials;
5556
freestanding?: boolean;
56-
exclude?: string[];
57+
components?: {
58+
exclude?: string[];
59+
include?: string[];
60+
};
5761
sentry?: string;
5862
metrics?: {
5963
port: number;
@@ -324,6 +328,30 @@ export class Wheatley {
324328
this.setup(config).catch(this.critical_error.bind(this));
325329
}
326330

331+
private *locate_components(config: core_config) {
332+
const visited = config.components?.include ? new Set<string>() : undefined;
333+
334+
const pw = new PathScurry(import.meta.dirname);
335+
336+
for (const file of globIterateSync("**/components/**/*.js", {
337+
ignore: config.components?.exclude,
338+
scurry: pw,
339+
withFileTypes: true,
340+
})) {
341+
yield file.relativePosix();
342+
visited?.add(file.fullpath());
343+
}
344+
345+
if (config.components?.include) {
346+
for (const file of config.components.include) {
347+
const path = pw.resolve(file);
348+
if (!visited!.has(path)) {
349+
yield file;
350+
}
351+
}
352+
}
353+
}
354+
327355
async setup(config: core_config) {
328356
assert(this.freestanding || config.mongo, "Missing MongoDB credentials");
329357
if (config.mongo) {
@@ -368,10 +396,7 @@ export class Wheatley {
368396
})().catch(this.critical_error.bind(this));
369397
});
370398

371-
for await (const file of globIterate("**/components/**/*.js", {
372-
ignore: config.exclude,
373-
cwd: import.meta.dirname,
374-
})) {
399+
for (const file of this.locate_components(config)) {
375400
const default_export = (await import(`./${file}`)).default;
376401
if (default_export !== undefined) {
377402
await this.add_component(default_export);

0 commit comments

Comments
 (0)