Skip to content

Commit 71faea2

Browse files
Richienbsindresorhus
andauthoredFeb 22, 2020
Allow the CACHE_DIR environment variable to override the cache location (#18)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 8ec64dc commit 71faea2

File tree

4 files changed

+60
-18
lines changed

4 files changed

+60
-18
lines changed
 

‎index.js

+34-17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const commonDir = require('commondir');
55
const pkgDir = require('pkg-dir');
66
const makeDir = require('make-dir');
77

8+
const {env} = process;
9+
810
const isWritable = path => {
911
try {
1012
fs.accessSync(path, fs.constants.W_OK);
@@ -14,8 +16,36 @@ const isWritable = path => {
1416
}
1517
};
1618

19+
function useDirectory(directory, options) {
20+
if (options.create) {
21+
makeDir.sync(directory);
22+
}
23+
24+
if (options.thunk) {
25+
return (...arguments_) => path.join(directory, ...arguments_);
26+
}
27+
28+
return directory;
29+
}
30+
31+
function getNodeModuleDirectory(directory) {
32+
const nodeModules = path.join(directory, 'node_modules');
33+
34+
if (
35+
!isWritable(nodeModules) &&
36+
(fs.existsSync(nodeModules) || !isWritable(path.join(directory)))
37+
) {
38+
return;
39+
}
40+
41+
return nodeModules;
42+
}
43+
1744
module.exports = (options = {}) => {
18-
const {name} = options;
45+
if (env.CACHE_DIR) {
46+
return useDirectory(path.join(env.CACHE_DIR, 'find-cache-dir'), options);
47+
}
48+
1949
let directory = options.cwd || process.cwd();
2050

2151
if (options.files) {
@@ -25,24 +55,11 @@ module.exports = (options = {}) => {
2555
directory = pkgDir.sync(directory);
2656

2757
if (directory) {
28-
const nodeModules = path.join(directory, 'node_modules');
29-
if (
30-
!isWritable(nodeModules) &&
31-
(fs.existsSync(nodeModules) || !isWritable(path.join(directory)))
32-
) {
58+
const nodeModules = getNodeModuleDirectory(directory);
59+
if (!nodeModules) {
3360
return undefined;
3461
}
3562

36-
directory = path.join(directory, 'node_modules', '.cache', name);
37-
38-
if (options.create) {
39-
makeDir.sync(directory);
40-
}
41-
42-
if (options.thunk) {
43-
return (...arguments_) => path.join(directory, ...arguments_);
44-
}
63+
return useDirectory(path.join(directory, 'node_modules', '.cache', options.name), options);
4564
}
46-
47-
return directory;
4865
};

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"coveralls": "^3.0.9",
3333
"del": "^4.0.0",
3434
"nyc": "^14.1.1",
35+
"tempy": "^0.4.0",
3536
"xo": "^0.25.3"
3637
},
3738
"nyc": {

‎readme.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,17 @@ findCacheDir({name: 'unicorns'});
3838
//=> '/user/path/node-modules/.cache/unicorns'
3939
```
4040

41+
42+
## Tips
43+
44+
- To test modules using `find-cache-dir`, set the `CACHE_DIR` environmental variable to temporarily override the directory that is resolved.
45+
46+
4147
## API
4248

4349
### findCacheDir(options?)
4450

45-
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 `undefined` if `package.json` was never found or if the `node_modules` directory is unwritable.
51+
Finds the cache directory using the supplied options. The algorithm checks for the `CACHE_DIR` environmental variable, and 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.
4652

4753
#### options
4854

‎test.js

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'fs';
22
import path from 'path';
33
import {serial as test} from 'ava';
44
import del from 'del';
5+
import {directory as tempDirectory} from 'tempy';
56
import findCacheDir from '.';
67

78
test('finds from a list of files', t => {
@@ -39,3 +40,20 @@ test('returns undefined if it can\'t find package.json', t => {
3940
process.chdir(path.join(__dirname, '..'));
4041
t.is(findCacheDir({name: 'foo'}), undefined);
4142
});
43+
44+
test('supports CACHE_DIR environment variable', t => {
45+
const newCacheDirectory = tempDirectory();
46+
const finalDirectory = path.join(newCacheDirectory, 'find-cache-dir');
47+
process.env.CACHE_DIR = newCacheDirectory;
48+
49+
t.is(findCacheDir(), finalDirectory);
50+
51+
findCacheDir({create: true});
52+
t.true(fs.existsSync(finalDirectory));
53+
54+
const thunk = findCacheDir({thunk: true});
55+
t.is(thunk('foo'), path.join(finalDirectory, 'foo'));
56+
t.is(thunk('bar'), path.join(finalDirectory, 'bar'));
57+
58+
delete process.env.CACHE_DIR;
59+
});

0 commit comments

Comments
 (0)
Please sign in to comment.