You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.0 KiB

4 years ago
  1. # find-up [![Build Status: Linux and macOS](https://travis-ci.org/sindresorhus/find-up.svg?branch=master)](https://travis-ci.org/sindresorhus/find-up) [![Build Status: Windows](https://ci.appveyor.com/api/projects/status/l0cyjmvh5lq72vq2/branch/master?svg=true)](https://ci.appveyor.com/project/sindresorhus/find-up/branch/master)
  2. > Find a file or directory by walking up parent directories
  3. ## Install
  4. ```
  5. $ npm install find-up
  6. ```
  7. ## Usage
  8. ```
  9. /
  10. └── Users
  11. └── sindresorhus
  12. ├── unicorn.png
  13. └── foo
  14. └── bar
  15. ├── baz
  16. └── example.js
  17. ```
  18. `example.js`
  19. ```js
  20. const findUp = require('find-up');
  21. (async () => {
  22. console.log(await findUp('unicorn.png'));
  23. //=> '/Users/sindresorhus/unicorn.png'
  24. console.log(await findUp(['rainbow.png', 'unicorn.png']));
  25. //=> '/Users/sindresorhus/unicorn.png'
  26. })();
  27. ```
  28. ## API
  29. ### findUp(filename, [options])
  30. Returns a `Promise` for either the filepath or `null` if it couldn't be found.
  31. ### findUp([filenameA, filenameB], [options])
  32. Returns a `Promise` for either the first filepath found (by respecting the order) or `null` if none could be found.
  33. ### findUp.sync(filename, [options])
  34. Returns a filepath or `null`.
  35. ### findUp.sync([filenameA, filenameB], [options])
  36. Returns the first filepath found (by respecting the order) or `null`.
  37. #### filename
  38. Type: `string`
  39. Filename of the file to find.
  40. #### options
  41. Type: `Object`
  42. ##### cwd
  43. Type: `string`<br>
  44. Default: `process.cwd()`
  45. Directory to start from.
  46. ## Related
  47. - [find-up-cli](https://github.com/sindresorhus/find-up-cli) - CLI for this module
  48. - [pkg-up](https://github.com/sindresorhus/pkg-up) - Find the closest package.json file
  49. - [pkg-dir](https://github.com/sindresorhus/pkg-dir) - Find the root directory of an npm package
  50. - [resolve-from](https://github.com/sindresorhus/resolve-from) - Resolve the path of a module like `require.resolve()` but from a given path
  51. ## License
  52. MIT © [Sindre Sorhus](https://sindresorhus.com)