Skip to content

Commit f348f3c

Browse files
authored
Accept URL as cwd (#60)
1 parent db9962c commit f348f3c

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import path from 'node:path';
2+
import {fileURLToPath} from 'node:url';
23
import {locatePath, locatePathSync} from 'locate-path';
34

5+
const toPath = urlOrPath => urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath;
6+
47
export const findUpStop = Symbol('findUpStop');
58

69
export async function findUpMultiple(name, options = {}) {
7-
let directory = path.resolve(options.cwd || '');
10+
let directory = path.resolve(toPath(options.cwd) || '');
811
const {root} = path.parse(directory);
912
const stopAt = path.resolve(directory, options.stopAt || root);
1013
const limit = options.limit || Number.POSITIVE_INFINITY;
@@ -48,7 +51,7 @@ export async function findUpMultiple(name, options = {}) {
4851
}
4952

5053
export function findUpMultipleSync(name, options = {}) {
51-
let directory = path.resolve(options.cwd || '');
54+
let directory = path.resolve(toPath(options.cwd) || '');
5255
const {root} = path.parse(directory);
5356
const stopAt = options.stopAt || root;
5457
const limit = options.limit || Number.POSITIVE_INFINITY;

index.test-d.ts

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {findUp, findUpSync, findUpMultiple, findUpMultipleSync, findUpStop, path
33

44
expectType<Promise<string | undefined>>(findUp('unicorn.png'));
55
expectType<Promise<string | undefined>>(findUp('unicorn.png', {cwd: ''}));
6+
expectType<Promise<string | undefined>>(findUp('unicorn.png', {cwd: new URL('file:///path/to/cwd/')}));
67
expectType<Promise<string | undefined>>(findUp(['rainbow.png', 'unicorn.png']));
78
expectType<Promise<string | undefined>>(findUp(['rainbow.png', 'unicorn.png'], {cwd: ''}));
89
expectType<Promise<string | undefined>>(findUp(['rainbow.png', 'unicorn.png'], {allowSymlinks: true}));
@@ -14,20 +15,23 @@ expectError(findUp(['rainbow.png', 'unicorn.png'], {concurrency: 1}));
1415

1516
expectType<Promise<string | undefined>>(findUp(() => 'unicorn.png'));
1617
expectType<Promise<string | undefined>>(findUp(() => 'unicorn.png', {cwd: ''}));
18+
expectType<Promise<string | undefined>>(findUp(() => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')}));
1719
expectType<Promise<string | undefined>>(findUp(() => 'unicorn.png', {allowSymlinks: true}));
1820
expectType<Promise<string | undefined>>(findUp(() => 'unicorn.png', {allowSymlinks: false}));
1921
expectType<Promise<string | undefined>>(findUp(() => 'unicorn.png', {type: 'file'}));
2022
expectType<Promise<string | undefined>>(findUp(() => 'unicorn.png', {type: 'directory'}));
2123
expectType<Promise<string | undefined>>(findUp(() => 'unicorn.png', {stopAt: 'foo'}));
2224
expectType<Promise<string | undefined>>(findUp(() => undefined));
2325
expectType<Promise<string | undefined>>(findUp(() => undefined, {cwd: ''}));
26+
expectType<Promise<string | undefined>>(findUp(() => undefined, {cwd: new URL('file:///path/to/cwd/')}));
2427
expectType<Promise<string | undefined>>(findUp(() => undefined, {allowSymlinks: true}));
2528
expectType<Promise<string | undefined>>(findUp(() => undefined, {allowSymlinks: false}));
2629
expectType<Promise<string | undefined>>(findUp(() => undefined, {type: 'file'}));
2730
expectType<Promise<string | undefined>>(findUp(() => undefined, {type: 'directory'}));
2831
expectType<Promise<string | undefined>>(findUp(() => undefined, {stopAt: 'foo'}));
2932
expectType<Promise<string | undefined>>(findUp((): typeof findUpStop => findUpStop));
3033
expectType<Promise<string | undefined>>(findUp((): typeof findUpStop => findUpStop, {cwd: ''}));
34+
expectType<Promise<string | undefined>>(findUp((): typeof findUpStop => findUpStop, {cwd: new URL('file:///path/to/cwd/')}));
3135
expectType<Promise<string | undefined>>(findUp((): typeof findUpStop => findUpStop, {stopAt: 'foo'}));
3236
expectType<Promise<string | undefined>>(findUp(async () => 'unicorn.png'));
3337
expectType<Promise<string | undefined>>(findUp(async () => 'unicorn.png', {cwd: ''}));
@@ -38,6 +42,7 @@ expectType<Promise<string | undefined>>(findUp(async () => 'unicorn.png', {type:
3842
expectType<Promise<string | undefined>>(findUp(async () => 'unicorn.png', {stopAt: 'foo'}));
3943
expectType<Promise<string | undefined>>(findUp(async () => undefined));
4044
expectType<Promise<string | undefined>>(findUp(async () => undefined, {cwd: ''}));
45+
expectType<Promise<string | undefined>>(findUp(async () => undefined, {cwd: new URL('file:///path/to/cwd/')}));
4146
expectType<Promise<string | undefined>>(findUp(async () => undefined, {allowSymlinks: true}));
4247
expectType<Promise<string | undefined>>(findUp(async () => undefined, {allowSymlinks: false}));
4348
expectType<Promise<string | undefined>>(findUp(async () => undefined, {type: 'file'}));
@@ -46,6 +51,7 @@ expectType<Promise<string | undefined>>(findUp(async () => undefined, {stopAt: '
4651

4752
expectType<Promise<string | undefined>>(findUp(async (): Promise<typeof findUpStop> => findUpStop));
4853
expectType<Promise<string | undefined>>(findUp(async (): Promise<typeof findUpStop> => findUpStop, {cwd: ''}));
54+
expectType<Promise<string | undefined>>(findUp(async (): Promise<typeof findUpStop> => findUpStop, {cwd: new URL('file:///path/to/cwd/')}));
4955
expectType<Promise<string | undefined>>(findUp(async (): Promise<typeof findUpStop> => findUpStop, {allowSymlinks: true}));
5056
expectType<Promise<string | undefined>>(findUp(async (): Promise<typeof findUpStop> => findUpStop, {allowSymlinks: false}));
5157
expectType<Promise<string | undefined>>(findUp(async (): Promise<typeof findUpStop> => findUpStop, {type: 'file'}));
@@ -54,8 +60,10 @@ expectType<Promise<string | undefined>>(findUp(async (): Promise<typeof findUpSt
5460

5561
expectType<Promise<string[]>>(findUpMultiple('unicorn.png'));
5662
expectType<Promise<string[]>>(findUpMultiple('unicorn.png', {cwd: ''}));
63+
expectType<Promise<string[]>>(findUpMultiple('unicorn.png', {cwd: new URL('file:///path/to/cwd/')}));
5764
expectType<Promise<string[]>>(findUpMultiple(['rainbow.png', 'unicorn.png']));
5865
expectType<Promise<string[]>>(findUpMultiple(['rainbow.png', 'unicorn.png'], {cwd: ''}));
66+
expectType<Promise<string[]>>(findUpMultiple(['rainbow.png', 'unicorn.png'], {cwd: new URL('file:///path/to/cwd/')}));
5967
expectType<Promise<string[]>>(findUpMultiple(['rainbow.png', 'unicorn.png'], {allowSymlinks: true}));
6068
expectType<Promise<string[]>>(findUpMultiple(['rainbow.png', 'unicorn.png'], {allowSymlinks: false}));
6169
expectType<Promise<string[]>>(findUpMultiple(['rainbow.png', 'unicorn.png'], {type: 'file'}));
@@ -65,30 +73,35 @@ expectError(findUpMultiple(['rainbow.png', 'unicorn.png'], {concurrency: 1}));
6573

6674
expectType<Promise<string[]>>(findUpMultiple(() => 'unicorn.png'));
6775
expectType<Promise<string[]>>(findUpMultiple(() => 'unicorn.png', {cwd: ''}));
76+
expectType<Promise<string[]>>(findUpMultiple(() => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')}));
6877
expectType<Promise<string[]>>(findUpMultiple(() => 'unicorn.png', {allowSymlinks: true}));
6978
expectType<Promise<string[]>>(findUpMultiple(() => 'unicorn.png', {allowSymlinks: false}));
7079
expectType<Promise<string[]>>(findUpMultiple(() => 'unicorn.png', {type: 'file'}));
7180
expectType<Promise<string[]>>(findUpMultiple(() => 'unicorn.png', {type: 'directory'}));
7281
expectType<Promise<string[]>>(findUpMultiple(() => 'unicorn.png', {stopAt: 'foo'}));
7382
expectType<Promise<string[]>>(findUpMultiple(() => undefined));
7483
expectType<Promise<string[]>>(findUpMultiple(() => undefined, {cwd: ''}));
84+
expectType<Promise<string[]>>(findUpMultiple(() => undefined, {cwd: new URL('file:///path/to/cwd/')}));
7585
expectType<Promise<string[]>>(findUpMultiple(() => undefined, {allowSymlinks: true}));
7686
expectType<Promise<string[]>>(findUpMultiple(() => undefined, {allowSymlinks: false}));
7787
expectType<Promise<string[]>>(findUpMultiple(() => undefined, {type: 'file'}));
7888
expectType<Promise<string[]>>(findUpMultiple(() => undefined, {type: 'directory'}));
7989
expectType<Promise<string[]>>(findUpMultiple(() => undefined, {stopAt: 'foo'}));
8090
expectType<Promise<string[]>>(findUpMultiple((): typeof findUpStop => findUpStop));
8191
expectType<Promise<string[]>>(findUpMultiple((): typeof findUpStop => findUpStop, {cwd: ''}));
92+
expectType<Promise<string[]>>(findUpMultiple((): typeof findUpStop => findUpStop, {cwd: new URL('file:///path/to/cwd/')}));
8293
expectType<Promise<string[]>>(findUpMultiple((): typeof findUpStop => findUpStop, {stopAt: 'foo'}));
8394
expectType<Promise<string[]>>(findUpMultiple(async () => 'unicorn.png'));
8495
expectType<Promise<string[]>>(findUpMultiple(async () => 'unicorn.png', {cwd: ''}));
96+
expectType<Promise<string[]>>(findUpMultiple(async () => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')}));
8597
expectType<Promise<string[]>>(findUpMultiple(async () => 'unicorn.png', {allowSymlinks: true}));
8698
expectType<Promise<string[]>>(findUpMultiple(async () => 'unicorn.png', {allowSymlinks: false}));
8799
expectType<Promise<string[]>>(findUpMultiple(async () => 'unicorn.png', {type: 'file'}));
88100
expectType<Promise<string[]>>(findUpMultiple(async () => 'unicorn.png', {type: 'directory'}));
89101
expectType<Promise<string[]>>(findUpMultiple(async () => 'unicorn.png', {stopAt: 'foo'}));
90102
expectType<Promise<string[]>>(findUpMultiple(async () => undefined));
91103
expectType<Promise<string[]>>(findUpMultiple(async () => undefined, {cwd: ''}));
104+
expectType<Promise<string[]>>(findUpMultiple(async () => undefined, {cwd: new URL('file:///path/to/cwd/')}));
92105
expectType<Promise<string[]>>(findUpMultiple(async () => undefined, {allowSymlinks: true}));
93106
expectType<Promise<string[]>>(findUpMultiple(async () => undefined, {allowSymlinks: false}));
94107
expectType<Promise<string[]>>(findUpMultiple(async () => undefined, {type: 'file'}));
@@ -97,6 +110,7 @@ expectType<Promise<string[]>>(findUpMultiple(async () => undefined, {stopAt: 'fo
97110

98111
expectType<Promise<string[]>>(findUpMultiple(async (): Promise<typeof findUpStop> => findUpStop));
99112
expectType<Promise<string[]>>(findUpMultiple(async (): Promise<typeof findUpStop> => findUpStop, {cwd: ''}));
113+
expectType<Promise<string[]>>(findUpMultiple(async (): Promise<typeof findUpStop> => findUpStop, {cwd: new URL('file:///path/to/cwd/')}));
100114
expectType<Promise<string[]>>(findUpMultiple(async (): Promise<typeof findUpStop> => findUpStop, {allowSymlinks: true}));
101115
expectType<Promise<string[]>>(findUpMultiple(async (): Promise<typeof findUpStop> => findUpStop, {allowSymlinks: false}));
102116
expectType<Promise<string[]>>(findUpMultiple(async (): Promise<typeof findUpStop> => findUpStop, {type: 'file'}));
@@ -105,8 +119,10 @@ expectType<Promise<string[]>>(findUpMultiple(async (): Promise<typeof findUpStop
105119

106120
expectType<string | undefined>(findUpSync('unicorn.png'));
107121
expectType<string | undefined>(findUpSync('unicorn.png', {cwd: ''}));
122+
expectType<string | undefined>(findUpSync('unicorn.png', {cwd: new URL('file:///path/to/cwd/')}));
108123
expectType<string | undefined>(findUpSync(['rainbow.png', 'unicorn.png']));
109124
expectType<string | undefined>(findUpSync(['rainbow.png', 'unicorn.png'], {cwd: ''}));
125+
expectType<string | undefined>(findUpSync(['rainbow.png', 'unicorn.png'], {cwd: new URL('file:///path/to/cwd/')}));
110126
expectType<string | undefined>(findUpSync(['rainbow.png', 'unicorn.png'], {allowSymlinks: true}));
111127
expectType<string | undefined>(findUpSync(['rainbow.png', 'unicorn.png'], {allowSymlinks: false}));
112128
expectType<string | undefined>(findUpSync(['rainbow.png', 'unicorn.png'], {type: 'file'}));
@@ -115,49 +131,57 @@ expectType<string | undefined>(findUpSync(['rainbow.png', 'unicorn.png'], {stopA
115131

116132
expectType<string | undefined>(findUpSync(() => 'unicorn.png'));
117133
expectType<string | undefined>(findUpSync(() => 'unicorn.png', {cwd: ''}));
134+
expectType<string | undefined>(findUpSync(() => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')}));
118135
expectType<string | undefined>(findUpSync(() => 'unicorn.png', {allowSymlinks: true}));
119136
expectType<string | undefined>(findUpSync(() => 'unicorn.png', {allowSymlinks: false}));
120137
expectType<string | undefined>(findUpSync(() => 'unicorn.png', {type: 'file'}));
121138
expectType<string | undefined>(findUpSync(() => 'unicorn.png', {type: 'directory'}));
122139
expectType<string | undefined>(findUpSync(() => 'unicorn.png', {stopAt: 'foo'}));
123140
expectType<string | undefined>(findUpSync(() => undefined));
124141
expectType<string | undefined>(findUpSync(() => undefined, {cwd: ''}));
142+
expectType<string | undefined>(findUpSync(() => undefined, {cwd: new URL('file:///path/to/cwd/')}));
125143
expectType<string | undefined>(findUpSync(() => undefined, {allowSymlinks: true}));
126144
expectType<string | undefined>(findUpSync(() => undefined, {allowSymlinks: false}));
127145
expectType<string | undefined>(findUpSync(() => undefined, {type: 'file'}));
128146
expectType<string | undefined>(findUpSync(() => undefined, {type: 'directory'}));
129147
expectType<string | undefined>(findUpSync(() => undefined, {stopAt: 'foo'}));
130148
expectType<string | undefined>(findUpSync((): typeof findUpStop => findUpStop));
131149
expectType<string | undefined>(findUpSync((): typeof findUpStop => findUpStop, {cwd: ''}));
150+
expectType<string | undefined>(findUpSync((): typeof findUpStop => findUpStop, {cwd: new URL('file:///path/to/cwd/')}));
132151
expectType<string | undefined>(findUpSync((): typeof findUpStop => findUpStop, {type: 'file'}));
133152
expectType<string | undefined>(findUpSync((): typeof findUpStop => findUpStop, {type: 'directory'}));
134153
expectType<string | undefined>(findUpSync((): typeof findUpStop => findUpStop, {stopAt: 'foo'}));
135154

136155
expectType<string[]>(findUpMultipleSync('unicorn.png'));
137156
expectType<string[]>(findUpMultipleSync('unicorn.png', {cwd: ''}));
157+
expectType<string[]>(findUpMultipleSync('unicorn.png', {cwd: new URL('file:///path/to/cwd/')}));
138158
expectType<string[]>(findUpMultipleSync(['rainbow.png', 'unicorn.png']));
139159
expectType<string[]>(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {cwd: ''}));
160+
expectType<string[]>(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {cwd: new URL('file:///path/to/cwd/')}));
140161
expectType<string[]>(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {allowSymlinks: true}));
141162
expectType<string[]>(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {allowSymlinks: false}));
142163
expectType<string[]>(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {type: 'file'}));
143164
expectType<string[]>(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {type: 'directory'}));
144165
expectType<string[]>(findUpMultipleSync(['rainbow.png', 'unicorn.png'], {stopAt: 'foo'}));
145166
expectType<string[]>(findUpMultipleSync(() => 'unicorn.png'));
146167
expectType<string[]>(findUpMultipleSync(() => 'unicorn.png', {cwd: ''}));
168+
expectType<string[]>(findUpMultipleSync(() => 'unicorn.png', {cwd: new URL('file:///path/to/cwd/')}));
147169
expectType<string[]>(findUpMultipleSync(() => 'unicorn.png', {allowSymlinks: true}));
148170
expectType<string[]>(findUpMultipleSync(() => 'unicorn.png', {allowSymlinks: false}));
149171
expectType<string[]>(findUpMultipleSync(() => 'unicorn.png', {type: 'file'}));
150172
expectType<string[]>(findUpMultipleSync(() => 'unicorn.png', {type: 'directory'}));
151173
expectType<string[]>(findUpMultipleSync(() => 'unicorn.png', {stopAt: 'foo'}));
152174
expectType<string[]>(findUpMultipleSync(() => undefined));
153175
expectType<string[]>(findUpMultipleSync(() => undefined, {cwd: ''}));
176+
expectType<string[]>(findUpMultipleSync(() => undefined, {cwd: new URL('file:///path/to/cwd/')}));
154177
expectType<string[]>(findUpMultipleSync(() => undefined, {allowSymlinks: true}));
155178
expectType<string[]>(findUpMultipleSync(() => undefined, {allowSymlinks: false}));
156179
expectType<string[]>(findUpMultipleSync(() => undefined, {type: 'file'}));
157180
expectType<string[]>(findUpMultipleSync(() => undefined, {type: 'directory'}));
158181
expectType<string[]>(findUpMultipleSync(() => undefined, {stopAt: 'foo'}));
159182
expectType<string[]>(findUpMultipleSync((): typeof findUpStop => findUpStop));
160183
expectType<string[]>(findUpMultipleSync((): typeof findUpStop => findUpStop, {cwd: ''}));
184+
expectType<string[]>(findUpMultipleSync((): typeof findUpStop => findUpStop, {cwd: new URL('file:///path/to/cwd/')}));
161185
expectType<string[]>(findUpMultipleSync((): typeof findUpStop => findUpStop, {type: 'file'}));
162186
expectType<string[]>(findUpMultipleSync((): typeof findUpStop => findUpStop, {type: 'directory'}));
163187
expectType<string[]>(findUpMultipleSync((): typeof findUpStop => findUpStop, {stopAt: 'foo'}));

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"path"
4444
],
4545
"dependencies": {
46-
"locate-path": "^7.0.0",
46+
"locate-path": "^7.1.0",
4747
"path-exists": "^5.0.0"
4848
},
4949
"devDependencies": {

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Type: `object`
9898

9999
##### cwd
100100

101-
Type: `string`\
101+
Type: `URL | string`\
102102
Default: `process.cwd()`
103103

104104
The directory to start from.

test.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import process from 'node:process';
22
import {promisify} from 'node:util';
33
import fs from 'node:fs';
44
import path from 'node:path';
5-
import {fileURLToPath} from 'node:url';
5+
import {fileURLToPath, pathToFileURL} from 'node:url';
66
import test from 'ava';
77
import isPathInside from 'is-path-inside';
88
import tempy from 'tempy';
@@ -49,6 +49,10 @@ absolute.barDirQux = path.join(absolute.fixtureDirectory, name.fooDirectory, nam
4949
absolute.fileLink = path.join(absolute.fixtureDirectory, name.fileLink);
5050
absolute.directoryLink = path.join(absolute.fixtureDirectory, name.directoryLink);
5151

52+
const url = {
53+
fixtureDirectory: pathToFileURL(absolute.fixtureDirectory),
54+
};
55+
5256
// Create a disjoint directory, used for the not-found tests
5357
test.beforeEach(t => {
5458
const temporaryDirectory = tempy.directory();
@@ -123,6 +127,11 @@ test('async (child file, custom cwd)', async t => {
123127
});
124128

125129
t.is(foundPath, absolute.baz);
130+
131+
const foundPath2 = await findUp(name.baz, {
132+
cwd: url.fixtureDirectory,
133+
});
134+
t.is(foundPath2, foundPath);
126135
});
127136

128137
test('sync (child file, custom cwd)', t => {
@@ -131,6 +140,11 @@ test('sync (child file, custom cwd)', t => {
131140
});
132141

133142
t.is(foundPath, absolute.baz);
143+
144+
const foundPath2 = findUpSync(name.baz, {
145+
cwd: url.fixtureDirectory,
146+
});
147+
t.is(foundPath2, foundPath);
134148
});
135149

136150
test('async (child file, array, custom cwd)', async t => {

0 commit comments

Comments
 (0)