diff options
Diffstat (limited to 'tools/node_modules/eslint/node_modules/file-entry-cache/cache.js')
-rw-r--r-- | tools/node_modules/eslint/node_modules/file-entry-cache/cache.js | 106 |
1 files changed, 88 insertions, 18 deletions
diff --git a/tools/node_modules/eslint/node_modules/file-entry-cache/cache.js b/tools/node_modules/eslint/node_modules/file-entry-cache/cache.js index c37d679f63..ebc6386d1c 100644 --- a/tools/node_modules/eslint/node_modules/file-entry-cache/cache.js +++ b/tools/node_modules/eslint/node_modules/file-entry-cache/cache.js @@ -1,17 +1,17 @@ var path = require( 'path' ); +var crypto = require( 'crypto' ); module.exports = { - createFromFile: function ( filePath ) { + createFromFile: function ( filePath, useChecksum ) { var fname = path.basename( filePath ); var dir = path.dirname( filePath ); - return this.create( fname, dir ); + return this.create( fname, dir, useChecksum ); }, - create: function ( cacheId, _path ) { + create: function ( cacheId, _path, useChecksum ) { var fs = require( 'fs' ); var flatCache = require( 'flat-cache' ); var cache = flatCache.load( cacheId, _path ); - var assign = require( 'object-assign' ); var normalizedEntries = { }; var removeNotFoundFiles = function removeNotFoundFiles() { @@ -36,6 +36,20 @@ module.exports = { * @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 + */ + getHash: function ( buffer ) { + return crypto + .createHash( 'md5' ) + .update( buffer ) + .digest( 'hex' ); + }, + /** * Return whether or not a file has changed since last time reconcile was called. * @method hasFileChanged @@ -80,26 +94,37 @@ module.exports = { }, getFileDescriptor: function ( file ) { - var meta = cache.getKey( file ); - var cacheExists = !!meta; var fstat; - var me = this; try { fstat = fs.statSync( file ); } catch (ex) { - me.removeEntry( file ); + this.removeEntry( file ); return { key: file, notFound: true, err: ex }; } + if ( useChecksum ) { + return this._getFileDescriptorUsingChecksum( file ); + } + + return this._getFileDescriptorUsingMtimeAndSize( file, fstat ); + }, + + _getFileDescriptorUsingMtimeAndSize: function ( file, fstat ) { + var meta = cache.getKey( file ); + var cacheExists = !!meta; + var cSize = fstat.size; var cTime = fstat.mtime.getTime(); + var isDifferentDate; + var isDifferentSize; + if ( !meta ) { meta = { size: cSize, mtime: cTime }; } else { - var isDifferentDate = cTime !== meta.mtime; - var isDifferentSize = cSize !== meta.size; + isDifferentDate = cTime !== meta.mtime; + isDifferentSize = cSize !== meta.size; } var nEntry = normalizedEntries[ file ] = { @@ -111,6 +136,35 @@ module.exports = { return nEntry; }, + _getFileDescriptorUsingChecksum: function ( file ) { + var meta = cache.getKey( file ); + var cacheExists = !!meta; + + var contentBuffer; + try { + contentBuffer = fs.readFileSync( file ); + } catch (ex) { + contentBuffer = ''; + } + + var isDifferent = true; + var hash = this.getHash( contentBuffer ); + + if ( !meta ) { + meta = { hash: hash }; + } else { + isDifferent = hash !== meta.hash; + } + + var nEntry = normalizedEntries[ file ] = { + key: file, + changed: !cacheExists || isDifferent, + meta: meta + }; + + return nEntry; + }, + /** * Return the list o the files that changed compared * against the ones stored in the cache @@ -175,13 +229,32 @@ module.exports = { normalizedEntries = { }; cache.destroy(); }, + + _getMetaForFileUsingCheckSum: function ( cacheEntry ) { + var contentBuffer = fs.readFileSync( cacheEntry.key ); + var hash = this.getHash( contentBuffer ); + var meta = Object.assign( cacheEntry.meta, { hash: hash } ); + return meta; + }, + + _getMetaForFileUsingMtimeAndSize: function ( cacheEntry ) { + var stat = fs.statSync( cacheEntry.key ); + var meta = Object.assign( cacheEntry.meta, { + size: stat.size, + mtime: stat.mtime.getTime() + } ); + return meta; + }, + /** * Sync the files and persist them to the cache * @method reconcile */ - reconcile: function () { + reconcile: function ( noPrune ) { removeNotFoundFiles(); + noPrune = typeof noPrune === 'undefined' ? true : noPrune; + var entries = normalizedEntries; var keys = Object.keys( entries ); @@ -189,16 +262,13 @@ module.exports = { return; } + var me = this; + keys.forEach( function ( entryName ) { var cacheEntry = entries[ entryName ]; try { - var stat = fs.statSync( cacheEntry.key ); - var meta = assign( cacheEntry.meta, { - size: stat.size, - mtime: stat.mtime.getTime() - } ); - + var meta = useChecksum ? me._getMetaForFileUsingCheckSum( cacheEntry ) : me._getMetaForFileUsingMtimeAndSize( cacheEntry ); cache.setKey( entryName, meta ); } catch (err) { // if the file does not exists we don't save it @@ -209,7 +279,7 @@ module.exports = { } } ); - cache.save( true ); + cache.save( noPrune ); } }; } |