1 | import {type Options as LocatePathOptions} from 'locate-path';
|
2 |
|
3 | /**
|
4 | Return this in a `matcher` function to stop the search and force `findUp` to immediately return `undefined`.
|
5 | */
|
6 | export const findUpStop: unique symbol;
|
7 |
|
8 | export type Match = string | typeof findUpStop | undefined;
|
9 |
|
10 | export 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 | /**
|
20 | Find 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
|
37 | import {findUp} from 'find-up';
|
38 |
|
39 | console.log(await findUp('unicorn.png'));
|
40 | //=> '/Users/sindresorhus/unicorn.png'
|
41 |
|
42 | console.log(await findUp(['rainbow.png', 'unicorn.png']));
|
43 | //=> '/Users/sindresorhus/unicorn.png'
|
44 | ```
|
45 | */
|
46 | export function findUp(name: string | readonly string[], options?: Options): Promise<string | undefined>;
|
47 |
|
48 | /**
|
49 | Find 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 | ```
|
56 | import path from 'node:path';
|
57 | import {findUp, pathExists} from 'find-up';
|
58 |
|
59 | console.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 | */
|
66 | export function findUp(matcher: (directory: string) => (Match | Promise<Match>), options?: Options): Promise<string | undefined>;
|
67 |
|
68 | /**
|
69 | Synchronously 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
|
86 | import {findUpSync} from 'find-up';
|
87 |
|
88 | console.log(findUpSync('unicorn.png'));
|
89 | //=> '/Users/sindresorhus/unicorn.png'
|
90 |
|
91 | console.log(findUpSync(['rainbow.png', 'unicorn.png']));
|
92 | //=> '/Users/sindresorhus/unicorn.png'
|
93 | ```
|
94 | */
|
95 | export function findUpSync(name: string | readonly string[], options?: Options): string | undefined;
|
96 |
|
97 | /**
|
98 | Synchronously 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 | ```
|
105 | import path from 'node:path';
|
106 | import {findUpSync, pathExistsSync} from 'find-up';
|
107 |
|
108 | console.log(findUpSync(directory => {
|
109 | const hasUnicorns = pathExistsSync(path.join(directory, 'unicorn.png'));
|
110 | return hasUnicorns && directory;
|
111 | }, {type: 'directory'}));
|
112 | //=> '/Users/sindresorhus'
|
113 | ```
|
114 | */
|
115 | export function findUpSync(matcher: (directory: string) => Match, options?: Options): string | undefined;
|
116 |
|
117 | /**
|
118 | Find 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
|
136 | import {findUpMultiple} from 'find-up';
|
137 |
|
138 | console.log(await findUpMultiple('unicorn.png'));
|
139 | //=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png']
|
140 |
|
141 | console.log(await findUpMultiple(['rainbow.png', 'unicorn.png']));
|
142 | //=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png']
|
143 | ```
|
144 | */
|
145 | export function findUpMultiple(name: string | readonly string[], options?: Options): Promise<string[]>;
|
146 |
|
147 | /**
|
148 | Find 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 | ```
|
155 | import path from 'node:path';
|
156 | import {findUpMultiple, pathExists} from 'find-up';
|
157 |
|
158 | console.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 | */
|
165 | export function findUpMultiple(matcher: (directory: string) => (Match | Promise<Match>), options?: Options): Promise<string[]>;
|
166 |
|
167 | /**
|
168 | Synchronously 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
|
186 | import {findUpMultipleSync} from 'find-up';
|
187 |
|
188 | console.log(findUpMultipleSync('unicorn.png'));
|
189 | //=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png']
|
190 |
|
191 | console.log(findUpMultipleSync(['rainbow.png', 'unicorn.png']));
|
192 | //=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png']
|
193 | ```
|
194 | */
|
195 | export function findUpMultipleSync(name: string | readonly string[], options?: Options): string[];
|
196 |
|
197 | /**
|
198 | Synchronously 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 | ```
|
205 | import path from 'node:path';
|
206 | import {findUpMultipleSync, pathExistsSync} from 'find-up';
|
207 |
|
208 | console.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 | */
|
215 | export function findUpMultipleSync(matcher: (directory: string) => Match, options?: Options): string[];
|
216 |
|
217 | /**
|
218 | Check 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 | ```
|
225 | import {pathExists} from 'find-up';
|
226 |
|
227 | console.log(await pathExists('/Users/sindresorhus/unicorn.png'));
|
228 | //=> true
|
229 | ```
|
230 | */
|
231 | export function pathExists(path: string): Promise<boolean>;
|
232 |
|
233 | /**
|
234 | Synchronously 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 | ```
|
241 | import {pathExistsSync} from 'find-up';
|
242 |
|
243 | console.log(pathExistsSync('/Users/sindresorhus/unicorn.png'));
|
244 | //=> true
|
245 | ```
|
246 | */
|
247 | export function pathExistsSync(path: string): boolean;
|