Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
find-cache-dir
Advanced tools
The find-cache-dir npm package is used to find a directory to use for storing cache files. It is commonly used in tools that perform some form of compilation or data processing and need a place to store cache files to speed up future runs. The package helps to locate a suitable directory following conventions and best practices for cache storage.
Finding a cache directory
This feature allows you to find a cache directory for your application or module. You provide an options object with a 'name' property, and it returns a path to the cache directory.
const findCacheDir = require('find-cache-dir');
const cacheDir = findCacheDir({ name: 'my-app' });
console.log(cacheDir); // Outputs a directory path like '/path/to/cache/directory/my-app'
Creating a cache directory
In addition to finding the cache directory, you can also ensure that it is created if it does not already exist by setting the 'create' option to true.
const findCacheDir = require('find-cache-dir');
const cacheDir = findCacheDir({ name: 'my-app', create: true });
console.log(cacheDir); // Outputs a directory path and ensures that the directory exists
Using a custom directory
You can specify a custom directory to use as the base for your cache directory with the 'cwd' option. This allows you to control where the cache directory is located.
const findCacheDir = require('find-cache-dir');
const cacheDir = findCacheDir({ name: 'my-app', cwd: '/custom/directory' });
console.log(cacheDir); // Outputs a directory path within the specified custom directory
Theming cache directory
You can make the cache directory unique to a set of files by providing an array of file paths with the 'files' option. This is useful when the cache should be invalidated based on changes to specific files.
const findCacheDir = require('find-cache-dir');
const cacheDir = findCacheDir({ name: 'my-app', files: ['file.js'] });
console.log(cacheDir); // Outputs a directory path that is unique to the array of file paths provided
cache-manager is a more general-purpose caching framework that supports multiple storage backends and provides a flexible API for caching. It is not specifically designed for finding cache directories but can be used to manage cache data in a variety of contexts.
mkdirp is a package that allows you to create a new directory and its parent directories if they do not exist. While it does not provide cache directory finding capabilities, it can be used in conjunction with find-cache-dir to ensure that the cache directory is created.
tmp is a utility for creating temporary files and directories. It can be used to create temporary cache directories, but unlike find-cache-dir, it does not find or manage persistent cache directories across runs.
Finds the common standard cache directory
Recently the nyc
and AVA
projects decided to standardize on a common directory structure for storing cache information:
# nyc
./node_modules/.cache/nyc
# ava
./node_modules/.cache/ava
# your-module
./node_modules/.cache/your-module
This module makes it easy to correctly locate the cache directory according to this shared spec. If this pattern becomes ubiquitous, clearing the cache for multiple dependencies becomes easy and consistent:
rm -rf ./node_modules/.cache
If you decide to adopt this pattern, please file a PR adding your name to the list of adopters below.
$ npm install --save find-cache-dir
const findCacheDir = require('find-cache-dir');
findCacheDir({name: 'unicorns'});
//=> '/user/path/node-modules/.cache/unicorns'
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 null
if package.json
was never found.
Required
Type: string
Should be the same as your project name in package.json
.
Type: Array
`string
An array of files that will be searched for a common parent directory. This common parent directory will be used in lieu of the cwd
option below.
Type: string
Default process.cwd()
Directory to start searching for a package.json
from.
Type: boolean
Default false
If true
, the directory will be created synchronously before returning.
Type: boolean
Default false
If true
, this modifies the return type to be a function that is a thunk for path.join(theFoundCacheDirectory)
.
const thunk = findCacheDir({name: 'foo', thunk: true});
thunk();
//=> '/some/path/node_modules/.cache/foo'
thunk('bar.js')
//=> '/some/path/node_modules/.cache/foo/bar.js'
thunk('baz', 'quz.js')
//=> '/some/path/node_modules/.cache/foo/baz/quz.js'
This is helpful for actually putting actual files in the cache!
MIT © James Talmage
FAQs
Finds the common standard cache directory
The npm package find-cache-dir receives a total of 26,983,399 weekly downloads. As such, find-cache-dir popularity was classified as popular.
We found that find-cache-dir demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.