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.

98 lines
3.6 KiB

4 years ago
  1. # detect-file [![NPM version](https://img.shields.io/npm/v/detect-file.svg?style=flat)](https://www.npmjs.com/package/detect-file) [![NPM monthly downloads](https://img.shields.io/npm/dm/detect-file.svg?style=flat)](https://npmjs.org/package/detect-file) [![NPM total downloads](https://img.shields.io/npm/dt/detect-file.svg?style=flat)](https://npmjs.org/package/detect-file) [![Linux Build Status](https://img.shields.io/travis/doowb/detect-file.svg?style=flat&label=Travis)](https://travis-ci.org/doowb/detect-file) [![Windows Build Status](https://img.shields.io/appveyor/ci/doowb/detect-file.svg?style=flat&label=AppVeyor)](https://ci.appveyor.com/project/doowb/detect-file)
  2. > Detects if a file exists and returns the resolved filepath.
  3. ## Install
  4. Install with [npm](https://www.npmjs.com/):
  5. ```sh
  6. $ npm install --save detect-file
  7. ```
  8. Install with [yarn](https://yarnpkg.com):
  9. ```sh
  10. $ yarn add detect-file
  11. ```
  12. ## Usage
  13. ```js
  14. var detect = require('detect-file');
  15. ```
  16. ## API
  17. ### [detect](index.js#L33)
  18. Detect the given `filepath` if it exists.
  19. **Params**
  20. * `filepath` **{String}**: filepath to detect.
  21. * `options` **{Object}**: Additional options.
  22. * `options.nocase` **{Boolean}**: Set this to `true` to force case-insensitive filename checks. This is useful on case sensitive file systems.
  23. * `returns` **{String}**: Returns the detected filepath if it exists, otherwise returns `null`.
  24. **Example**
  25. ```js
  26. var res = detect('package.json');
  27. console.log(res);
  28. //=> "package.json"
  29. var res = detect('fake-file.json');
  30. console.log(res)
  31. //=> null
  32. ```
  33. ## Case sensitive file systems
  34. When using the `nocase` option, this library will attempt to detect the filepath with the following methods:
  35. 1. Try to read all files in the `filepath` using `fs.readdirSync`. If successful and `filepath` is a directory, return the `filepath`.
  36. 2. Try to read all files in the `filepath`'s directory using `fs.readdirSync`. If successful, do case insensitive comparasions of the `filepath` to the files in `filepath`'s directory.
  37. ## About
  38. ### Related projects
  39. [fs-exists-sync](https://www.npmjs.com/package/fs-exists-sync): Drop-in replacement for `fs.existsSync` with zero dependencies. Other libs I found either have crucial differences… [more](https://github.com/jonschlinkert/fs-exists-sync) | [homepage](https://github.com/jonschlinkert/fs-exists-sync "Drop-in replacement for `fs.existsSync` with zero dependencies. Other libs I found either have crucial differences from fs.existsSync, or unnecessary dependencies. See README.md for more info.")
  40. ### Contributing
  41. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  42. ### Building docs
  43. _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
  44. To generate the readme, run the following command:
  45. ```sh
  46. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  47. ```
  48. ### Running tests
  49. Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
  50. ```sh
  51. $ npm install && npm test
  52. ```
  53. ### Author
  54. **Brian Woodward**
  55. * [github/doowb](https://github.com/doowb)
  56. * [twitter/doowb](https://twitter.com/doowb)
  57. ### License
  58. Copyright © 2017, [Brian Woodward](https://github.com/doowb).
  59. Released under the [MIT License](LICENSE).
  60. ***
  61. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on August 05, 2017._