Skip to content

Commit

Permalink
feat: add URL support for register methods (#231)
Browse files Browse the repository at this point in the history
* feat: add URL support for register methods

* Update src/lib/internal/Path.ts
  • Loading branch information
kyranet committed Aug 4, 2022
1 parent f83a05d commit 3d2d1be
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/lib/internal/Path.ts
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 6 additions & 3 deletions src/lib/structures/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -85,9 +86,11 @@ export class Store<T extends Piece> extends Collection<string, T> {
* .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;
}

Expand Down
6 changes: 4 additions & 2 deletions src/lib/structures/StoreRegistry.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -65,9 +66,10 @@ export class StoreRegistry extends Collection<Key, Value> {
* @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<Piece>>) {
store.registerPath(join(rootDirectory, store.name));
store.registerPath(join(root, store.name));
}
}

Expand Down

0 comments on commit 3d2d1be

Please sign in to comment.