file-entry-cache 7.0.2 → 9.0.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 +1 -2
  2. package/cache.js +244 -237
  3. package/package.json +21 -17
package/README.md CHANGED
@@ -1,6 +1,5 @@
1
1
  # file-entry-cache
2
- > Super simple cache for file metadata, useful for process that work on a given series of files
3
- > and that only need to repeat the job on the changed ones since the previous run of the process — Edit
2
+ > Super simple cache for file metadata, useful for process that work on a given series of files and that only need to repeat the job on the changed ones since the previous run of the process
4
3
 
5
4
  [![NPM Version](https://img.shields.io/npm/v/file-entry-cache.svg?style=flat)](https://npmjs.org/package/file-entry-cache)
6
5
  [![tests](https://github.com/jaredwray/file-entry-cache/actions/workflows/tests.yaml/badge.svg?branch=master)](https://github.com/jaredwray/file-entry-cache/actions/workflows/tests.yaml)
package/cache.js CHANGED
@@ -1,64 +1,66 @@
1
- var path = require('path');
2
- var crypto = require('crypto');
1
+ /* eslint-disable unicorn/no-this-assignment, func-names, no-multi-assign */
2
+ const path = require('node:path');
3
+ const process = require('node:process');
4
+ const crypto = require('node:crypto');
3
5
 
4
6
  module.exports = {
5
- createFromFile: function (filePath, useChecksum) {
6
- var fname = path.basename(filePath);
7
- var dir = path.dirname(filePath);
8
- return this.create(fname, dir, useChecksum);
9
- },
10
-
11
- create: function (cacheId, _path, useChecksum) {
12
- var fs = require('fs');
13
- var flatCache = require('flat-cache');
14
- var cache = flatCache.load(cacheId, _path);
15
- var normalizedEntries = {};
16
-
17
- var removeNotFoundFiles = function removeNotFoundFiles() {
18
- const cachedEntries = cache.keys();
19
- // remove not found entries
20
- cachedEntries.forEach(function remover(fPath) {
21
- try {
22
- fs.statSync(fPath);
23
- } catch (err) {
24
- if (err.code === 'ENOENT') {
25
- cache.removeKey(fPath);
26
- }
27
- }
28
- });
29
- };
30
-
31
- removeNotFoundFiles();
32
-
33
- return {
34
- /**
35
- * the flat cache storage used to persist the metadata of the `files
7
+ createFromFile(filePath, useChecksum) {
8
+ const fname = path.basename(filePath);
9
+ const dir = path.dirname(filePath);
10
+ return this.create(fname, dir, useChecksum);
11
+ },
12
+
13
+ create(cacheId, _path, useChecksum) {
14
+ const fs = require('node:fs');
15
+ const flatCache = require('flat-cache');
16
+ const cache = flatCache.load(cacheId, _path);
17
+ let normalizedEntries = {};
18
+
19
+ const removeNotFoundFiles = function removeNotFoundFiles() {
20
+ const cachedEntries = cache.keys();
21
+ // Remove not found entries
22
+ for (const fPath of cachedEntries) {
23
+ try {
24
+ fs.statSync(fPath);
25
+ } catch (error) {
26
+ if (error.code === 'ENOENT') {
27
+ cache.removeKey(fPath);
28
+ }
29
+ }
30
+ }
31
+ };
32
+
33
+ removeNotFoundFiles();
34
+
35
+ return {
36
+ /**
37
+ * The flat cache storage used to persist the metadata of the `files
36
38
  * @type {Object}
37
39
  */
38
- cache: cache,
40
+ cache,
39
41
 
40
- /**
42
+ /**
41
43
  * Given a buffer, calculate md5 hash of its content.
42
44
  * @method getHash
43
45
  * @param {Buffer} buffer buffer to calculate hash on
44
46
  * @return {String} content hash digest
45
47
  */
46
- getHash: function (buffer) {
47
- return crypto.createHash('md5').update(buffer).digest('hex');
48
- },
48
+ getHash(buffer) {
49
+ return crypto.createHash('md5').update(buffer).digest('hex');
50
+ },
49
51
 
50
- /**
52
+ /**
51
53
  * Return whether or not a file has changed since last time reconcile was called.
52
54
  * @method hasFileChanged
53
55
  * @param {String} file the filepath to check
54
56
  * @return {Boolean} wheter or not the file has changed
55
57
  */
56
- hasFileChanged: function (file) {
57
- return this.getFileDescriptor(file).changed;
58
- },
58
+ hasFileChanged(file) {
59
+ return this.getFileDescriptor(file).changed;
60
+ },
59
61
 
60
- /**
61
- * given an array of file paths it return and object with three arrays:
62
+ /**
63
+ * Given an array of file paths it return and object with three arrays:
62
64
  * - changedFiles: Files that changed since previous run
63
65
  * - notChangedFiles: Files that haven't change
64
66
  * - notFoundFiles: Files that were not found, probably deleted
@@ -66,103 +68,110 @@ module.exports = {
66
68
  * @param {Array} files the files to analyze and compare to the previous seen files
67
69
  * @return {[type]} [description]
68
70
  */
69
- analyzeFiles: function (files) {
70
- var me = this;
71
- files = files || [];
72
-
73
- var res = {
74
- changedFiles: [],
75
- notFoundFiles: [],
76
- notChangedFiles: [],
77
- };
78
-
79
- me.normalizeEntries(files).forEach(function (entry) {
80
- if (entry.changed) {
81
- res.changedFiles.push(entry.key);
82
- return;
83
- }
84
- if (entry.notFound) {
85
- res.notFoundFiles.push(entry.key);
86
- return;
87
- }
88
- res.notChangedFiles.push(entry.key);
89
- });
90
- return res;
91
- },
92
-
93
- getFileDescriptor: function (file) {
94
- var fstat;
95
-
96
- try {
97
- fstat = fs.statSync(file);
98
- } catch (ex) {
99
- this.removeEntry(file);
100
- return { key: file, notFound: true, err: ex };
101
- }
102
-
103
- if (useChecksum) {
104
- return this._getFileDescriptorUsingChecksum(file);
105
- }
106
-
107
- return this._getFileDescriptorUsingMtimeAndSize(file, fstat);
108
- },
109
-
110
- _getFileDescriptorUsingMtimeAndSize: function (file, fstat) {
111
- var meta = cache.getKey(file);
112
- var cacheExists = !!meta;
113
-
114
- var cSize = fstat.size;
115
- var cTime = fstat.mtime.getTime();
116
-
117
- var isDifferentDate;
118
- var isDifferentSize;
119
-
120
- if (!meta) {
121
- meta = { size: cSize, mtime: cTime };
122
- } else {
123
- isDifferentDate = cTime !== meta.mtime;
124
- isDifferentSize = cSize !== meta.size;
125
- }
126
-
127
- var nEntry = (normalizedEntries[file] = {
128
- key: file,
129
- changed: !cacheExists || isDifferentDate || isDifferentSize,
130
- meta: meta,
131
- });
132
-
133
- return nEntry;
134
- },
135
-
136
- _getFileDescriptorUsingChecksum: function (file) {
137
- var meta = cache.getKey(file);
138
- var cacheExists = !!meta;
139
-
140
- var contentBuffer;
141
- try {
142
- contentBuffer = fs.readFileSync(file);
143
- } catch (ex) {
144
- contentBuffer = '';
145
- }
146
-
147
- var isDifferent = true;
148
- var hash = this.getHash(contentBuffer);
149
-
150
- if (!meta) {
151
- meta = { hash: hash };
152
- } else {
153
- isDifferent = hash !== meta.hash;
154
- }
155
-
156
- var nEntry = (normalizedEntries[file] = {
157
- key: file,
158
- changed: !cacheExists || isDifferent,
159
- meta: meta,
160
- });
161
-
162
- return nEntry;
163
- },
164
-
165
- /**
71
+ analyzeFiles(files) {
72
+ const me = this;
73
+ files ||= [];
74
+
75
+ const res = {
76
+ changedFiles: [],
77
+ notFoundFiles: [],
78
+ notChangedFiles: [],
79
+ };
80
+
81
+ for (const entry of me.normalizeEntries(files)) {
82
+ if (entry.changed) {
83
+ res.changedFiles.push(entry.key);
84
+ continue;
85
+ }
86
+
87
+ if (entry.notFound) {
88
+ res.notFoundFiles.push(entry.key);
89
+ continue;
90
+ }
91
+
92
+ res.notChangedFiles.push(entry.key);
93
+ }
94
+
95
+ return res;
96
+ },
97
+
98
+ getFileDescriptor(file) {
99
+ let fstat;
100
+
101
+ try {
102
+ if (!path.isAbsolute(file)) {
103
+ file = path.resolve(process.cwd(), file);
104
+ }
105
+
106
+ fstat = fs.statSync(file);
107
+ } catch (error) {
108
+ this.removeEntry(file);
109
+ return {key: file, notFound: true, err: error};
110
+ }
111
+
112
+ if (useChecksum) {
113
+ return this._getFileDescriptorUsingChecksum(file);
114
+ }
115
+
116
+ return this._getFileDescriptorUsingMtimeAndSize(file, fstat);
117
+ },
118
+
119
+ _getFileDescriptorUsingMtimeAndSize(file, fstat) {
120
+ let meta = cache.getKey(file);
121
+ const cacheExists = Boolean(meta);
122
+
123
+ const cSize = fstat.size;
124
+ const cTime = fstat.mtime.getTime();
125
+
126
+ let isDifferentDate;
127
+ let isDifferentSize;
128
+
129
+ if (meta) {
130
+ isDifferentDate = cTime !== meta.mtime;
131
+ isDifferentSize = cSize !== meta.size;
132
+ } else {
133
+ meta = {size: cSize, mtime: cTime};
134
+ }
135
+
136
+ const nEntry = (normalizedEntries[file] = {
137
+ key: file,
138
+ changed: !cacheExists || isDifferentDate || isDifferentSize,
139
+ meta,
140
+ });
141
+
142
+ return nEntry;
143
+ },
144
+
145
+ _getFileDescriptorUsingChecksum(file) {
146
+ let meta = cache.getKey(file);
147
+ const cacheExists = Boolean(meta);
148
+
149
+ let contentBuffer;
150
+ try {
151
+ contentBuffer = fs.readFileSync(file);
152
+ } catch {
153
+ contentBuffer = '';
154
+ }
155
+
156
+ let isDifferent = true;
157
+ const hash = this.getHash(contentBuffer);
158
+
159
+ if (meta) {
160
+ isDifferent = hash !== meta.hash;
161
+ } else {
162
+ meta = {hash};
163
+ }
164
+
165
+ const nEntry = (normalizedEntries[file] = {
166
+ key: file,
167
+ changed: !cacheExists || isDifferent,
168
+ meta,
169
+ });
170
+
171
+ return nEntry;
172
+ },
173
+
174
+ /**
166
175
  * Return the list o the files that changed compared
167
176
  * against the ones stored in the cache
168
177
  *
@@ -170,122 +179,120 @@ module.exports = {
170
179
  * @param files {Array} the array of files to compare against the ones in the cache
171
180
  * @returns {Array}
172
181
  */
173
- getUpdatedFiles: function (files) {
174
- var me = this;
175
- files = files || [];
176
-
177
- return me
178
- .normalizeEntries(files)
179
- .filter(function (entry) {
180
- return entry.changed;
181
- })
182
- .map(function (entry) {
183
- return entry.key;
184
- });
185
- },
186
-
187
- /**
188
- * return the list of files
182
+ getUpdatedFiles(files) {
183
+ const me = this;
184
+ files ||= [];
185
+
186
+ return me
187
+ .normalizeEntries(files)
188
+ .filter(entry => entry.changed)
189
+ .map(entry => entry.key);
190
+ },
191
+
192
+ /**
193
+ * Return the list of files
189
194
  * @method normalizeEntries
190
195
  * @param files
191
196
  * @returns {*}
192
197
  */
193
- normalizeEntries: function (files) {
194
- files = files || [];
198
+ normalizeEntries(files) {
199
+ files ||= [];
195
200
 
196
- var me = this;
197
- var nEntries = files.map(function (file) {
198
- return me.getFileDescriptor(file);
199
- });
201
+ const me = this;
202
+ const nEntries = files.map(file => me.getFileDescriptor(file));
200
203
 
201
- //normalizeEntries = nEntries;
202
- return nEntries;
203
- },
204
+ // NormalizeEntries = nEntries;
205
+ return nEntries;
206
+ },
204
207
 
205
- /**
208
+ /**
206
209
  * Remove an entry from the file-entry-cache. Useful to force the file to still be considered
207
210
  * modified the next time the process is run
208
211
  *
209
212
  * @method removeEntry
210
213
  * @param entryName
211
214
  */
212
- removeEntry: function (entryName) {
213
- delete normalizedEntries[entryName];
214
- cache.removeKey(entryName);
215
- },
215
+ removeEntry(entryName) {
216
+ if (!path.isAbsolute(entryName)) {
217
+ entryName = path.resolve(process.cwd(), entryName);
218
+ }
216
219
 
217
- /**
220
+ delete normalizedEntries[entryName];
221
+ cache.removeKey(entryName);
222
+ },
223
+
224
+ /**
218
225
  * Delete the cache file from the disk
219
226
  * @method deleteCacheFile
220
227
  */
221
- deleteCacheFile: function () {
222
- cache.removeCacheFile();
223
- },
228
+ deleteCacheFile() {
229
+ cache.removeCacheFile();
230
+ },
224
231
 
225
- /**
226
- * remove the cache from the file and clear the memory cache
232
+ /**
233
+ * Remove the cache from the file and clear the memory cache
227
234
  */
228
- destroy: function () {
229
- normalizedEntries = {};
230
- cache.destroy();
231
- },
232
-
233
- _getMetaForFileUsingCheckSum: function (cacheEntry) {
234
- var contentBuffer = fs.readFileSync(cacheEntry.key);
235
- var hash = this.getHash(contentBuffer);
236
- var meta = Object.assign(cacheEntry.meta, { hash: hash });
237
- delete meta.size;
238
- delete meta.mtime;
239
- return meta;
240
- },
241
-
242
- _getMetaForFileUsingMtimeAndSize: function (cacheEntry) {
243
- var stat = fs.statSync(cacheEntry.key);
244
- var meta = Object.assign(cacheEntry.meta, {
245
- size: stat.size,
246
- mtime: stat.mtime.getTime(),
247
- });
248
- delete meta.hash;
249
- return meta;
250
- },
251
-
252
- /**
235
+ destroy() {
236
+ normalizedEntries = {};
237
+ cache.destroy();
238
+ },
239
+
240
+ _getMetaForFileUsingCheckSum(cacheEntry) {
241
+ const contentBuffer = fs.readFileSync(cacheEntry.key);
242
+ const hash = this.getHash(contentBuffer);
243
+ const meta = Object.assign(cacheEntry.meta, {hash});
244
+ delete meta.size;
245
+ delete meta.mtime;
246
+ return meta;
247
+ },
248
+
249
+ _getMetaForFileUsingMtimeAndSize(cacheEntry) {
250
+ const stat = fs.statSync(cacheEntry.key);
251
+ const meta = Object.assign(cacheEntry.meta, {
252
+ size: stat.size,
253
+ mtime: stat.mtime.getTime(),
254
+ });
255
+ delete meta.hash;
256
+ return meta;
257
+ },
258
+
259
+ /**
253
260
  * Sync the files and persist them to the cache
254
261
  * @method reconcile
255
262
  */
256
- reconcile: function (noPrune) {
257
- removeNotFoundFiles();
258
-
259
- noPrune = typeof noPrune === 'undefined' ? true : noPrune;
260
-
261
- var entries = normalizedEntries;
262
- var keys = Object.keys(entries);
263
-
264
- if (keys.length === 0) {
265
- return;
266
- }
267
-
268
- var me = this;
269
-
270
- keys.forEach(function (entryName) {
271
- var cacheEntry = entries[entryName];
272
-
273
- try {
274
- var meta = useChecksum
275
- ? me._getMetaForFileUsingCheckSum(cacheEntry)
276
- : me._getMetaForFileUsingMtimeAndSize(cacheEntry);
277
- cache.setKey(entryName, meta);
278
- } catch (err) {
279
- // if the file does not exists we don't save it
280
- // other errors are just thrown
281
- if (err.code !== 'ENOENT') {
282
- throw err;
283
- }
284
- }
285
- });
286
-
287
- cache.save(noPrune);
288
- },
289
- };
290
- },
263
+ reconcile(noPrune) {
264
+ removeNotFoundFiles();
265
+
266
+ noPrune = noPrune === undefined ? true : noPrune;
267
+
268
+ const entries = normalizedEntries;
269
+ const keys = Object.keys(entries);
270
+
271
+ if (keys.length === 0) {
272
+ return;
273
+ }
274
+
275
+ const me = this;
276
+
277
+ for (const entryName of keys) {
278
+ const cacheEntry = entries[entryName];
279
+
280
+ try {
281
+ const meta = useChecksum
282
+ ? me._getMetaForFileUsingCheckSum(cacheEntry)
283
+ : me._getMetaForFileUsingMtimeAndSize(cacheEntry);
284
+ cache.setKey(entryName, meta);
285
+ } catch (error) {
286
+ // If the file does not exists we don't save it
287
+ // other errors are just thrown
288
+ if (error.code !== 'ENOENT') {
289
+ throw error;
290
+ }
291
+ }
292
+ }
293
+
294
+ cache.save(noPrune);
295
+ },
296
+ };
297
+ },
291
298
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "file-entry-cache",
3
- "version": "7.0.2",
3
+ "version": "9.0.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",
@@ -13,20 +13,19 @@
13
13
  "cache.js"
14
14
  ],
15
15
  "engines": {
16
- "node": ">=12.0.0"
16
+ "node": ">=18"
17
17
  },
18
18
  "scripts": {
19
- "eslint": "eslint --cache --cache-location=node_modules/.cache/ 'cache.js' 'test/**/*.js' 'perf.js'",
20
- "autofix": "npm run eslint -- --fix",
21
- "test": "npm run eslint --silent && c8 mocha -R spec test/specs",
22
- "test:ci": "npm run eslint --silent && c8 --reporter=lcov mocha -R spec test/specs",
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",
23
22
  "perf": "node perf.js"
24
23
  },
25
24
  "prepush": [
26
- "npm run eslint --silent"
25
+ "npm run test"
27
26
  ],
28
27
  "precommit": [
29
- "npm run eslint --silent"
28
+ "npm run test"
30
29
  ],
31
30
  "keywords": [
32
31
  "file cache",
@@ -37,18 +36,23 @@
37
36
  "cache"
38
37
  ],
39
38
  "devDependencies": {
40
- "c8": "^8.0.1",
39
+ "c8": "^9.1.0",
41
40
  "chai": "^4.3.10",
42
- "eslint": "^8.50.0",
43
- "eslint-config-prettier": "^9.0.0",
44
- "eslint-plugin-mocha": "^10.2.0",
45
- "eslint-plugin-prettier": "^3.1.4",
46
41
  "glob-expand": "^0.2.1",
47
- "mocha": "^10.2.0",
48
- "prettier": "^2.1.2",
49
- "write": "^2.0.0"
42
+ "mocha": "^10.4.0",
43
+ "rimraf": "^5.0.7",
44
+ "webpack": "^5.91.0",
45
+ "write": "^2.0.0",
46
+ "xo": "^0.58.0"
50
47
  },
51
48
  "dependencies": {
52
- "flat-cache": "^3.2.0"
49
+ "flat-cache": "^5.0.0"
50
+ },
51
+ "xo": {
52
+ "rules": {
53
+ "unicorn/prefer-module": "off",
54
+ "n/prefer-global/process": "off",
55
+ "unicorn/prevent-abbreviations": "off"
56
+ }
53
57
  }
54
58
  }