file-entry-cache
Advanced tools
Comparing version 9.0.0 to 9.1.0
68
cache.js
/* eslint-disable unicorn/no-this-assignment, func-names, no-multi-assign */ | ||
const path = require('node:path'); | ||
const process = require('node:process'); | ||
const crypto = require('node:crypto'); | ||
module.exports = { | ||
createFromFile(filePath, useChecksum) { | ||
createFromFile(filePath, useChecksum, currentWorkingDir) { | ||
const fname = path.basename(filePath); | ||
const dir = path.dirname(filePath); | ||
return this.create(fname, dir, useChecksum); | ||
return this.create(fname, dir, useChecksum, currentWorkingDir); | ||
}, | ||
create(cacheId, _path, useChecksum) { | ||
create(cacheId, _path, useChecksum, currentWorkingDir) { | ||
const fs = require('node:fs'); | ||
@@ -24,3 +23,8 @@ const flatCache = require('flat-cache'); | ||
try { | ||
fs.statSync(fPath); | ||
let filePath = fPath; | ||
if (currentWorkingDir) { | ||
filePath = path.join(currentWorkingDir, fPath); | ||
} | ||
fs.statSync(filePath); | ||
} catch (error) { | ||
@@ -44,2 +48,8 @@ if (error.code === 'ENOENT') { | ||
/** | ||
* To enable relative paths as the key with current working directory | ||
* @type {string} | ||
*/ | ||
currentWorkingDir: currentWorkingDir ?? undefined, | ||
/** | ||
* Given a buffer, calculate md5 hash of its content. | ||
@@ -104,6 +114,2 @@ * @method getHash | ||
try { | ||
if (!path.isAbsolute(file)) { | ||
file = path.resolve(process.cwd(), file); | ||
} | ||
fstat = fs.statSync(file); | ||
@@ -122,4 +128,12 @@ } catch (error) { | ||
_getFileKey(file) { | ||
if (this.currentWorkingDir) { | ||
return file.split(this.currentWorkingDir).pop(); | ||
} | ||
return file; | ||
}, | ||
_getFileDescriptorUsingMtimeAndSize(file, fstat) { | ||
let meta = cache.getKey(file); | ||
let meta = cache.getKey(this._getFileKey(file)); | ||
const cacheExists = Boolean(meta); | ||
@@ -140,4 +154,4 @@ | ||
const nEntry = (normalizedEntries[file] = { | ||
key: file, | ||
const nEntry = (normalizedEntries[this._getFileKey(file)] = { | ||
key: this._getFileKey(file), | ||
changed: !cacheExists || isDifferentDate || isDifferentSize, | ||
@@ -151,3 +165,3 @@ meta, | ||
_getFileDescriptorUsingChecksum(file) { | ||
let meta = cache.getKey(file); | ||
let meta = cache.getKey(this._getFileKey(file)); | ||
const cacheExists = Boolean(meta); | ||
@@ -171,4 +185,4 @@ | ||
const nEntry = (normalizedEntries[file] = { | ||
key: file, | ||
const nEntry = (normalizedEntries[this._getFileKey(file)] = { | ||
key: this._getFileKey(file), | ||
changed: !cacheExists || isDifferent, | ||
@@ -223,8 +237,4 @@ meta, | ||
removeEntry(entryName) { | ||
if (!path.isAbsolute(entryName)) { | ||
entryName = path.resolve(process.cwd(), entryName); | ||
} | ||
delete normalizedEntries[entryName]; | ||
cache.removeKey(entryName); | ||
delete normalizedEntries[this._getFileKey(entryName)]; | ||
cache.removeKey(this._getFileKey(entryName)); | ||
}, | ||
@@ -249,3 +259,8 @@ | ||
_getMetaForFileUsingCheckSum(cacheEntry) { | ||
const contentBuffer = fs.readFileSync(cacheEntry.key); | ||
let filePath = cacheEntry.key; | ||
if (this.currentWorkingDir) { | ||
filePath = path.join(this.currentWorkingDir, filePath); | ||
} | ||
const contentBuffer = fs.readFileSync(filePath); | ||
const hash = this.getHash(contentBuffer); | ||
@@ -259,3 +274,8 @@ const meta = Object.assign(cacheEntry.meta, {hash}); | ||
_getMetaForFileUsingMtimeAndSize(cacheEntry) { | ||
const stat = fs.statSync(cacheEntry.key); | ||
let filePath = cacheEntry.key; | ||
if (currentWorkingDir) { | ||
filePath = path.join(currentWorkingDir, filePath); | ||
} | ||
const stat = fs.statSync(filePath); | ||
const meta = Object.assign(cacheEntry.meta, { | ||
@@ -294,3 +314,3 @@ size: stat.size, | ||
: me._getMetaForFileUsingMtimeAndSize(cacheEntry); | ||
cache.setKey(entryName, meta); | ||
cache.setKey(this._getFileKey(entryName), meta); | ||
} catch (error) { | ||
@@ -297,0 +317,0 @@ // If the file does not exists we don't save it |
{ | ||
"name": "file-entry-cache", | ||
"version": "9.0.0", | ||
"version": "9.1.0", | ||
"description": "Super simple cache for file metadata, useful for process that work o a given series of files and that only need to repeat the job on the changed ones since the previous run of the process", | ||
@@ -20,4 +20,5 @@ "repository": "jaredwray/file-entry-cache", | ||
"clean": "rimraf ./coverage /node_modules ./package-lock.json ./yarn.lock ./pnpm-lock.yaml", | ||
"test": "xo --fix && c8 mocha -R spec test/specs", | ||
"test:ci": "xo && c8 --reporter=lcov mocha -R spec test/specs", | ||
"test": "xo --fix && c8 mocha -R spec test/specs/cache.js test/relative.js", | ||
"test:relative": "rimraf ./rfixtures ./tfixtures && mocha test/relative.js", | ||
"test:ci": "xo && c8 --reporter=lcov mocha -R spec test/specs/cache.js test/relative.js", | ||
"perf": "node perf.js" | ||
@@ -40,8 +41,8 @@ }, | ||
"devDependencies": { | ||
"c8": "^9.1.0", | ||
"c8": "^10.1.2", | ||
"chai": "^4.3.10", | ||
"glob-expand": "^0.2.1", | ||
"mocha": "^10.4.0", | ||
"mocha": "^10.5.1", | ||
"rimraf": "^5.0.7", | ||
"webpack": "^5.91.0", | ||
"webpack": "^5.92.1", | ||
"write": "^2.0.0", | ||
@@ -48,0 +49,0 @@ "xo": "^0.58.0" |
@@ -20,10 +20,12 @@ # file-entry-cache | ||
## `create(cacheName, [directory, useCheckSum])` | ||
## `create(cacheName, [directory, useCheckSum, currentWorkingDir])` | ||
- **cacheName**: the name of the cache to be created | ||
- **directory**: Optional the directory to load the cache from | ||
- **usecheckSum**: Whether to use md5 checksum to verify if file changed. If false the default will be to use the mtime and size of the file. | ||
- **currentWorkingDir**: Optional the current working directory to use when resolving relative paths | ||
## `createFromFile(pathToCache, [useCheckSum])` | ||
## `createFromFile(pathToCache, [useCheckSum, currentWorkingDir])` | ||
- **pathToCache**: the path to the cache file (this combines the cache name and directory) | ||
- **useCheckSum**: Whether to use md5 checksum to verify if file changed. If false the default will be to use the mtime and size of the file. | ||
- **currentWorkingDir**: Optional the current working directory to use when resolving relative paths | ||
@@ -113,4 +115,4 @@ ```js | ||
MIT | ||
MIT (c) Jared Wray | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16203
265
117