Skip to content

Commit

Permalink
Return undefined if the found node_modules directory is not writa…
Browse files Browse the repository at this point in the history
…ble (#19)
  • Loading branch information
Richienb authored and sindresorhus committed Nov 9, 2019
1 parent da06f09 commit 3cea1e7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions index.js
@@ -1,9 +1,19 @@
'use strict';
const path = require('path');
const fs = require('fs');
const commonDir = require('commondir');
const pkgDir = require('pkg-dir');
const makeDir = require('make-dir');

const isWritable = path => {
try {
fs.accessSync(path, fs.constants.W_OK);
return true;
} catch (_) {
return false;
}
};

module.exports = (options = {}) => {
const {name} = options;
let directory = options.cwd;
Expand All @@ -17,6 +27,10 @@ module.exports = (options = {}) => {
directory = pkgDir.sync(directory);

if (directory) {
if (!isWritable(path.join(directory, 'node_modules'))) {
return undefined;
}

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

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

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

#### options

Expand Down

0 comments on commit 3cea1e7

Please sign in to comment.