cache-changed
v1.3.2
Published
Library for asynchronous recursive comparison of directory files and metadata cache file
Downloads
82
Readme
cache-changed
A simple library to monitor changes in a directory and cache the results. It allows you to compare the current state of a directory with its previous state, and return a list of added, updated, and deleted files. The library also provides a method to create a cache of the current state of the directory.
The library uses only asynchronous operations where possible. Due to this, better performance is expected. But no error stack, so the library pass all errors to call method and you should add to your log
new Error().stack
as shown below.
Installation
npm i cache-changed
Usage
Import CacheChanged
class and get an instance of it:
import CacheChanged from 'cache-changed';
const cacheChanged = new CacheChanged({
// Default /tmp-dir-of-your-os/cache-cached_[basename(CWD)].json
cacheFilePath: './tmp/cache.json',
// Default [CWD]
targetDirPath: './',
// Default ['.git'], if passed then concat with default
exclude: ['node_modules', 'some-dir/node_modules'],
});
Creating a cache from the targetDirPath
directory to the cacheFilePath
file:
cacheChanged
.create({
noWrite: false,
})
.catch((err) => {
// Log error with a stack
console.error('Failed to create cache', err, new Error().stack);
})
.then((code) => {
console.log('Created cache files count', code);
});
Comparison of cache from file cacheFilePath
and directory targetDirPath
:
cacheChanged
.compare()
.catch((err) => {
// Log error with a stack
console.error('Failed to compare cache', err, new Error().stack);
})
.then((res) => {
console.log('Compared cache result', res);
});
Result of compare:
{
"added": [],
"updated": [
{
"pathAbs": "/home/user/projects/cache-changed/.gitignore",
"pathRel": ".gitignore",
"mtimeMs": 1705743775005.3252,
"size": 16,
"isDir": false
}
],
"deleted": [],
"isChanged": true
}