UNPKG

7.42 kBTypeScriptView Raw
1import {type Options as LocatePathOptions} from 'locate-path';
2
3/**
4Return this in a `matcher` function to stop the search and force `findUp` to immediately return `undefined`.
5*/
6export const findUpStop: unique symbol;
7
8export type Match = string | typeof findUpStop | undefined;
9
10export type Options = {
11 /**
12 A directory path where the search halts if no matches are found before reaching this point.
13
14 Default: Root directory
15 */
16 readonly stopAt?: string;
17} & LocatePathOptions;
18
19/**
20Find a file or directory by walking up parent directories.
21
22@param name - The name of the file or directory to find. Can be multiple.
23@returns The first path found (by respecting the order of `name`s) or `undefined` if none could be found.
24
25@example
26```
27// /
28// └── Users
29// └── sindresorhus
30// ├── unicorn.png
31// └── foo
32// └── bar
33// ├── baz
34// └── example.js
35
36// example.js
37import {findUp} from 'find-up';
38
39console.log(await findUp('unicorn.png'));
40//=> '/Users/sindresorhus/unicorn.png'
41
42console.log(await findUp(['rainbow.png', 'unicorn.png']));
43//=> '/Users/sindresorhus/unicorn.png'
44```
45*/
46export function findUp(name: string | readonly string[], options?: Options): Promise<string | undefined>;
47
48/**
49Find a file or directory by walking up parent directories.
50
51@param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search.
52@returns The first path found or `undefined` if none could be found.
53
54@example
55```
56import path from 'node:path';
57import {findUp, pathExists} from 'find-up';
58
59console.log(await findUp(async directory => {
60 const hasUnicorns = await pathExists(path.join(directory, 'unicorn.png'));
61 return hasUnicorns && directory;
62}, {type: 'directory'}));
63//=> '/Users/sindresorhus'
64```
65*/
66export function findUp(matcher: (directory: string) => (Match | Promise<Match>), options?: Options): Promise<string | undefined>;
67
68/**
69Synchronously find a file or directory by walking up parent directories.
70
71@param name - The name of the file or directory to find. Can be multiple.
72@returns The first path found (by respecting the order of `name`s) or `undefined` if none could be found.
73
74@example
75```
76// /
77// └── Users
78// └── sindresorhus
79// ├── unicorn.png
80// └── foo
81// └── bar
82// ├── baz
83// └── example.js
84
85// example.js
86import {findUpSync} from 'find-up';
87
88console.log(findUpSync('unicorn.png'));
89//=> '/Users/sindresorhus/unicorn.png'
90
91console.log(findUpSync(['rainbow.png', 'unicorn.png']));
92//=> '/Users/sindresorhus/unicorn.png'
93```
94*/
95export function findUpSync(name: string | readonly string[], options?: Options): string | undefined;
96
97/**
98Synchronously find a file or directory by walking up parent directories.
99
100@param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search.
101@returns The first path found or `undefined` if none could be found.
102
103@example
104```
105import path from 'node:path';
106import {findUpSync, pathExistsSync} from 'find-up';
107
108console.log(findUpSync(directory => {
109 const hasUnicorns = pathExistsSync(path.join(directory, 'unicorn.png'));
110 return hasUnicorns && directory;
111}, {type: 'directory'}));
112//=> '/Users/sindresorhus'
113```
114*/
115export function findUpSync(matcher: (directory: string) => Match, options?: Options): string | undefined;
116
117/**
118Find files or directories by walking up parent directories.
119
120@param name - The name of the file or directory to find. Can be multiple.
121@returns All paths found (by respecting the order of `name`s) or an empty array if none could be found.
122
123@example
124```
125// /
126// └── Users
127// └── sindresorhus
128// ├── unicorn.png
129// └── foo
130// ├── unicorn.png
131// └── bar
132// ├── baz
133// └── example.js
134
135// example.js
136import {findUpMultiple} from 'find-up';
137
138console.log(await findUpMultiple('unicorn.png'));
139//=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png']
140
141console.log(await findUpMultiple(['rainbow.png', 'unicorn.png']));
142//=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png']
143```
144*/
145export function findUpMultiple(name: string | readonly string[], options?: Options): Promise<string[]>;
146
147/**
148Find files or directories by walking up parent directories.
149
150@param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search.
151@returns All paths found or an empty array if none could be found.
152
153@example
154```
155import path from 'node:path';
156import {findUpMultiple, pathExists} from 'find-up';
157
158console.log(await findUpMultiple(async directory => {
159 const hasUnicorns = await pathExists(path.join(directory, 'unicorn.png'));
160 return hasUnicorns && directory;
161}, {type: 'directory'}));
162//=> ['/Users/sindresorhus/foo', '/Users/sindresorhus']
163```
164*/
165export function findUpMultiple(matcher: (directory: string) => (Match | Promise<Match>), options?: Options): Promise<string[]>;
166
167/**
168Synchronously find files or directories by walking up parent directories.
169
170@param name - The name of the file or directory to find. Can be multiple.
171@returns All paths found (by respecting the order of `name`s) or an empty array if none could be found.
172
173@example
174```
175// /
176// └── Users
177// └── sindresorhus
178// ├── unicorn.png
179// └── foo
180// ├── unicorn.png
181// └── bar
182// ├── baz
183// └── example.js
184
185// example.js
186import {findUpMultipleSync} from 'find-up';
187
188console.log(findUpMultipleSync('unicorn.png'));
189//=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png']
190
191console.log(findUpMultipleSync(['rainbow.png', 'unicorn.png']));
192//=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png']
193```
194*/
195export function findUpMultipleSync(name: string | readonly string[], options?: Options): string[];
196
197/**
198Synchronously find files or directories by walking up parent directories.
199
200@param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search.
201@returns All paths found or an empty array if none could be found.
202
203@example
204```
205import path from 'node:path';
206import {findUpMultipleSync, pathExistsSync} from 'find-up';
207
208console.log(findUpMultipleSync(directory => {
209 const hasUnicorns = pathExistsSync(path.join(directory, 'unicorn.png'));
210 return hasUnicorns && directory;
211}, {type: 'directory'}));
212//=> ['/Users/sindresorhus/foo', '/Users/sindresorhus']
213```
214*/
215export function findUpMultipleSync(matcher: (directory: string) => Match, options?: Options): string[];
216
217/**
218Check if a path exists.
219
220@param path - The path to a file or directory.
221@returns Whether the path exists.
222
223@example
224```
225import {pathExists} from 'find-up';
226
227console.log(await pathExists('/Users/sindresorhus/unicorn.png'));
228//=> true
229```
230*/
231export function pathExists(path: string): Promise<boolean>;
232
233/**
234Synchronously check if a path exists.
235
236@param path - Path to the file or directory.
237@returns Whether the path exists.
238
239@example
240```
241import {pathExistsSync} from 'find-up';
242
243console.log(pathExistsSync('/Users/sindresorhus/unicorn.png'));
244//=> true
245```
246*/
247export function pathExistsSync(path: string): boolean;