refactor: update deno template and loader logic (#11060)

* refactor: update deno template and loader logic

* yeet

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Almeida
2025-09-04 13:36:26 +01:00
committed by GitHub
parent 5a656b849f
commit 8ca279e0c3
18 changed files with 96 additions and 148 deletions

View File

@@ -3,10 +3,10 @@ import { z } from 'zod';
/**
* Defines the structure of an event.
*
* @template {keyof import('discord.js').ClientEvents} [T=keyof import('discord.js').ClientEvents]
* @template {keyof import('discord.js').ClientEvents} [EventName=keyof import('discord.js').ClientEvents]
* @typedef {object} Event
* @property {(...parameters: import('discord.js').ClientEvents[T]) => Promise<void> | void} execute The function to execute the command
* @property {T} name The name of the event to listen to
* @property {(...parameters: import('discord.js').ClientEvents[EventName]) => Promise<void> | void} execute The function to execute the command
* @property {EventName} name The name of the event to listen to
* @property {boolean} [once] Whether or not the event should only be listened to once
*/

View File

@@ -1,23 +1,23 @@
import { readdir, stat } from 'node:fs/promises';
import { URL } from 'node:url';
import { glob, stat } from 'node:fs/promises';
import { fileURLToPath, resolve, URL } from 'node:url';
import { predicate as commandPredicate } from '../commands/index.js';
import { predicate as eventPredicate } from '../events/index.js';
/**
* A predicate to check if the structure is valid.
*
* @template T
* @typedef {(structure: unknown) => structure is T} StructurePredicate
* @template Structure
* @typedef {(structure: unknown) => structure is Structure} StructurePredicate
*/
/**
* Loads all the structures in the provided directory.
*
* @template T
* @template Structure
* @param {import('node:fs').PathLike} dir - The directory to load the structures from
* @param {StructurePredicate<T>} predicate - The predicate to check if the structure is valid
* @param {StructurePredicate<Structure>} predicate - The predicate to check if the structure is valid
* @param {boolean} recursive - Whether to recursively load the structures in the directory
* @returns {Promise<T[]>}
* @returns {Promise<Structure[]>}
*/
export async function loadStructures(dir, predicate, recursive = true) {
// Get the stats of the directory
@@ -28,35 +28,25 @@ export async function loadStructures(dir, predicate, recursive = true) {
throw new Error(`The directory '${dir}' is not a directory.`);
}
// Get all the files in the directory
const files = await readdir(dir);
// Create an empty array to store the structures
/** @type {T[]} */
/** @type {Structure[]} */
const structures = [];
// Loop through all the files in the directory
for (const file of files) {
const fileUrl = new URL(`${dir}/${file}`, import.meta.url);
// Create a glob pattern to match the .js files
const basePath = dir instanceof URL ? fileURLToPath(dir) : dir.toString();
const pattern = resolve(basePath, recursive ? '**/*.js' : '*.js');
// Get the stats of the file
const statFile = await stat(fileUrl);
// If the file is a directory and recursive is true, recursively load the structures in the directory
if (statFile.isDirectory() && recursive) {
structures.push(...(await loadStructures(fileUrl, predicate, recursive)));
continue;
}
// If the file is index.js or the file does not end with .js, skip the file
if (file === 'index.js' || !file.endsWith('.js')) {
// Loop through all the matching files in the directory
for await (const file of glob(pattern)) {
// If the file is index.js, skip the file
if (file.endsWith('/index.js')) {
continue;
}
// Import the structure dynamically from the file
const structure = (await import(fileUrl.toString())).default;
const { default: structure } = await import(file);
// If the structure is a valid structure, add it
// If the default export is a valid structure, add it
if (predicate(structure)) {
structures.push(structure);
}
@@ -68,7 +58,7 @@ export async function loadStructures(dir, predicate, recursive = true) {
/**
* @param {import('node:fs').PathLike} dir
* @param {boolean} [recursive]
* @returns {Promise<Map<string,import('../commands/index.js').Command>>}
* @returns {Promise<Map<string, import('../commands/index.js').Command>>}
*/
export async function loadCommands(dir, recursive = true) {
return (await loadStructures(dir, commandPredicate, recursive)).reduce(