mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
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:
@@ -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
|
||||
*/
|
||||
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user