2021-02-26 03:58:33 +00:00
|
|
|
var path = require('path');
|
|
|
|
var crypto = require('crypto');
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2021-02-26 03:58:33 +00:00
|
|
|
createFromFile: function (filePath, useChecksum) {
|
|
|
|
var fname = path.basename(filePath);
|
|
|
|
var dir = path.dirname(filePath);
|
|
|
|
return this.create(fname, dir, useChecksum);
|
2020-08-25 23:57:08 +00:00
|
|
|
},
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
create: function (cacheId, _path, useChecksum) {
|
|
|
|
var fs = require('fs');
|
|
|
|
var flatCache = require('flat-cache');
|
|
|
|
var cache = flatCache.load(cacheId, _path);
|
|
|
|
var normalizedEntries = {};
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
var removeNotFoundFiles = function removeNotFoundFiles() {
|
|
|
|
const cachedEntries = cache.keys();
|
|
|
|
// remove not found entries
|
2021-02-26 03:58:33 +00:00
|
|
|
cachedEntries.forEach(function remover(fPath) {
|
2020-08-25 23:57:08 +00:00
|
|
|
try {
|
2021-02-26 03:58:33 +00:00
|
|
|
fs.statSync(fPath);
|
2020-08-25 23:57:08 +00:00
|
|
|
} catch (err) {
|
2021-02-26 03:58:33 +00:00
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
cache.removeKey(fPath);
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-26 03:58:33 +00:00
|
|
|
});
|
2020-08-25 23:57:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
removeNotFoundFiles();
|
|
|
|
|
|
|
|
return {
|
|
|
|
/**
|
|
|
|
* the flat cache storage used to persist the metadata of the `files
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
cache: cache,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a buffer, calculate md5 hash of its content.
|
|
|
|
* @method getHash
|
|
|
|
* @param {Buffer} buffer buffer to calculate hash on
|
|
|
|
* @return {String} content hash digest
|
|
|
|
*/
|
2021-02-26 03:58:33 +00:00
|
|
|
getHash: function (buffer) {
|
|
|
|
return crypto.createHash('md5').update(buffer).digest('hex');
|
2020-08-25 23:57:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether or not a file has changed since last time reconcile was called.
|
|
|
|
* @method hasFileChanged
|
|
|
|
* @param {String} file the filepath to check
|
|
|
|
* @return {Boolean} wheter or not the file has changed
|
|
|
|
*/
|
2021-02-26 03:58:33 +00:00
|
|
|
hasFileChanged: function (file) {
|
|
|
|
return this.getFileDescriptor(file).changed;
|
2020-08-25 23:57:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* given an array of file paths it return and object with three arrays:
|
|
|
|
* - changedFiles: Files that changed since previous run
|
|
|
|
* - notChangedFiles: Files that haven't change
|
|
|
|
* - notFoundFiles: Files that were not found, probably deleted
|
|
|
|
*
|
|
|
|
* @param {Array} files the files to analyze and compare to the previous seen files
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2021-02-26 03:58:33 +00:00
|
|
|
analyzeFiles: function (files) {
|
2020-08-25 23:57:08 +00:00
|
|
|
var me = this;
|
2021-02-26 03:58:33 +00:00
|
|
|
files = files || [];
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
var res = {
|
|
|
|
changedFiles: [],
|
|
|
|
notFoundFiles: [],
|
2021-02-26 03:58:33 +00:00
|
|
|
notChangedFiles: [],
|
2020-08-25 23:57:08 +00:00
|
|
|
};
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
me.normalizeEntries(files).forEach(function (entry) {
|
|
|
|
if (entry.changed) {
|
|
|
|
res.changedFiles.push(entry.key);
|
2020-08-25 23:57:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-02-26 03:58:33 +00:00
|
|
|
if (entry.notFound) {
|
|
|
|
res.notFoundFiles.push(entry.key);
|
2020-08-25 23:57:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-02-26 03:58:33 +00:00
|
|
|
res.notChangedFiles.push(entry.key);
|
|
|
|
});
|
2020-08-25 23:57:08 +00:00
|
|
|
return res;
|
|
|
|
},
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
getFileDescriptor: function (file) {
|
2020-08-25 23:57:08 +00:00
|
|
|
var fstat;
|
|
|
|
|
|
|
|
try {
|
2021-02-26 03:58:33 +00:00
|
|
|
fstat = fs.statSync(file);
|
2020-08-25 23:57:08 +00:00
|
|
|
} catch (ex) {
|
2021-02-26 03:58:33 +00:00
|
|
|
this.removeEntry(file);
|
2020-08-25 23:57:08 +00:00
|
|
|
return { key: file, notFound: true, err: ex };
|
|
|
|
}
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
if (useChecksum) {
|
|
|
|
return this._getFileDescriptorUsingChecksum(file);
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
return this._getFileDescriptorUsingMtimeAndSize(file, fstat);
|
2020-08-25 23:57:08 +00:00
|
|
|
},
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
_getFileDescriptorUsingMtimeAndSize: function (file, fstat) {
|
|
|
|
var meta = cache.getKey(file);
|
2020-08-25 23:57:08 +00:00
|
|
|
var cacheExists = !!meta;
|
|
|
|
|
|
|
|
var cSize = fstat.size;
|
|
|
|
var cTime = fstat.mtime.getTime();
|
|
|
|
|
|
|
|
var isDifferentDate;
|
|
|
|
var isDifferentSize;
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
if (!meta) {
|
2020-08-25 23:57:08 +00:00
|
|
|
meta = { size: cSize, mtime: cTime };
|
|
|
|
} else {
|
|
|
|
isDifferentDate = cTime !== meta.mtime;
|
|
|
|
isDifferentSize = cSize !== meta.size;
|
|
|
|
}
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
var nEntry = (normalizedEntries[file] = {
|
2020-08-25 23:57:08 +00:00
|
|
|
key: file,
|
|
|
|
changed: !cacheExists || isDifferentDate || isDifferentSize,
|
2021-02-26 03:58:33 +00:00
|
|
|
meta: meta,
|
|
|
|
});
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
return nEntry;
|
|
|
|
},
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
_getFileDescriptorUsingChecksum: function (file) {
|
|
|
|
var meta = cache.getKey(file);
|
2020-08-25 23:57:08 +00:00
|
|
|
var cacheExists = !!meta;
|
|
|
|
|
|
|
|
var contentBuffer;
|
|
|
|
try {
|
2021-02-26 03:58:33 +00:00
|
|
|
contentBuffer = fs.readFileSync(file);
|
2020-08-25 23:57:08 +00:00
|
|
|
} catch (ex) {
|
|
|
|
contentBuffer = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
var isDifferent = true;
|
2021-02-26 03:58:33 +00:00
|
|
|
var hash = this.getHash(contentBuffer);
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
if (!meta) {
|
2020-08-25 23:57:08 +00:00
|
|
|
meta = { hash: hash };
|
|
|
|
} else {
|
|
|
|
isDifferent = hash !== meta.hash;
|
|
|
|
}
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
var nEntry = (normalizedEntries[file] = {
|
2020-08-25 23:57:08 +00:00
|
|
|
key: file,
|
|
|
|
changed: !cacheExists || isDifferent,
|
2021-02-26 03:58:33 +00:00
|
|
|
meta: meta,
|
|
|
|
});
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
return nEntry;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the list o the files that changed compared
|
|
|
|
* against the ones stored in the cache
|
|
|
|
*
|
|
|
|
* @method getUpdated
|
|
|
|
* @param files {Array} the array of files to compare against the ones in the cache
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
2021-02-26 03:58:33 +00:00
|
|
|
getUpdatedFiles: function (files) {
|
2020-08-25 23:57:08 +00:00
|
|
|
var me = this;
|
2021-02-26 03:58:33 +00:00
|
|
|
files = files || [];
|
|
|
|
|
|
|
|
return me
|
|
|
|
.normalizeEntries(files)
|
|
|
|
.filter(function (entry) {
|
|
|
|
return entry.changed;
|
|
|
|
})
|
|
|
|
.map(function (entry) {
|
|
|
|
return entry.key;
|
|
|
|
});
|
2020-08-25 23:57:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return the list of files
|
|
|
|
* @method normalizeEntries
|
|
|
|
* @param files
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2021-02-26 03:58:33 +00:00
|
|
|
normalizeEntries: function (files) {
|
|
|
|
files = files || [];
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
var me = this;
|
2021-02-26 03:58:33 +00:00
|
|
|
var nEntries = files.map(function (file) {
|
|
|
|
return me.getFileDescriptor(file);
|
|
|
|
});
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
//normalizeEntries = nEntries;
|
|
|
|
return nEntries;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an entry from the file-entry-cache. Useful to force the file to still be considered
|
|
|
|
* modified the next time the process is run
|
|
|
|
*
|
|
|
|
* @method removeEntry
|
|
|
|
* @param entryName
|
|
|
|
*/
|
2021-02-26 03:58:33 +00:00
|
|
|
removeEntry: function (entryName) {
|
|
|
|
delete normalizedEntries[entryName];
|
|
|
|
cache.removeKey(entryName);
|
2020-08-25 23:57:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the cache file from the disk
|
|
|
|
* @method deleteCacheFile
|
|
|
|
*/
|
|
|
|
deleteCacheFile: function () {
|
|
|
|
cache.removeCacheFile();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remove the cache from the file and clear the memory cache
|
|
|
|
*/
|
|
|
|
destroy: function () {
|
2021-02-26 03:58:33 +00:00
|
|
|
normalizedEntries = {};
|
2020-08-25 23:57:08 +00:00
|
|
|
cache.destroy();
|
|
|
|
},
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
_getMetaForFileUsingCheckSum: function (cacheEntry) {
|
|
|
|
var contentBuffer = fs.readFileSync(cacheEntry.key);
|
|
|
|
var hash = this.getHash(contentBuffer);
|
|
|
|
var meta = Object.assign(cacheEntry.meta, { hash: hash });
|
|
|
|
delete meta.size;
|
|
|
|
delete meta.mtime;
|
2020-08-25 23:57:08 +00:00
|
|
|
return meta;
|
|
|
|
},
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
_getMetaForFileUsingMtimeAndSize: function (cacheEntry) {
|
|
|
|
var stat = fs.statSync(cacheEntry.key);
|
|
|
|
var meta = Object.assign(cacheEntry.meta, {
|
2020-08-25 23:57:08 +00:00
|
|
|
size: stat.size,
|
2021-02-26 03:58:33 +00:00
|
|
|
mtime: stat.mtime.getTime(),
|
|
|
|
});
|
|
|
|
delete meta.hash;
|
2020-08-25 23:57:08 +00:00
|
|
|
return meta;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync the files and persist them to the cache
|
|
|
|
* @method reconcile
|
|
|
|
*/
|
2021-02-26 03:58:33 +00:00
|
|
|
reconcile: function (noPrune) {
|
2020-08-25 23:57:08 +00:00
|
|
|
removeNotFoundFiles();
|
|
|
|
|
|
|
|
noPrune = typeof noPrune === 'undefined' ? true : noPrune;
|
|
|
|
|
|
|
|
var entries = normalizedEntries;
|
2021-02-26 03:58:33 +00:00
|
|
|
var keys = Object.keys(entries);
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
if (keys.length === 0) {
|
2020-08-25 23:57:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var me = this;
|
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
keys.forEach(function (entryName) {
|
|
|
|
var cacheEntry = entries[entryName];
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
try {
|
2021-02-26 03:58:33 +00:00
|
|
|
var meta = useChecksum
|
|
|
|
? me._getMetaForFileUsingCheckSum(cacheEntry)
|
|
|
|
: me._getMetaForFileUsingMtimeAndSize(cacheEntry);
|
|
|
|
cache.setKey(entryName, meta);
|
2020-08-25 23:57:08 +00:00
|
|
|
} catch (err) {
|
|
|
|
// if the file does not exists we don't save it
|
|
|
|
// other errors are just thrown
|
2021-02-26 03:58:33 +00:00
|
|
|
if (err.code !== 'ENOENT') {
|
2020-08-25 23:57:08 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2021-02-26 03:58:33 +00:00
|
|
|
});
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2021-02-26 03:58:33 +00:00
|
|
|
cache.save(noPrune);
|
|
|
|
},
|
2020-08-25 23:57:08 +00:00
|
|
|
};
|
2021-02-26 03:58:33 +00:00
|
|
|
},
|
2020-08-25 23:57:08 +00:00
|
|
|
};
|