From 9a7c53c67ecb1938f4ab536c1b44f204b4c9867b Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Sat, 4 May 2024 14:17:49 +0300 Subject: [PATCH] change directory reading to be async --- src/client/DiscordClient.ts | 4 ++-- src/client/components/MusicLibrary.ts | 2 +- src/client/components/Registry.ts | 2 +- src/utilities/Util.ts | 18 +++++++----------- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/client/DiscordClient.ts b/src/client/DiscordClient.ts index 8a2e976..ae68a4e 100644 --- a/src/client/DiscordClient.ts +++ b/src/client/DiscordClient.ts @@ -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); diff --git a/src/client/components/MusicLibrary.ts b/src/client/components/MusicLibrary.ts index 4242b22..5680c4e 100644 --- a/src/client/components/MusicLibrary.ts +++ b/src/client/components/MusicLibrary.ts @@ -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'); diff --git a/src/client/components/Registry.ts b/src/client/components/Registry.ts index cf6f51c..6c2b806 100644 --- a/src/client/components/Registry.ts +++ b/src/client/components/Registry.ts @@ -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) diff --git a/src/utilities/Util.ts b/src/utilities/Util.ts index 9753348..c9a9d35 100644 --- a/src/utilities/Util.ts +++ b/src/utilities/Util.ts @@ -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,14 +234,10 @@ 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;