Skip to content

Commit b607c2a

Browse files
authored
Merge pull request #161 from ghostdevv/totalist
2 parents 8321632 + d751a40 commit b607c2a

File tree

7 files changed

+57
-53
lines changed

7 files changed

+57
-53
lines changed

.changeset/wise-needles-look.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'jellycommands': patch
3+
---
4+
5+
change command/event file resolution logic

packages/jellycommands/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
"dependencies": {
4848
"axios": "^1.3.3",
4949
"discord-api-types": "^0.37.35",
50-
"ghoststools": "^0.6.3",
51-
"joi": "^17.8.3"
50+
"joi": "^17.8.3",
51+
"totalist": "^3.0.0"
5252
},
5353
"repository": {
5454
"type": "git",

packages/jellycommands/src/commands/resolve.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { GuildCommands, GlobalCommands } from './types.d';
2-
import { resolveItems } from '../utils/resolveItems';
32
import { BaseCommand } from './types/BaseCommand';
43
import { JellyCommands } from '../JellyCommands';
4+
import { read } from '../utils/files';
55

66
export const resolveCommands = async (
77
client: JellyCommands,
88
items: string | Array<string | BaseCommand>,
99
) => {
10-
const commands = await resolveItems(items);
10+
const commands = new Set<BaseCommand>();
11+
12+
await read(items, (command) => {
13+
if (!(command instanceof BaseCommand))
14+
throw new Error(`Found invalid item "${command}" in options.commands`);
15+
16+
commands.add(command);
17+
});
1118

1219
const globalCommands: GlobalCommands = new Set();
1320
const guildCommands: GuildCommands = new Map();

packages/jellycommands/src/events/register.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import { resolveItems } from '../utils/resolveItems';
21
import { JellyCommands } from '../JellyCommands';
2+
import { read } from '../utils/files';
33
import { Event } from './Event';
44

55
export const registerEvents = async (
66
client: JellyCommands,
77
items: string | Array<string | Event<any>>,
88
) => {
9-
const events = await resolveItems(items);
9+
const events = new Set<Event>();
10+
11+
await read(items, (event) => {
12+
if (!(event instanceof Event))
13+
throw new Error(`Found invalid item "${event}" in options.events`);
14+
15+
events.add(event);
16+
});
1017

1118
for (const event of events) {
1219
if (event.options.disabled) continue;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { totalist } from 'totalist';
2+
3+
// Takes in file/folder paths and T and will resolve all to T
4+
// When T is found callback is called
5+
export async function read<T>(things: string | Array<string | T>, callback: (item: T) => void) {
6+
const thingsArray = Array.isArray(things) ? things : [things];
7+
8+
for (const item of thingsArray) {
9+
if (typeof item != 'string') {
10+
callback(item);
11+
continue;
12+
}
13+
14+
await totalist(item, async (name, path) => {
15+
// If it starts with an _ we ignore it
16+
if (name.startsWith('_')) return;
17+
18+
// Import the file
19+
const data = await import(path);
20+
21+
// Call add with the default export
22+
callback(data.default);
23+
});
24+
}
25+
}

packages/jellycommands/src/utils/resolveItems.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

pnpm-lock.yaml

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)