UNPKG

3.68 kBTypeScriptView Raw
1import {Options as LocatePathOptions} from 'locate-path';
2
3declare const stop: unique symbol;
4
5declare namespace findUp {
6 interface Options extends LocatePathOptions {}
7
8 type StopSymbol = typeof stop;
9
10 type Match = string | StopSymbol | undefined;
11}
12
13declare const findUp: {
14 /**
15 Find a file or directory by walking up parent directories.
16
17 @param name - Name of the file or directory to find. Can be multiple.
18 @returns The first path found (by respecting the order of `name`s) or `undefined` if none could be found.
19
20 @example
21 ```
22 // /
23 // └── Users
24 // └── sindresorhus
25 // ├── unicorn.png
26 // └── foo
27 // └── bar
28 // ├── baz
29 // └── example.js
30
31 // example.js
32 import findUp = require('find-up');
33
34 (async () => {
35 console.log(await findUp('unicorn.png'));
36 //=> '/Users/sindresorhus/unicorn.png'
37
38 console.log(await findUp(['rainbow.png', 'unicorn.png']));
39 //=> '/Users/sindresorhus/unicorn.png'
40 })();
41 ```
42 */
43 (name: string | string[], options?: findUp.Options): Promise<string | undefined>;
44
45 /**
46 Find a file or directory by walking up parent directories.
47
48 @param matcher - Called for each directory in the search. Return a path or `findUp.stop` to stop the search.
49 @returns The first path found or `undefined` if none could be found.
50
51 @example
52 ```
53 import path = require('path');
54 import findUp = require('find-up');
55
56 (async () => {
57 console.log(await findUp(async directory => {
58 const hasUnicorns = await findUp.exists(path.join(directory, 'unicorn.png'));
59 return hasUnicorns && directory;
60 }, {type: 'directory'}));
61 //=> '/Users/sindresorhus'
62 })();
63 ```
64 */
65 (matcher: (directory: string) => (findUp.Match | Promise<findUp.Match>), options?: findUp.Options): Promise<string | undefined>;
66
67 sync: {
68 /**
69 Synchronously find a file or directory by walking up parent directories.
70
71 @param name - 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 (name: string | string[], options?: findUp.Options): string | undefined;
75
76 /**
77 Synchronously find a file or directory by walking up parent directories.
78
79 @param matcher - Called for each directory in the search. Return a path or `findUp.stop` to stop the search.
80 @returns The first path found or `undefined` if none could be found.
81
82 @example
83 ```
84 import path = require('path');
85 import findUp = require('find-up');
86
87 console.log(findUp.sync(directory => {
88 const hasUnicorns = findUp.sync.exists(path.join(directory, 'unicorn.png'));
89 return hasUnicorns && directory;
90 }, {type: 'directory'}));
91 //=> '/Users/sindresorhus'
92 ```
93 */
94 (matcher: (directory: string) => findUp.Match, options?: findUp.Options): string | undefined;
95
96 /**
97 Synchronously check if a path exists.
98
99 @param path - Path to the file or directory.
100 @returns Whether the path exists.
101
102 @example
103 ```
104 import findUp = require('find-up');
105
106 console.log(findUp.sync.exists('/Users/sindresorhus/unicorn.png'));
107 //=> true
108 ```
109 */
110 exists(path: string): boolean;
111 }
112
113 /**
114 Check if a path exists.
115
116 @param path - Path to a file or directory.
117 @returns Whether the path exists.
118
119 @example
120 ```
121 import findUp = require('find-up');
122
123 (async () => {
124 console.log(await findUp.exists('/Users/sindresorhus/unicorn.png'));
125 //=> true
126 })();
127 ```
128 */
129 exists(path: string): Promise<boolean>;
130
131 /**
132 Return this in a `matcher` function to stop the search and force `findUp` to immediately return `undefined`.
133 */
134 readonly stop: findUp.StopSymbol;
135};
136
137export = findUp;