1 var path = require('path');
2 var crypto = require('crypto');
5 createFromFile: function (filePath, useChecksum) {
6 var fname = path.basename(filePath);
7 var dir = path.dirname(filePath);
8 return this.create(fname, dir, useChecksum);
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 = {};
17 var removeNotFoundFiles = function removeNotFoundFiles() {
18 const cachedEntries = cache.keys();
19 // remove not found entries
20 cachedEntries.forEach(function remover(fPath) {
24 if (err.code === 'ENOENT') {
25 cache.removeKey(fPath);
31 removeNotFoundFiles();
35 * the flat cache storage used to persist the metadata of the `files
41 * Given a buffer, calculate md5 hash of its content.
43 * @param {Buffer} buffer buffer to calculate hash on
44 * @return {String} content hash digest
46 getHash: function (buffer) {
47 return crypto.createHash('md5').update(buffer).digest('hex');
51 * Return whether or not a file has changed since last time reconcile was called.
52 * @method hasFileChanged
53 * @param {String} file the filepath to check
54 * @return {Boolean} wheter or not the file has changed
56 hasFileChanged: function (file) {
57 return this.getFileDescriptor(file).changed;
61 * given an array of file paths it return and object with three arrays:
62 * - changedFiles: Files that changed since previous run
63 * - notChangedFiles: Files that haven't change
64 * - notFoundFiles: Files that were not found, probably deleted
66 * @param {Array} files the files to analyze and compare to the previous seen files
67 * @return {[type]} [description]
69 analyzeFiles: function (files) {
79 me.normalizeEntries(files).forEach(function (entry) {
81 res.changedFiles.push(entry.key);
85 res.notFoundFiles.push(entry.key);
88 res.notChangedFiles.push(entry.key);
93 getFileDescriptor: function (file) {
97 fstat = fs.statSync(file);
99 this.removeEntry(file);
100 return { key: file, notFound: true, err: ex };
104 return this._getFileDescriptorUsingChecksum(file);
107 return this._getFileDescriptorUsingMtimeAndSize(file, fstat);
110 _getFileDescriptorUsingMtimeAndSize: function (file, fstat) {
111 var meta = cache.getKey(file);
112 var cacheExists = !!meta;
114 var cSize = fstat.size;
115 var cTime = fstat.mtime.getTime();
121 meta = { size: cSize, mtime: cTime };
123 isDifferentDate = cTime !== meta.mtime;
124 isDifferentSize = cSize !== meta.size;
127 var nEntry = (normalizedEntries[file] = {
129 changed: !cacheExists || isDifferentDate || isDifferentSize,
136 _getFileDescriptorUsingChecksum: function (file) {
137 var meta = cache.getKey(file);
138 var cacheExists = !!meta;
142 contentBuffer = fs.readFileSync(file);
147 var isDifferent = true;
148 var hash = this.getHash(contentBuffer);
151 meta = { hash: hash };
153 isDifferent = hash !== meta.hash;
156 var nEntry = (normalizedEntries[file] = {
158 changed: !cacheExists || isDifferent,
166 * Return the list o the files that changed compared
167 * against the ones stored in the cache
170 * @param files {Array} the array of files to compare against the ones in the cache
173 getUpdatedFiles: function (files) {
178 .normalizeEntries(files)
179 .filter(function (entry) {
180 return entry.changed;
182 .map(function (entry) {
188 * return the list of files
189 * @method normalizeEntries
193 normalizeEntries: function (files) {
197 var nEntries = files.map(function (file) {
198 return me.getFileDescriptor(file);
201 //normalizeEntries = nEntries;
206 * Remove an entry from the file-entry-cache. Useful to force the file to still be considered
207 * modified the next time the process is run
209 * @method removeEntry
212 removeEntry: function (entryName) {
213 delete normalizedEntries[entryName];
214 cache.removeKey(entryName);
218 * Delete the cache file from the disk
219 * @method deleteCacheFile
221 deleteCacheFile: function () {
222 cache.removeCacheFile();
226 * remove the cache from the file and clear the memory cache
228 destroy: function () {
229 normalizedEntries = {};
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 });
242 _getMetaForFileUsingMtimeAndSize: function (cacheEntry) {
243 var stat = fs.statSync(cacheEntry.key);
244 var meta = Object.assign(cacheEntry.meta, {
246 mtime: stat.mtime.getTime(),
253 * Sync the files and persist them to the cache
256 reconcile: function (noPrune) {
257 removeNotFoundFiles();
259 noPrune = typeof noPrune === 'undefined' ? true : noPrune;
261 var entries = normalizedEntries;
262 var keys = Object.keys(entries);
264 if (keys.length === 0) {
270 keys.forEach(function (entryName) {
271 var cacheEntry = entries[entryName];
274 var meta = useChecksum
275 ? me._getMetaForFileUsingCheckSum(cacheEntry)
276 : me._getMetaForFileUsingMtimeAndSize(cacheEntry);
277 cache.setKey(entryName, meta);
279 // if the file does not exists we don't save it
280 // other errors are just thrown
281 if (err.code !== 'ENOENT') {