change directory reading to be async

This commit is contained in:
Erik 2024-05-04 14:17:49 +03:00
parent 8a9e10d815
commit 9a7c53c67e
4 changed files with 11 additions and 15 deletions

View File

@ -131,7 +131,7 @@ class DiscordClient extends Client
{
if (tmp.length + segment.length >= 1900)
{
const codeBlock = tmp.match(/```/ug)?.length === 1;
const codeBlock = (tmp.match(/```/ug)?.length ?? 0) % 2 === 1;
if (codeBlock)
tmp += '```';
out.push(tmp);
@ -145,7 +145,7 @@ class DiscordClient extends Client
tmp += delimiter + segment;
}
}
if (tmp.match(/```/gu)?.length === 1)
if ((tmp.match(/```/ug)?.length ?? 0) % 2 === 1)
tmp += '```';
out.push(tmp);

View File

@ -212,7 +212,7 @@ class MusicLibrary implements Initialisable
{
this.#logger.info('Starting library scan');
const start = Date.now();
const filePaths = Util.readdirRecursive(this.#path, { ignoreSuffixes: [ '.nfo', '.jpg', '.jpeg', '.png' ] });
const filePaths = await Util.readdirRecursive(this.#path, { ignoreSuffixes: [ '.nfo', '.jpg', '.jpeg', '.png' ] });
this.#logger.debug(`${filePaths.length} files to go through`);
if (!this.#index.size)
this.#logger.info('No index built, performing first time scan. This may take some time depending on the size of your music library');

View File

@ -36,7 +36,7 @@ class Registry
async loadComponents (dir: string, classToHandle: typeof Component)
{
const directory = path.join(this.#client.rootDir, 'client', dir); // Finds directory of component folder relative to current working directory.
const files = Util.readdirRecursive(directory); // Loops through all folders in the directory and returns the files.
const files = await Util.readdirRecursive(directory); // Loops through all folders in the directory and returns the files.
const loaded: Component[] = [];
for (const filePath of files)

View File

@ -1,7 +1,7 @@
import moment from 'moment';
import humaniseDuration from 'humanize-duration';
import path from 'node:path';
import fs from 'node:fs';
import fs from 'node:fs/promises';
import {
AnySelectMenuInteraction,
APIEmbed,
@ -220,12 +220,12 @@ class Util
* @return {string[]} Array with the paths to the files within the directory
* @memberof Util
*/
static readdirRecursive (dir: string, { ignoreDotfiles = true, ignoreSuffixes = [] }: ReaddirOpts = {})
static async readdirRecursive (dir: string, { ignoreDotfiles = true, ignoreSuffixes = [] }: ReaddirOpts = {})
{
const result = [];
(function read (directory: string)
const result: string[] = [];
await (async function read (directory: string)
{
const files = fs.readdirSync(directory);
const files = await fs.readdir(directory);
for (const file of files)
{
if (file.startsWith('.') && ignoreDotfiles)
@ -234,15 +234,11 @@ class Util
continue;
const filePath = path.join(directory, file);
if (fs.statSync(filePath).isDirectory())
{
read(filePath);
}
if ((await fs.stat(filePath)).isDirectory())
await read(filePath);
else
{
result.push(filePath);
}
}
}(dir));
return result;
}