index.d.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* eslint-disable @typescript-eslint/unified-signatures */
  2. import {Options as LocatePathOptions} from 'locate-path';
  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. export type Match = string | typeof findUpStop | undefined;
  8. export interface Options extends LocatePathOptions {
  9. /**
  10. The path to the directory to stop the search before reaching root if there were no matches before the `stopAt` directory.
  11. @default path.parse(cwd).root
  12. */
  13. readonly stopAt?: string;
  14. }
  15. /**
  16. Find a file or directory by walking up parent directories.
  17. @param name - The 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. @example
  20. ```
  21. // /
  22. // └── Users
  23. // └── sindresorhus
  24. // ├── unicorn.png
  25. // └── foo
  26. // └── bar
  27. // ├── baz
  28. // └── example.js
  29. // example.js
  30. import {findUp} from 'find-up';
  31. console.log(await findUp('unicorn.png'));
  32. //=> '/Users/sindresorhus/unicorn.png'
  33. console.log(await findUp(['rainbow.png', 'unicorn.png']));
  34. //=> '/Users/sindresorhus/unicorn.png'
  35. ```
  36. */
  37. export function findUp(name: string | readonly string[], options?: Options): Promise<string | undefined>;
  38. /**
  39. Find a file or directory by walking up parent directories.
  40. @param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search.
  41. @returns The first path found or `undefined` if none could be found.
  42. @example
  43. ```
  44. import path from 'node:path';
  45. import {findUp, pathExists} from 'find-up';
  46. console.log(await findUp(async directory => {
  47. const hasUnicorns = await pathExists(path.join(directory, 'unicorn.png'));
  48. return hasUnicorns && directory;
  49. }, {type: 'directory'}));
  50. //=> '/Users/sindresorhus'
  51. ```
  52. */
  53. export function findUp(matcher: (directory: string) => (Match | Promise<Match>), options?: Options): Promise<string | undefined>;
  54. /**
  55. Synchronously find a file or directory by walking up parent directories.
  56. @param name - The name of the file or directory to find. Can be multiple.
  57. @returns The first path found (by respecting the order of `name`s) or `undefined` if none could be found.
  58. @example
  59. ```
  60. // /
  61. // └── Users
  62. // └── sindresorhus
  63. // ├── unicorn.png
  64. // └── foo
  65. // └── bar
  66. // ├── baz
  67. // └── example.js
  68. // example.js
  69. import {findUpSync} from 'find-up';
  70. console.log(findUpSync('unicorn.png'));
  71. //=> '/Users/sindresorhus/unicorn.png'
  72. console.log(findUpSync(['rainbow.png', 'unicorn.png']));
  73. //=> '/Users/sindresorhus/unicorn.png'
  74. ```
  75. */
  76. export function findUpSync(name: string | readonly string[], options?: Options): string | undefined;
  77. /**
  78. Synchronously find a file or directory by walking up parent directories.
  79. @param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search.
  80. @returns The first path found or `undefined` if none could be found.
  81. @example
  82. ```
  83. import path from 'node:path';
  84. import {findUpSync, pathExistsSync} from 'find-up';
  85. console.log(findUpSync(directory => {
  86. const hasUnicorns = pathExistsSync(path.join(directory, 'unicorn.png'));
  87. return hasUnicorns && directory;
  88. }, {type: 'directory'}));
  89. //=> '/Users/sindresorhus'
  90. ```
  91. */
  92. export function findUpSync(matcher: (directory: string) => Match, options?: Options): string | undefined;
  93. /**
  94. Find files or directories by walking up parent directories.
  95. @param name - The name of the file or directory to find. Can be multiple.
  96. @returns All paths found (by respecting the order of `name`s) or an empty array if none could be found.
  97. @example
  98. ```
  99. // /
  100. // └── Users
  101. // └── sindresorhus
  102. // ├── unicorn.png
  103. // └── foo
  104. // ├── unicorn.png
  105. // └── bar
  106. // ├── baz
  107. // └── example.js
  108. // example.js
  109. import {findUpMultiple} from 'find-up';
  110. console.log(await findUpMultiple('unicorn.png'));
  111. //=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png']
  112. console.log(await findUpMultiple(['rainbow.png', 'unicorn.png']));
  113. //=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png']
  114. ```
  115. */
  116. export function findUpMultiple(name: string | readonly string[], options?: Options): Promise<string[]>;
  117. /**
  118. Find files or directories by walking up parent directories.
  119. @param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search.
  120. @returns All paths found or an empty array if none could be found.
  121. @example
  122. ```
  123. import path from 'node:path';
  124. import {findUpMultiple, pathExists} from 'find-up';
  125. console.log(await findUpMultiple(async directory => {
  126. const hasUnicorns = await pathExists(path.join(directory, 'unicorn.png'));
  127. return hasUnicorns && directory;
  128. }, {type: 'directory'}));
  129. //=> ['/Users/sindresorhus/foo', '/Users/sindresorhus']
  130. ```
  131. */
  132. export function findUpMultiple(matcher: (directory: string) => (Match | Promise<Match>), options?: Options): Promise<string[]>;
  133. /**
  134. Synchronously find files or directories by walking up parent directories.
  135. @param name - The name of the file or directory to find. Can be multiple.
  136. @returns All paths found (by respecting the order of `name`s) or an empty array if none could be found.
  137. @example
  138. ```
  139. // /
  140. // └── Users
  141. // └── sindresorhus
  142. // ├── unicorn.png
  143. // └── foo
  144. // ├── unicorn.png
  145. // └── bar
  146. // ├── baz
  147. // └── example.js
  148. // example.js
  149. import {findUpMultipleSync} from 'find-up';
  150. console.log(findUpMultipleSync('unicorn.png'));
  151. //=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png']
  152. console.log(findUpMultipleSync(['rainbow.png', 'unicorn.png']));
  153. //=> ['/Users/sindresorhus/foo/unicorn.png', '/Users/sindresorhus/unicorn.png']
  154. ```
  155. */
  156. export function findUpMultipleSync(name: string | readonly string[], options?: Options): string[];
  157. /**
  158. Synchronously find files or directories by walking up parent directories.
  159. @param matcher - Called for each directory in the search. Return a path or `findUpStop` to stop the search.
  160. @returns All paths found or an empty array if none could be found.
  161. @example
  162. ```
  163. import path from 'node:path';
  164. import {findUpMultipleSync, pathExistsSync} from 'find-up';
  165. console.log(findUpMultipleSync(directory => {
  166. const hasUnicorns = pathExistsSync(path.join(directory, 'unicorn.png'));
  167. return hasUnicorns && directory;
  168. }, {type: 'directory'}));
  169. //=> ['/Users/sindresorhus/foo', '/Users/sindresorhus']
  170. ```
  171. */
  172. export function findUpMultipleSync(matcher: (directory: string) => Match, options?: Options): string[];
  173. /**
  174. Check if a path exists.
  175. @param path - The path to a file or directory.
  176. @returns Whether the path exists.
  177. @example
  178. ```
  179. import {pathExists} from 'find-up';
  180. console.log(await pathExists('/Users/sindresorhus/unicorn.png'));
  181. //=> true
  182. ```
  183. */
  184. export function pathExists(path: string): Promise<boolean>;
  185. /**
  186. Synchronously check if a path exists.
  187. @param path - Path to the file or directory.
  188. @returns Whether the path exists.
  189. @example
  190. ```
  191. import {pathExistsSync} from 'find-up';
  192. console.log(pathExistsSync('/Users/sindresorhus/unicorn.png'));
  193. //=> true
  194. ```
  195. */
  196. export function pathExistsSync(path: string): boolean;