Skip to content

Commit

Permalink
Ignore true/false/1/0 values for CACHE_DIR environment variable (#27)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Richienb and sindresorhus committed Mar 9, 2020
1 parent e30f2cf commit e7dd4a6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
8 changes: 5 additions & 3 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, cwd} = process;

const isWritable = path => {
try {
fs.accessSync(path, fs.constants.W_OK);
Expand Down Expand Up @@ -40,11 +42,11 @@ function getNodeModuleDirectory(directory) {
}

module.exports = (options = {}) => {
if (process.env.CACHE_DIR) {
return useDirectory(path.join(process.env.CACHE_DIR, 'find-cache-dir'), options);
if (env.CACHE_DIR && !['true', 'false', '1', '0'].includes(env.CACHE_DIR)) {
return useDirectory(path.join(env.CACHE_DIR, 'find-cache-dir'), options);
}

let {cwd: directory = process.cwd()} = options;
let {cwd: directory = cwd()} = options;

if (options.files) {
directory = commonDir(directory, options.files);
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -42,7 +42,7 @@ findCacheDir({name: 'unicorns'});

### findCacheDir(options?)

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

#### options

Expand Down
6 changes: 6 additions & 0 deletions test.js
Expand Up @@ -57,3 +57,9 @@ test('supports CACHE_DIR environment variable', t => {

delete process.env.CACHE_DIR;
});

test('ignores `false` for CACHE_DIR environment variable', t => {
process.env.CACHE_DIR = 'false';

t.not(findCacheDir(), path.resolve(__dirname, 'false', 'find-cache-dir'));
});

0 comments on commit e7dd4a6

Please sign in to comment.