file-entry-cache 9.1.0 → 10.0.1

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.
package/cache.js DELETED
@@ -1,318 +0,0 @@
1
- /* eslint-disable unicorn/no-this-assignment, func-names, no-multi-assign */
2
- const path = require('node:path');
3
- const crypto = require('node:crypto');
4
-
5
- module.exports = {
6
- createFromFile(filePath, useChecksum, currentWorkingDir) {
7
- const fname = path.basename(filePath);
8
- const dir = path.dirname(filePath);
9
- return this.create(fname, dir, useChecksum, currentWorkingDir);
10
- },
11
-
12
- create(cacheId, _path, useChecksum, currentWorkingDir) {
13
- const fs = require('node:fs');
14
- const flatCache = require('flat-cache');
15
- const cache = flatCache.load(cacheId, _path);
16
- let normalizedEntries = {};
17
-
18
- const removeNotFoundFiles = function removeNotFoundFiles() {
19
- const cachedEntries = cache.keys();
20
- // Remove not found entries
21
- for (const fPath of cachedEntries) {
22
- try {
23
- let filePath = fPath;
24
- if (currentWorkingDir) {
25
- filePath = path.join(currentWorkingDir, fPath);
26
- }
27
-
28
- fs.statSync(filePath);
29
- } catch (error) {
30
- if (error.code === 'ENOENT') {
31
- cache.removeKey(fPath);
32
- }
33
- }
34
- }
35
- };
36
-
37
- removeNotFoundFiles();
38
-
39
- return {
40
- /**
41
- * The flat cache storage used to persist the metadata of the `files
42
- * @type {Object}
43
- */
44
- cache,
45
-
46
- /**
47
- * To enable relative paths as the key with current working directory
48
- * @type {string}
49
- */
50
- currentWorkingDir: currentWorkingDir ?? undefined,
51
-
52
- /**
53
- * Given a buffer, calculate md5 hash of its content.
54
- * @method getHash
55
- * @param {Buffer} buffer buffer to calculate hash on
56
- * @return {String} content hash digest
57
- */
58
- getHash(buffer) {
59
- return crypto.createHash('md5').update(buffer).digest('hex');
60
- },
61
-
62
- /**
63
- * Return whether or not a file has changed since last time reconcile was called.
64
- * @method hasFileChanged
65
- * @param {String} file the filepath to check
66
- * @return {Boolean} wheter or not the file has changed
67
- */
68
- hasFileChanged(file) {
69
- return this.getFileDescriptor(file).changed;
70
- },
71
-
72
- /**
73
- * Given an array of file paths it return and object with three arrays:
74
- * - changedFiles: Files that changed since previous run
75
- * - notChangedFiles: Files that haven't change
76
- * - notFoundFiles: Files that were not found, probably deleted
77
- *
78
- * @param {Array} files the files to analyze and compare to the previous seen files
79
- * @return {[type]} [description]
80
- */
81
- analyzeFiles(files) {
82
- const me = this;
83
- files ||= [];
84
-
85
- const res = {
86
- changedFiles: [],
87
- notFoundFiles: [],
88
- notChangedFiles: [],
89
- };
90
-
91
- for (const entry of me.normalizeEntries(files)) {
92
- if (entry.changed) {
93
- res.changedFiles.push(entry.key);
94
- continue;
95
- }
96
-
97
- if (entry.notFound) {
98
- res.notFoundFiles.push(entry.key);
99
- continue;
100
- }
101
-
102
- res.notChangedFiles.push(entry.key);
103
- }
104
-
105
- return res;
106
- },
107
-
108
- getFileDescriptor(file) {
109
- let fstat;
110
-
111
- try {
112
- fstat = fs.statSync(file);
113
- } catch (error) {
114
- this.removeEntry(file);
115
- return {key: file, notFound: true, err: error};
116
- }
117
-
118
- if (useChecksum) {
119
- return this._getFileDescriptorUsingChecksum(file);
120
- }
121
-
122
- return this._getFileDescriptorUsingMtimeAndSize(file, fstat);
123
- },
124
-
125
- _getFileKey(file) {
126
- if (this.currentWorkingDir) {
127
- return file.split(this.currentWorkingDir).pop();
128
- }
129
-
130
- return file;
131
- },
132
-
133
- _getFileDescriptorUsingMtimeAndSize(file, fstat) {
134
- let meta = cache.getKey(this._getFileKey(file));
135
- const cacheExists = Boolean(meta);
136
-
137
- const cSize = fstat.size;
138
- const cTime = fstat.mtime.getTime();
139
-
140
- let isDifferentDate;
141
- let isDifferentSize;
142
-
143
- if (meta) {
144
- isDifferentDate = cTime !== meta.mtime;
145
- isDifferentSize = cSize !== meta.size;
146
- } else {
147
- meta = {size: cSize, mtime: cTime};
148
- }
149
-
150
- const nEntry = (normalizedEntries[this._getFileKey(file)] = {
151
- key: this._getFileKey(file),
152
- changed: !cacheExists || isDifferentDate || isDifferentSize,
153
- meta,
154
- });
155
-
156
- return nEntry;
157
- },
158
-
159
- _getFileDescriptorUsingChecksum(file) {
160
- let meta = cache.getKey(this._getFileKey(file));
161
- const cacheExists = Boolean(meta);
162
-
163
- let contentBuffer;
164
- try {
165
- contentBuffer = fs.readFileSync(file);
166
- } catch {
167
- contentBuffer = '';
168
- }
169
-
170
- let isDifferent = true;
171
- const hash = this.getHash(contentBuffer);
172
-
173
- if (meta) {
174
- isDifferent = hash !== meta.hash;
175
- } else {
176
- meta = {hash};
177
- }
178
-
179
- const nEntry = (normalizedEntries[this._getFileKey(file)] = {
180
- key: this._getFileKey(file),
181
- changed: !cacheExists || isDifferent,
182
- meta,
183
- });
184
-
185
- return nEntry;
186
- },
187
-
188
- /**
189
- * Return the list o the files that changed compared
190
- * against the ones stored in the cache
191
- *
192
- * @method getUpdated
193
- * @param files {Array} the array of files to compare against the ones in the cache
194
- * @returns {Array}
195
- */
196
- getUpdatedFiles(files) {
197
- const me = this;
198
- files ||= [];
199
-
200
- return me
201
- .normalizeEntries(files)
202
- .filter(entry => entry.changed)
203
- .map(entry => entry.key);
204
- },
205
-
206
- /**
207
- * Return the list of files
208
- * @method normalizeEntries
209
- * @param files
210
- * @returns {*}
211
- */
212
- normalizeEntries(files) {
213
- files ||= [];
214
-
215
- const me = this;
216
- const nEntries = files.map(file => me.getFileDescriptor(file));
217
-
218
- // NormalizeEntries = nEntries;
219
- return nEntries;
220
- },
221
-
222
- /**
223
- * Remove an entry from the file-entry-cache. Useful to force the file to still be considered
224
- * modified the next time the process is run
225
- *
226
- * @method removeEntry
227
- * @param entryName
228
- */
229
- removeEntry(entryName) {
230
- delete normalizedEntries[this._getFileKey(entryName)];
231
- cache.removeKey(this._getFileKey(entryName));
232
- },
233
-
234
- /**
235
- * Delete the cache file from the disk
236
- * @method deleteCacheFile
237
- */
238
- deleteCacheFile() {
239
- cache.removeCacheFile();
240
- },
241
-
242
- /**
243
- * Remove the cache from the file and clear the memory cache
244
- */
245
- destroy() {
246
- normalizedEntries = {};
247
- cache.destroy();
248
- },
249
-
250
- _getMetaForFileUsingCheckSum(cacheEntry) {
251
- let filePath = cacheEntry.key;
252
- if (this.currentWorkingDir) {
253
- filePath = path.join(this.currentWorkingDir, filePath);
254
- }
255
-
256
- const contentBuffer = fs.readFileSync(filePath);
257
- const hash = this.getHash(contentBuffer);
258
- const meta = Object.assign(cacheEntry.meta, {hash});
259
- delete meta.size;
260
- delete meta.mtime;
261
- return meta;
262
- },
263
-
264
- _getMetaForFileUsingMtimeAndSize(cacheEntry) {
265
- let filePath = cacheEntry.key;
266
- if (currentWorkingDir) {
267
- filePath = path.join(currentWorkingDir, filePath);
268
- }
269
-
270
- const stat = fs.statSync(filePath);
271
- const meta = Object.assign(cacheEntry.meta, {
272
- size: stat.size,
273
- mtime: stat.mtime.getTime(),
274
- });
275
- delete meta.hash;
276
- return meta;
277
- },
278
-
279
- /**
280
- * Sync the files and persist them to the cache
281
- * @method reconcile
282
- */
283
- reconcile(noPrune) {
284
- removeNotFoundFiles();
285
-
286
- noPrune = noPrune === undefined ? true : noPrune;
287
-
288
- const entries = normalizedEntries;
289
- const keys = Object.keys(entries);
290
-
291
- if (keys.length === 0) {
292
- return;
293
- }
294
-
295
- const me = this;
296
-
297
- for (const entryName of keys) {
298
- const cacheEntry = entries[entryName];
299
-
300
- try {
301
- const meta = useChecksum
302
- ? me._getMetaForFileUsingCheckSum(cacheEntry)
303
- : me._getMetaForFileUsingMtimeAndSize(cacheEntry);
304
- cache.setKey(this._getFileKey(entryName), meta);
305
- } catch (error) {
306
- // If the file does not exists we don't save it
307
- // other errors are just thrown
308
- if (error.code !== 'ENOENT') {
309
- throw error;
310
- }
311
- }
312
- }
313
-
314
- cache.save(noPrune);
315
- },
316
- };
317
- },
318
- };