From 3d2d1befd99ec0ccaaf3ea5ce6b03568b0ec5d2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?A=2E=20Rom=C3=A1n?= Date: Fri, 5 Aug 2022 00:31:01 +0200 Subject: [PATCH] feat: add URL support for register methods (#231) * feat: add URL support for register methods * Update src/lib/internal/Path.ts --- src/lib/internal/Path.ts | 8 ++++++++ src/lib/structures/Store.ts | 9 ++++++--- src/lib/structures/StoreRegistry.ts | 6 ++++-- 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 src/lib/internal/Path.ts diff --git a/src/lib/internal/Path.ts b/src/lib/internal/Path.ts new file mode 100644 index 00000000..debeb6ef --- /dev/null +++ b/src/lib/internal/Path.ts @@ -0,0 +1,8 @@ +import { fileURLToPath } from 'node:url'; + +export type Path = string | URL; + +export function resolvePath(path: Path): string { + if (typeof path === 'string') return path; + return fileURLToPath(path); +} diff --git a/src/lib/structures/Store.ts b/src/lib/structures/Store.ts index b170f25f..3676b8ad 100644 --- a/src/lib/structures/Store.ts +++ b/src/lib/structures/Store.ts @@ -3,6 +3,7 @@ import type { AbstractConstructor } from '@sapphire/utilities'; import { promises as fsp } from 'fs'; import { join } from 'path'; import { LoaderError, LoaderErrorType } from '../errors/LoaderError'; +import { Path, resolvePath } from '../internal/Path'; import { container, Container } from '../shared/Container'; import type { HydratedModuleData, ILoaderResultEntry, ILoaderStrategy, ModuleData } from '../strategies/ILoaderStrategy'; import { LoaderStrategy } from '../strategies/LoaderStrategy'; @@ -85,9 +86,11 @@ export class Store extends Collection { * .registerPath(resolve('third-party', 'commands')); * ``` */ - public registerPath(path: string): this { - this.paths.add(path); - Store.logger?.(`[STORE => ${this.name}] [REGISTER] Registered path '${path}'.`); + public registerPath(path: Path): this { + const root = resolvePath(path); + + this.paths.add(root); + Store.logger?.(`[STORE => ${this.name}] [REGISTER] Registered path '${root}'.`); return this; } diff --git a/src/lib/structures/StoreRegistry.ts b/src/lib/structures/StoreRegistry.ts index 4d57ec41..e5177f52 100644 --- a/src/lib/structures/StoreRegistry.ts +++ b/src/lib/structures/StoreRegistry.ts @@ -1,5 +1,6 @@ import { Collection } from '@discordjs/collection'; import { join } from 'path'; +import { Path, resolvePath } from '../internal/Path'; import { getRootData } from '../internal/RootScan'; import type { Piece } from './Piece'; import type { Store } from './Store'; @@ -65,9 +66,10 @@ export class StoreRegistry extends Collection { * @since 2.1.0 * @param rootDirectory The root directory to register pieces at. */ - public registerPath(rootDirectory = getRootData().root) { + public registerPath(rootDirectory: Path = getRootData().root) { + const root = resolvePath(rootDirectory); for (const store of this.values() as IterableIterator>) { - store.registerPath(join(rootDirectory, store.name)); + store.registerPath(join(root, store.name)); } }