file-entry-cache 9.0.0 → 9.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +5 -3
  2. package/cache.js +44 -24
  3. package/package.json +7 -6
package/README.md CHANGED
@@ -17,14 +17,16 @@ npm i --save file-entry-cache
17
17
 
18
18
  The module exposes two functions `create` and `createFromFile`.
19
19
 
20
- ## `create(cacheName, [directory, useCheckSum])`
20
+ ## `create(cacheName, [directory, useCheckSum, currentWorkingDir])`
21
21
  - **cacheName**: the name of the cache to be created
22
22
  - **directory**: Optional the directory to load the cache from
23
23
  - **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.
24
+ - **currentWorkingDir**: Optional the current working directory to use when resolving relative paths
24
25
 
25
- ## `createFromFile(pathToCache, [useCheckSum])`
26
+ ## `createFromFile(pathToCache, [useCheckSum, currentWorkingDir])`
26
27
  - **pathToCache**: the path to the cache file (this combines the cache name and directory)
27
28
  - **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.
29
+ - **currentWorkingDir**: Optional the current working directory to use when resolving relative paths
28
30
 
29
31
  ```js
30
32
  // loads the cache, if one does not exists for the given
@@ -109,6 +111,6 @@ In the worst case scenario all the files will be processed. In the best case sce
109
111
 
110
112
  ## License
111
113
 
112
- MIT
114
+ MIT (c) Jared Wray
113
115
 
114
116
 
package/cache.js CHANGED
@@ -1,16 +1,15 @@
1
1
  /* eslint-disable unicorn/no-this-assignment, func-names, no-multi-assign */
2
2
  const path = require('node:path');
3
- const process = require('node:process');
4
3
  const crypto = require('node:crypto');
5
4
 
6
5
  module.exports = {
7
- createFromFile(filePath, useChecksum) {
6
+ createFromFile(filePath, useChecksum, currentWorkingDir) {
8
7
  const fname = path.basename(filePath);
9
8
  const dir = path.dirname(filePath);
10
- return this.create(fname, dir, useChecksum);
9
+ return this.create(fname, dir, useChecksum, currentWorkingDir);
11
10
  },
12
11
 
13
- create(cacheId, _path, useChecksum) {
12
+ create(cacheId, _path, useChecksum, currentWorkingDir) {
14
13
  const fs = require('node:fs');
15
14
  const flatCache = require('flat-cache');
16
15
  const cache = flatCache.load(cacheId, _path);
@@ -21,7 +20,12 @@ module.exports = {
21
20
  // Remove not found entries
22
21
  for (const fPath of cachedEntries) {
23
22
  try {
24
- fs.statSync(fPath);
23
+ let filePath = fPath;
24
+ if (currentWorkingDir) {
25
+ filePath = path.join(currentWorkingDir, fPath);
26
+ }
27
+
28
+ fs.statSync(filePath);
25
29
  } catch (error) {
26
30
  if (error.code === 'ENOENT') {
27
31
  cache.removeKey(fPath);
@@ -39,6 +43,12 @@ module.exports = {
39
43
  */
40
44
  cache,
41
45
 
46
+ /**
47
+ * To enable relative paths as the key with current working directory
48
+ * @type {string}
49
+ */
50
+ currentWorkingDir: currentWorkingDir ?? undefined,
51
+
42
52
  /**
43
53
  * Given a buffer, calculate md5 hash of its content.
44
54
  * @method getHash
@@ -99,10 +109,6 @@ module.exports = {
99
109
  let fstat;
100
110
 
101
111
  try {
102
- if (!path.isAbsolute(file)) {
103
- file = path.resolve(process.cwd(), file);
104
- }
105
-
106
112
  fstat = fs.statSync(file);
107
113
  } catch (error) {
108
114
  this.removeEntry(file);
@@ -116,8 +122,16 @@ module.exports = {
116
122
  return this._getFileDescriptorUsingMtimeAndSize(file, fstat);
117
123
  },
118
124
 
125
+ _getFileKey(file) {
126
+ if (this.currentWorkingDir) {
127
+ return file.split(this.currentWorkingDir).pop();
128
+ }
129
+
130
+ return file;
131
+ },
132
+
119
133
  _getFileDescriptorUsingMtimeAndSize(file, fstat) {
120
- let meta = cache.getKey(file);
134
+ let meta = cache.getKey(this._getFileKey(file));
121
135
  const cacheExists = Boolean(meta);
122
136
 
123
137
  const cSize = fstat.size;
@@ -133,8 +147,8 @@ module.exports = {
133
147
  meta = {size: cSize, mtime: cTime};
134
148
  }
135
149
 
136
- const nEntry = (normalizedEntries[file] = {
137
- key: file,
150
+ const nEntry = (normalizedEntries[this._getFileKey(file)] = {
151
+ key: this._getFileKey(file),
138
152
  changed: !cacheExists || isDifferentDate || isDifferentSize,
139
153
  meta,
140
154
  });
@@ -143,7 +157,7 @@ module.exports = {
143
157
  },
144
158
 
145
159
  _getFileDescriptorUsingChecksum(file) {
146
- let meta = cache.getKey(file);
160
+ let meta = cache.getKey(this._getFileKey(file));
147
161
  const cacheExists = Boolean(meta);
148
162
 
149
163
  let contentBuffer;
@@ -162,8 +176,8 @@ module.exports = {
162
176
  meta = {hash};
163
177
  }
164
178
 
165
- const nEntry = (normalizedEntries[file] = {
166
- key: file,
179
+ const nEntry = (normalizedEntries[this._getFileKey(file)] = {
180
+ key: this._getFileKey(file),
167
181
  changed: !cacheExists || isDifferent,
168
182
  meta,
169
183
  });
@@ -213,12 +227,8 @@ module.exports = {
213
227
  * @param entryName
214
228
  */
215
229
  removeEntry(entryName) {
216
- if (!path.isAbsolute(entryName)) {
217
- entryName = path.resolve(process.cwd(), entryName);
218
- }
219
-
220
- delete normalizedEntries[entryName];
221
- cache.removeKey(entryName);
230
+ delete normalizedEntries[this._getFileKey(entryName)];
231
+ cache.removeKey(this._getFileKey(entryName));
222
232
  },
223
233
 
224
234
  /**
@@ -238,7 +248,12 @@ module.exports = {
238
248
  },
239
249
 
240
250
  _getMetaForFileUsingCheckSum(cacheEntry) {
241
- const contentBuffer = fs.readFileSync(cacheEntry.key);
251
+ let filePath = cacheEntry.key;
252
+ if (this.currentWorkingDir) {
253
+ filePath = path.join(this.currentWorkingDir, filePath);
254
+ }
255
+
256
+ const contentBuffer = fs.readFileSync(filePath);
242
257
  const hash = this.getHash(contentBuffer);
243
258
  const meta = Object.assign(cacheEntry.meta, {hash});
244
259
  delete meta.size;
@@ -247,7 +262,12 @@ module.exports = {
247
262
  },
248
263
 
249
264
  _getMetaForFileUsingMtimeAndSize(cacheEntry) {
250
- const stat = fs.statSync(cacheEntry.key);
265
+ let filePath = cacheEntry.key;
266
+ if (currentWorkingDir) {
267
+ filePath = path.join(currentWorkingDir, filePath);
268
+ }
269
+
270
+ const stat = fs.statSync(filePath);
251
271
  const meta = Object.assign(cacheEntry.meta, {
252
272
  size: stat.size,
253
273
  mtime: stat.mtime.getTime(),
@@ -281,7 +301,7 @@ module.exports = {
281
301
  const meta = useChecksum
282
302
  ? me._getMetaForFileUsingCheckSum(cacheEntry)
283
303
  : me._getMetaForFileUsingMtimeAndSize(cacheEntry);
284
- cache.setKey(entryName, meta);
304
+ cache.setKey(this._getFileKey(entryName), meta);
285
305
  } catch (error) {
286
306
  // If the file does not exists we don't save it
287
307
  // other errors are just thrown
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "file-entry-cache",
3
- "version": "9.0.0",
3
+ "version": "9.1.0",
4
4
  "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",
5
5
  "repository": "jaredwray/file-entry-cache",
6
6
  "license": "MIT",
@@ -17,8 +17,9 @@
17
17
  },
18
18
  "scripts": {
19
19
  "clean": "rimraf ./coverage /node_modules ./package-lock.json ./yarn.lock ./pnpm-lock.yaml",
20
- "test": "xo --fix && c8 mocha -R spec test/specs",
21
- "test:ci": "xo && c8 --reporter=lcov mocha -R spec test/specs",
20
+ "test": "xo --fix && c8 mocha -R spec test/specs/cache.js test/relative.js",
21
+ "test:relative": "rimraf ./rfixtures ./tfixtures && mocha test/relative.js",
22
+ "test:ci": "xo && c8 --reporter=lcov mocha -R spec test/specs/cache.js test/relative.js",
22
23
  "perf": "node perf.js"
23
24
  },
24
25
  "prepush": [
@@ -36,12 +37,12 @@
36
37
  "cache"
37
38
  ],
38
39
  "devDependencies": {
39
- "c8": "^9.1.0",
40
+ "c8": "^10.1.2",
40
41
  "chai": "^4.3.10",
41
42
  "glob-expand": "^0.2.1",
42
- "mocha": "^10.4.0",
43
+ "mocha": "^10.5.1",
43
44
  "rimraf": "^5.0.7",
44
- "webpack": "^5.91.0",
45
+ "webpack": "^5.92.1",
45
46
  "write": "^2.0.0",
46
47
  "xo": "^0.58.0"
47
48
  },