Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

index.d.ts 3.7KB

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