Skip to content

Commit e7dd4a6

Browse files
Richienbsindresorhus
andauthoredMar 9, 2020
Ignore true/false/1/0 values for CACHE_DIR environment variable (#27)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent e30f2cf commit e7dd4a6

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed
 

‎index.js

+5-3
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, cwd} = process;
9+
810
const isWritable = path => {
911
try {
1012
fs.accessSync(path, fs.constants.W_OK);
@@ -40,11 +42,11 @@ function getNodeModuleDirectory(directory) {
4042
}
4143

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

47-
let {cwd: directory = process.cwd()} = options;
49+
let {cwd: directory = cwd()} = options;
4850

4951
if (options.files) {
5052
directory = commonDir(directory, options.files);

‎readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ findCacheDir({name: 'unicorns'});
4242

4343
### findCacheDir(options?)
4444

45-
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.
45+
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.
4646

4747
#### options
4848

‎test.js

+6
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,9 @@ test('supports CACHE_DIR environment variable', t => {
5757

5858
delete process.env.CACHE_DIR;
5959
});
60+
61+
test('ignores `false` for CACHE_DIR environment variable', t => {
62+
process.env.CACHE_DIR = 'false';
63+
64+
t.not(findCacheDir(), path.resolve(__dirname, 'false', 'find-cache-dir'));
65+
});

0 commit comments

Comments
 (0)
Please sign in to comment.