diff --git a/index.js b/index.js index d2d9184..647a1ce 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,13 @@ import path from 'node:path'; +import {fileURLToPath} from 'node:url'; import {locatePath, locatePathSync} from 'locate-path'; +const toPath = urlOrPath => urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath; + export const findUpStop = Symbol('findUpStop'); export async function findUpMultiple(name, options = {}) { - let directory = path.resolve(options.cwd || ''); + let directory = path.resolve(toPath(options.cwd) || ''); const {root} = path.parse(directory); const stopAt = path.resolve(directory, options.stopAt || root); const limit = options.limit || Number.POSITIVE_INFINITY; @@ -48,7 +51,7 @@ export async function findUpMultiple(name, options = {}) { } export function findUpMultipleSync(name, options = {}) { - let directory = path.resolve(options.cwd || ''); + let directory = path.resolve(toPath(options.cwd) || ''); const {root} = path.parse(directory); const stopAt = options.stopAt || root; const limit = options.limit || Number.POSITIVE_INFINITY; diff --git a/index.test-d.ts b/index.test-d.ts index 9e9c205..4d154a5 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -3,6 +3,7 @@ import {findUp, findUpSync, findUpMultiple, findUpMultipleSync, findUpStop, path expectType>(findUp('unicorn.png')); expectType>(findUp('unicorn.png', {cwd: ''})); +expectType>(findUp('unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUp(['rainbow.png', 'unicorn.png'])); expectType>(findUp(['rainbow.png', 'unicorn.png'], {cwd: ''})); expectType>(findUp(['rainbow.png', 'unicorn.png'], {allowSymlinks: true})); @@ -14,6 +15,7 @@ expectError(findUp(['rainbow.png', 'unicorn.png'], {concurrency: 1})); expectType>(findUp(() => 'unicorn.png')); expectType>(findUp(() => 'unicorn.png', {cwd: ''})); +expectType>(findUp(() => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUp(() => 'unicorn.png', {allowSymlinks: true})); expectType>(findUp(() => 'unicorn.png', {allowSymlinks: false})); expectType>(findUp(() => 'unicorn.png', {type: 'file'})); @@ -21,6 +23,7 @@ expectType>(findUp(() => 'unicorn.png', {type: 'dire expectType>(findUp(() => 'unicorn.png', {stopAt: 'foo'})); expectType>(findUp(() => undefined)); expectType>(findUp(() => undefined, {cwd: ''})); +expectType>(findUp(() => undefined, {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUp(() => undefined, {allowSymlinks: true})); expectType>(findUp(() => undefined, {allowSymlinks: false})); expectType>(findUp(() => undefined, {type: 'file'})); @@ -28,6 +31,7 @@ expectType>(findUp(() => undefined, {type: 'director expectType>(findUp(() => undefined, {stopAt: 'foo'})); expectType>(findUp((): typeof findUpStop => findUpStop)); expectType>(findUp((): typeof findUpStop => findUpStop, {cwd: ''})); +expectType>(findUp((): typeof findUpStop => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUp((): typeof findUpStop => findUpStop, {stopAt: 'foo'})); expectType>(findUp(async () => 'unicorn.png')); expectType>(findUp(async () => 'unicorn.png', {cwd: ''})); @@ -38,6 +42,7 @@ expectType>(findUp(async () => 'unicorn.png', {type: expectType>(findUp(async () => 'unicorn.png', {stopAt: 'foo'})); expectType>(findUp(async () => undefined)); expectType>(findUp(async () => undefined, {cwd: ''})); +expectType>(findUp(async () => undefined, {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUp(async () => undefined, {allowSymlinks: true})); expectType>(findUp(async () => undefined, {allowSymlinks: false})); expectType>(findUp(async () => undefined, {type: 'file'})); @@ -46,6 +51,7 @@ expectType>(findUp(async () => undefined, {stopAt: ' expectType>(findUp(async (): Promise => findUpStop)); expectType>(findUp(async (): Promise => findUpStop, {cwd: ''})); +expectType>(findUp(async (): Promise => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUp(async (): Promise => findUpStop, {allowSymlinks: true})); expectType>(findUp(async (): Promise => findUpStop, {allowSymlinks: false})); expectType>(findUp(async (): Promise => findUpStop, {type: 'file'})); @@ -54,8 +60,10 @@ expectType>(findUp(async (): Promise>(findUpMultiple('unicorn.png')); expectType>(findUpMultiple('unicorn.png', {cwd: ''})); +expectType>(findUpMultiple('unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'])); expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {cwd: ''})); +expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {allowSymlinks: true})); expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {allowSymlinks: false})); expectType>(findUpMultiple(['rainbow.png', 'unicorn.png'], {type: 'file'})); @@ -65,6 +73,7 @@ expectError(findUpMultiple(['rainbow.png', 'unicorn.png'], {concurrency: 1})); expectType>(findUpMultiple(() => 'unicorn.png')); expectType>(findUpMultiple(() => 'unicorn.png', {cwd: ''})); +expectType>(findUpMultiple(() => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUpMultiple(() => 'unicorn.png', {allowSymlinks: true})); expectType>(findUpMultiple(() => 'unicorn.png', {allowSymlinks: false})); expectType>(findUpMultiple(() => 'unicorn.png', {type: 'file'})); @@ -72,6 +81,7 @@ expectType>(findUpMultiple(() => 'unicorn.png', {type: 'direct expectType>(findUpMultiple(() => 'unicorn.png', {stopAt: 'foo'})); expectType>(findUpMultiple(() => undefined)); expectType>(findUpMultiple(() => undefined, {cwd: ''})); +expectType>(findUpMultiple(() => undefined, {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUpMultiple(() => undefined, {allowSymlinks: true})); expectType>(findUpMultiple(() => undefined, {allowSymlinks: false})); expectType>(findUpMultiple(() => undefined, {type: 'file'})); @@ -79,9 +89,11 @@ expectType>(findUpMultiple(() => undefined, {type: 'directory' expectType>(findUpMultiple(() => undefined, {stopAt: 'foo'})); expectType>(findUpMultiple((): typeof findUpStop => findUpStop)); expectType>(findUpMultiple((): typeof findUpStop => findUpStop, {cwd: ''})); +expectType>(findUpMultiple((): typeof findUpStop => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUpMultiple((): typeof findUpStop => findUpStop, {stopAt: 'foo'})); expectType>(findUpMultiple(async () => 'unicorn.png')); expectType>(findUpMultiple(async () => 'unicorn.png', {cwd: ''})); +expectType>(findUpMultiple(async () => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUpMultiple(async () => 'unicorn.png', {allowSymlinks: true})); expectType>(findUpMultiple(async () => 'unicorn.png', {allowSymlinks: false})); expectType>(findUpMultiple(async () => 'unicorn.png', {type: 'file'})); @@ -89,6 +101,7 @@ expectType>(findUpMultiple(async () => 'unicorn.png', {type: ' expectType>(findUpMultiple(async () => 'unicorn.png', {stopAt: 'foo'})); expectType>(findUpMultiple(async () => undefined)); expectType>(findUpMultiple(async () => undefined, {cwd: ''})); +expectType>(findUpMultiple(async () => undefined, {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUpMultiple(async () => undefined, {allowSymlinks: true})); expectType>(findUpMultiple(async () => undefined, {allowSymlinks: false})); expectType>(findUpMultiple(async () => undefined, {type: 'file'})); @@ -97,6 +110,7 @@ expectType>(findUpMultiple(async () => undefined, {stopAt: 'fo expectType>(findUpMultiple(async (): Promise => findUpStop)); expectType>(findUpMultiple(async (): Promise => findUpStop, {cwd: ''})); +expectType>(findUpMultiple(async (): Promise => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); expectType>(findUpMultiple(async (): Promise => findUpStop, {allowSymlinks: true})); expectType>(findUpMultiple(async (): Promise => findUpStop, {allowSymlinks: false})); expectType>(findUpMultiple(async (): Promise => findUpStop, {type: 'file'})); @@ -105,8 +119,10 @@ expectType>(findUpMultiple(async (): Promise(findUpSync('unicorn.png')); expectType(findUpSync('unicorn.png', {cwd: ''})); +expectType(findUpSync('unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpSync(['rainbow.png', 'unicorn.png'])); expectType(findUpSync(['rainbow.png', 'unicorn.png'], {cwd: ''})); +expectType(findUpSync(['rainbow.png', 'unicorn.png'], {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpSync(['rainbow.png', 'unicorn.png'], {allowSymlinks: true})); expectType(findUpSync(['rainbow.png', 'unicorn.png'], {allowSymlinks: false})); expectType(findUpSync(['rainbow.png', 'unicorn.png'], {type: 'file'})); @@ -115,6 +131,7 @@ expectType(findUpSync(['rainbow.png', 'unicorn.png'], {stopA expectType(findUpSync(() => 'unicorn.png')); expectType(findUpSync(() => 'unicorn.png', {cwd: ''})); +expectType(findUpSync(() => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpSync(() => 'unicorn.png', {allowSymlinks: true})); expectType(findUpSync(() => 'unicorn.png', {allowSymlinks: false})); expectType(findUpSync(() => 'unicorn.png', {type: 'file'})); @@ -122,6 +139,7 @@ expectType(findUpSync(() => 'unicorn.png', {type: 'directory expectType(findUpSync(() => 'unicorn.png', {stopAt: 'foo'})); expectType(findUpSync(() => undefined)); expectType(findUpSync(() => undefined, {cwd: ''})); +expectType(findUpSync(() => undefined, {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpSync(() => undefined, {allowSymlinks: true})); expectType(findUpSync(() => undefined, {allowSymlinks: false})); expectType(findUpSync(() => undefined, {type: 'file'})); @@ -129,14 +147,17 @@ expectType(findUpSync(() => undefined, {type: 'directory'})) expectType(findUpSync(() => undefined, {stopAt: 'foo'})); expectType(findUpSync((): typeof findUpStop => findUpStop)); expectType(findUpSync((): typeof findUpStop => findUpStop, {cwd: ''})); +expectType(findUpSync((): typeof findUpStop => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpSync((): typeof findUpStop => findUpStop, {type: 'file'})); expectType(findUpSync((): typeof findUpStop => findUpStop, {type: 'directory'})); expectType(findUpSync((): typeof findUpStop => findUpStop, {stopAt: 'foo'})); expectType(findUpMultipleSync('unicorn.png')); expectType(findUpMultipleSync('unicorn.png', {cwd: ''})); +expectType(findUpMultipleSync('unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'])); expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {cwd: ''})); +expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {allowSymlinks: true})); expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {allowSymlinks: false})); expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {type: 'file'})); @@ -144,6 +165,7 @@ expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {type: ' expectType(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {stopAt: 'foo'})); expectType(findUpMultipleSync(() => 'unicorn.png')); expectType(findUpMultipleSync(() => 'unicorn.png', {cwd: ''})); +expectType(findUpMultipleSync(() => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpMultipleSync(() => 'unicorn.png', {allowSymlinks: true})); expectType(findUpMultipleSync(() => 'unicorn.png', {allowSymlinks: false})); expectType(findUpMultipleSync(() => 'unicorn.png', {type: 'file'})); @@ -151,6 +173,7 @@ expectType(findUpMultipleSync(() => 'unicorn.png', {type: 'directory'} expectType(findUpMultipleSync(() => 'unicorn.png', {stopAt: 'foo'})); expectType(findUpMultipleSync(() => undefined)); expectType(findUpMultipleSync(() => undefined, {cwd: ''})); +expectType(findUpMultipleSync(() => undefined, {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpMultipleSync(() => undefined, {allowSymlinks: true})); expectType(findUpMultipleSync(() => undefined, {allowSymlinks: false})); expectType(findUpMultipleSync(() => undefined, {type: 'file'})); @@ -158,6 +181,7 @@ expectType(findUpMultipleSync(() => undefined, {type: 'directory'})); expectType(findUpMultipleSync(() => undefined, {stopAt: 'foo'})); expectType(findUpMultipleSync((): typeof findUpStop => findUpStop)); expectType(findUpMultipleSync((): typeof findUpStop => findUpStop, {cwd: ''})); +expectType(findUpMultipleSync((): typeof findUpStop => findUpStop, {cwd: new URL('file:///path/to/cwd/')})); expectType(findUpMultipleSync((): typeof findUpStop => findUpStop, {type: 'file'})); expectType(findUpMultipleSync((): typeof findUpStop => findUpStop, {type: 'directory'})); expectType(findUpMultipleSync((): typeof findUpStop => findUpStop, {stopAt: 'foo'})); diff --git a/package.json b/package.json index 13f8e92..2656d5e 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "path" ], "dependencies": { - "locate-path": "^7.0.0", + "locate-path": "^7.1.0", "path-exists": "^5.0.0" }, "devDependencies": { diff --git a/readme.md b/readme.md index e3dc9ff..488ac61 100644 --- a/readme.md +++ b/readme.md @@ -98,7 +98,7 @@ Type: `object` ##### cwd -Type: `string`\ +Type: `URL | string`\ Default: `process.cwd()` The directory to start from. diff --git a/test.js b/test.js index ca8b2bd..d142f83 100644 --- a/test.js +++ b/test.js @@ -2,7 +2,7 @@ import process from 'node:process'; import {promisify} from 'node:util'; import fs from 'node:fs'; import path from 'node:path'; -import {fileURLToPath} from 'node:url'; +import {fileURLToPath, pathToFileURL} from 'node:url'; import test from 'ava'; import isPathInside from 'is-path-inside'; import tempy from 'tempy'; @@ -49,6 +49,10 @@ absolute.barDirQux = path.join(absolute.fixtureDirectory, name.fooDirectory, nam absolute.fileLink = path.join(absolute.fixtureDirectory, name.fileLink); absolute.directoryLink = path.join(absolute.fixtureDirectory, name.directoryLink); +const url = { + fixtureDirectory: pathToFileURL(absolute.fixtureDirectory), +}; + // Create a disjoint directory, used for the not-found tests test.beforeEach(t => { const temporaryDirectory = tempy.directory(); @@ -123,6 +127,11 @@ test('async (child file, custom cwd)', async t => { }); t.is(foundPath, absolute.baz); + + const foundPath2 = await findUp(name.baz, { + cwd: url.fixtureDirectory, + }); + t.is(foundPath2, foundPath); }); test('sync (child file, custom cwd)', t => { @@ -131,6 +140,11 @@ test('sync (child file, custom cwd)', t => { }); t.is(foundPath, absolute.baz); + + const foundPath2 = findUpSync(name.baz, { + cwd: url.fixtureDirectory, + }); + t.is(foundPath2, foundPath); }); test('async (child file, array, custom cwd)', async t => {