UNPKG

3.73 kBMarkdownView Raw
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
3> Finds the common standard cache directory
4
5The [`nyc`](https://github.com/istanbuljs/nyc) and [`AVA`](https://ava.li) projects decided to standardize on a common directory structure for storing cache information:
6
7```sh
8# nyc
9./node_modules/.cache/nyc
10
11# ava
12./node_modules/.cache/ava
13
14# your-module
15./node_modules/.cache/your-module
16```
17
18This 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:
19
20```
21rm -rf ./node_modules/.cache
22```
23
24If you decide to adopt this pattern, please file a PR adding your name to the list of adopters below.
25
26## Install
27
28```
29$ npm install find-cache-dir
30```
31
32## Usage
33
34```js
35const findCacheDir = require('find-cache-dir');
36
37findCacheDir({name: 'unicorns'});
38//=> '/user/path/node-modules/.cache/unicorns'
39```
40
41## API
42
43### findCacheDir(options?)
44
45Finds the cache directory using the supplied options. The algorithm checks for the `CACHE_DIR` environmental variable and uses it if it is not set to `true`, `false`, `1` or `0`. If one is not found, it 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 `undefined` if `package.json` was never found or if the `node_modules` directory is unwritable.
46
47#### options
48
49Type: `object`
50
51##### name
52
53*Required*\
54Type: `string`
55
56Should be the same as your project name in `package.json`.
57
58##### files
59
60Type: `string[] | string`
61
62An 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.
63
64##### cwd
65
66Type: `string`\
67Default `process.cwd()`
68
69Directory to start searching for a `package.json` from.
70
71##### create
72
73Type: `boolean`\
74Default `false`
75
76If `true`, the directory will be created synchronously before returning.
77
78##### thunk
79
80Type: `boolean`\
81Default `false`
82
83If `true`, this modifies the return type to be a function that is a thunk for `path.join(theFoundCacheDirectory)`.
84
85```js
86const thunk = findCacheDir({name: 'foo', thunk: true});
87
88thunk();
89//=> '/some/path/node_modules/.cache/foo'
90
91thunk('bar.js')
92//=> '/some/path/node_modules/.cache/foo/bar.js'
93
94thunk('baz', 'quz.js')
95//=> '/some/path/node_modules/.cache/foo/baz/quz.js'
96```
97
98This is helpful for actually putting actual files in the cache!
99
100## Tips
101
102- To test modules using `find-cache-dir`, set the `CACHE_DIR` environment variable to temporarily override the directory that is resolved.
103
104## Adopters
105
106- [`AVA`](https://ava.li)
107- [`nyc`](https://github.com/istanbuljs/nyc)
108- [`Storybook`](https://storybook.js.org)
109- [`babel-loader`](https://github.com/babel/babel-loader)
110- [`eslint-loader`](https://github.com/MoOx/eslint-loader)
111- [`Phenomic`](https://phenomic.io)
112- [`javascripthon-loader`](https://github.com/Beg-in/javascripthon-loader)
113
114---
115
116<div align="center">
117 <b>
118 <a href="https://tidelift.com/subscription/pkg/npm-find_cache-dir?utm_source=npm-find-cache-dir&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
119 </b>
120 <br>
121 <sub>
122 Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
123 </sub>
124</div>