From 2ba4a20d56126b495766500c65f3490392e6735c Mon Sep 17 00:00:00 2001 From: Ryuu Mitsuki Date: Fri, 26 Apr 2024 23:53:11 +0700 Subject: [PATCH] feat(rootDir): Add file URL support to `rootDir` option Please note, this only supports the URL with 'file:' protocol. Attempting to use other than 'file:' protocol will causing an error. --- src/lsfnd.ts | 9 ++++++++- types/index.d.ts | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/lsfnd.ts b/src/lsfnd.ts index 94d8e04..ce745bb 100644 --- a/src/lsfnd.ts +++ b/src/lsfnd.ts @@ -245,7 +245,14 @@ export async function ls( + (Array.isArray(options) ? 'array' : typeof options)); } - // Resolve its absolute and relative path + // Check and resolve the `rootDir` option + if (options.rootDir && (options.rootDir instanceof URL + || (typeof options.rootDir === 'string' && /^[a-zA-Z]+:/.test(options.rootDir)) + )) { + options.rootDir = fileUrlToPath(options.rootDir); + } + + // Resolve the absolute and relative of the dirpath argument absdirpath = path.isAbsolute( dirpath) ? dirpath : path.posix.resolve( dirpath); diff --git a/types/index.d.ts b/types/index.d.ts index 0ee1cb7..3c0c51b 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -130,7 +130,7 @@ export declare interface LsOptions { * @defaultValue `'.'` or `process.cwd()` * @since 1.0.0 */ - rootDir?: StringPath | undefined; + rootDir?: StringPath | URL | undefined; /** * Determines whether to return absolute paths for all entries. * @@ -249,7 +249,7 @@ export declare interface DefaultLsOptions { readonly recursive: false; readonly match: RegExp; readonly exclude: undefined; - readonly rootDir: StringPath; + readonly rootDir: StringPath | URL; readonly absolute: false; readonly basename: false; }