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

View File

@ -212,7 +212,7 @@ class MusicLibrary implements Initialisable
{ {
this.#logger.info('Starting library scan'); this.#logger.info('Starting library scan');
const start = Date.now(); 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`); this.#logger.debug(`${filePaths.length} files to go through`);
if (!this.#index.size) 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'); 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) 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 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[] = []; const loaded: Component[] = [];
for (const filePath of files) for (const filePath of files)

View File

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