Repository: discordjs/discord.js
File: packages/create-discord-bot/template/JavaScript/src/util/loaders.js (JavaScript)

Code before:
 * @returns {Promise<T[]>}
 */
export async function loadStructures(dir, predicate, recursive = true) {
	// Get the stats of the directory
	const statDir = await stat(dir);

	// If the provided directory path is not a directory, throw an error
	if (!statDir.isDirectory()) {
		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[]} */
	const structures = [];

	// Loop through all the files in the directory
	for (const file of files) {
		// Get the stats of the file
		const statFile = await stat(new URL(`${dir}/${file}`));

		// 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(new URL(`${dir}/${file}`), 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')) {
			continue;
		}

		// Import the structure dynamically from the file
		const structure = (await import(`${dir}/${file}`)).default;

		// If the structure is a valid structure, add it
		if (predicate(structure)) {
			structures.push(structure);
		}
	}

	return structures;
}

/**
 * @param {import('node:fs').PathLike} dir
 * @param {boolean} [recursive]
 * @returns {Promise<Map<string,import('../commands/index.js').Command>>}
 */

Review comment (performance):
since we use that `URL` twice now (here and 4 lines earlier) shouldn't it be defined as a const and be used twice for performance reasons?

Code after:
 * @returns {Promise<T[]>}
 */
export async function loadStructures(dir, predicate, recursive = true) {
	// Get the stats of the directory
	const statDir = await stat(dir);

	// If the provided directory path is not a directory, throw an error
	if (!statDir.isDirectory()) {
		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[]} */
	const structures = [];

	// Loop through all the files in the directory
	for (const file of files) {
		const fileUrl = new URL(`${dir}/${file}`, import.meta.url);

		// 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')) {
			continue;
		}

		// Import the structure dynamically from the file
		const structure = (await import(fileURLToPath(fileUrl))).default;

		// If the structure is a valid structure, add it
		if (predicate(structure)) {
			structures.push(structure);
		}
	}

	return structures;
}

/**
 * @param {import('node:fs').PathLike} dir
 * @param {boolean} [recursive]