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.

116 lines
2.9 KiB

4 years ago
  1. # find-cache-dir [![Build Status](https://travis-ci.org/avajs/find-cache-dir.svg?branch=master)](https://travis-ci.org/avajs/find-cache-dir) [![Coverage Status](https://coveralls.io/repos/github/avajs/find-cache-dir/badge.svg?branch=master)](https://coveralls.io/github/avajs/find-cache-dir?branch=master)
  2. > Finds the common standard cache directory
  3. The [`nyc`](https://github.com/istanbuljs/nyc) and [`AVA`](https://ava.li) projects decided to standardize on a common directory structure for storing cache information:
  4. ```sh
  5. # nyc
  6. ./node_modules/.cache/nyc
  7. # ava
  8. ./node_modules/.cache/ava
  9. # your-module
  10. ./node_modules/.cache/your-module
  11. ```
  12. This module makes it easy to correctly locate the cache directory according to this shared spec. If this pattern becomes ubiquitous, clearing the cache for multiple dependencies becomes easy and consistent:
  13. ```
  14. rm -rf ./node_modules/.cache
  15. ```
  16. If you decide to adopt this pattern, please file a PR adding your name to the list of adopters below.
  17. ## Install
  18. ```
  19. $ npm install find-cache-dir
  20. ```
  21. ## Usage
  22. ```js
  23. const findCacheDir = require('find-cache-dir');
  24. findCacheDir({name: 'unicorns'});
  25. //=> '/user/path/node-modules/.cache/unicorns'
  26. ```
  27. ## API
  28. ### findCacheDir([options])
  29. Finds the cache directory using the supplied options. The algorithm tries to find a `package.json` file, searching every parent directory of the `cwd` specified (or implied from other options). It returns a `string` containing the absolute path to the cache directory, or `null` if `package.json` was never found.
  30. #### options
  31. Type: `Object`
  32. ##### name
  33. *Required*<br>
  34. Type: `string`
  35. Should be the same as your project name in `package.json`.
  36. ##### files
  37. Type: `string[]` `string`
  38. An array of files that will be searched for a common parent directory. This common parent directory will be used in lieu of the `cwd` option below.
  39. ##### cwd
  40. Type: `string`<br>
  41. Default `process.cwd()`
  42. Directory to start searching for a `package.json` from.
  43. ##### create
  44. Type: `boolean`<br>
  45. Default `false`
  46. If `true`, the directory will be created synchronously before returning.
  47. ##### thunk
  48. Type: `boolean`<br>
  49. Default `false`
  50. If `true`, this modifies the return type to be a function that is a thunk for `path.join(theFoundCacheDirectory)`.
  51. ```js
  52. const thunk = findCacheDir({name: 'foo', thunk: true});
  53. thunk();
  54. //=> '/some/path/node_modules/.cache/foo'
  55. thunk('bar.js')
  56. //=> '/some/path/node_modules/.cache/foo/bar.js'
  57. thunk('baz', 'quz.js')
  58. //=> '/some/path/node_modules/.cache/foo/baz/quz.js'
  59. ```
  60. This is helpful for actually putting actual files in the cache!
  61. ## Adopters
  62. - [`AVA`](https://ava.li)
  63. - [`nyc`](https://github.com/istanbuljs/nyc)
  64. - [`babel-loader`](https://github.com/babel/babel-loader)
  65. - [`eslint-loader`](https://github.com/MoOx/eslint-loader)
  66. - [`Phenomic`](https://phenomic.io)
  67. - [`javascripthon-loader`](https://github.com/Beg-in/javascripthon-loader)
  68. ## License
  69. MIT