Skip to content

Commit

Permalink
Allow the CACHE_DIR environment variable to override the cache loca…
Browse files Browse the repository at this point in the history
…tion (#18)

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Richienb and sindresorhus committed Feb 22, 2020
1 parent 8ec64dc commit 71faea2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
51 changes: 34 additions & 17 deletions index.js
Expand Up @@ -5,6 +5,8 @@ const commonDir = require('commondir');
const pkgDir = require('pkg-dir');
const makeDir = require('make-dir');

const {env} = process;

const isWritable = path => {
try {
fs.accessSync(path, fs.constants.W_OK);
Expand All @@ -14,8 +16,36 @@ const isWritable = path => {
}
};

function useDirectory(directory, options) {
if (options.create) {
makeDir.sync(directory);
}

if (options.thunk) {
return (...arguments_) => path.join(directory, ...arguments_);
}

return directory;
}

function getNodeModuleDirectory(directory) {
const nodeModules = path.join(directory, 'node_modules');

if (
!isWritable(nodeModules) &&
(fs.existsSync(nodeModules) || !isWritable(path.join(directory)))
) {
return;
}

return nodeModules;
}

module.exports = (options = {}) => {
const {name} = options;
if (env.CACHE_DIR) {
return useDirectory(path.join(env.CACHE_DIR, 'find-cache-dir'), options);
}

let directory = options.cwd || process.cwd();

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

if (directory) {
const nodeModules = path.join(directory, 'node_modules');
if (
!isWritable(nodeModules) &&
(fs.existsSync(nodeModules) || !isWritable(path.join(directory)))
) {
const nodeModules = getNodeModuleDirectory(directory);
if (!nodeModules) {
return undefined;
}

directory = path.join(directory, 'node_modules', '.cache', name);

if (options.create) {
makeDir.sync(directory);
}

if (options.thunk) {
return (...arguments_) => path.join(directory, ...arguments_);
}
return useDirectory(path.join(directory, 'node_modules', '.cache', options.name), options);
}

return directory;
};
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -32,6 +32,7 @@
"coveralls": "^3.0.9",
"del": "^4.0.0",
"nyc": "^14.1.1",
"tempy": "^0.4.0",
"xo": "^0.25.3"
},
"nyc": {
Expand Down
8 changes: 7 additions & 1 deletion readme.md
Expand Up @@ -38,11 +38,17 @@ findCacheDir({name: 'unicorns'});
//=> '/user/path/node-modules/.cache/unicorns'
```


## Tips

- To test modules using `find-cache-dir`, set the `CACHE_DIR` environmental variable to temporarily override the directory that is resolved.


## API

### findCacheDir(options?)

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.
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.

#### options

Expand Down
18 changes: 18 additions & 0 deletions test.js
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';
import path from 'path';
import {serial as test} from 'ava';
import del from 'del';
import {directory as tempDirectory} from 'tempy';
import findCacheDir from '.';

test('finds from a list of files', t => {
Expand Down Expand Up @@ -39,3 +40,20 @@ test('returns undefined if it can\'t find package.json', t => {
process.chdir(path.join(__dirname, '..'));
t.is(findCacheDir({name: 'foo'}), undefined);
});

test('supports CACHE_DIR environment variable', t => {
const newCacheDirectory = tempDirectory();
const finalDirectory = path.join(newCacheDirectory, 'find-cache-dir');
process.env.CACHE_DIR = newCacheDirectory;

t.is(findCacheDir(), finalDirectory);

findCacheDir({create: true});
t.true(fs.existsSync(finalDirectory));

const thunk = findCacheDir({thunk: true});
t.is(thunk('foo'), path.join(finalDirectory, 'foo'));
t.is(thunk('bar'), path.join(finalDirectory, 'bar'));

delete process.env.CACHE_DIR;
});

0 comments on commit 71faea2

Please sign in to comment.