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 ) {
54 * Return whether or not a file has changed since last time reconcile was called.
55 * @method hasFileChanged
56 * @param {String} file the filepath to check
57 * @return {Boolean} wheter or not the file has changed
59 hasFileChanged: function ( file ) {
60 return this.getFileDescriptor( file ).changed;
64 * given an array of file paths it return and object with three arrays:
65 * - changedFiles: Files that changed since previous run
66 * - notChangedFiles: Files that haven't change
67 * - notFoundFiles: Files that were not found, probably deleted
69 * @param {Array} files the files to analyze and compare to the previous seen files
70 * @return {[type]} [description]
72 analyzeFiles: function ( files ) {
82 me.normalizeEntries( files ).forEach( function ( entry ) {
83 if ( entry.changed ) {
84 res.changedFiles.push( entry.key );
87 if ( entry.notFound ) {
88 res.notFoundFiles.push( entry.key );
91 res.notChangedFiles.push( entry.key );
96 getFileDescriptor: function ( file ) {
100 fstat = fs.statSync( file );
102 this.removeEntry( file );
103 return { key: file, notFound: true, err: ex };
107 return this._getFileDescriptorUsingChecksum( file );
110 return this._getFileDescriptorUsingMtimeAndSize( file, fstat );
113 _getFileDescriptorUsingMtimeAndSize: function ( file, fstat ) {
114 var meta = cache.getKey( file );
115 var cacheExists = !!meta;
117 var cSize = fstat.size;
118 var cTime = fstat.mtime.getTime();
124 meta = { size: cSize, mtime: cTime };
126 isDifferentDate = cTime !== meta.mtime;
127 isDifferentSize = cSize !== meta.size;
130 var nEntry = normalizedEntries[ file ] = {
132 changed: !cacheExists || isDifferentDate || isDifferentSize,
139 _getFileDescriptorUsingChecksum: function ( file ) {
140 var meta = cache.getKey( file );
141 var cacheExists = !!meta;
145 contentBuffer = fs.readFileSync( file );
150 var isDifferent = true;
151 var hash = this.getHash( contentBuffer );
154 meta = { hash: hash };
156 isDifferent = hash !== meta.hash;
159 var nEntry = normalizedEntries[ file ] = {
161 changed: !cacheExists || isDifferent,
169 * Return the list o the files that changed compared
170 * against the ones stored in the cache
173 * @param files {Array} the array of files to compare against the ones in the cache
176 getUpdatedFiles: function ( files ) {
178 files = files || [ ];
180 return me.normalizeEntries( files ).filter( function ( entry ) {
181 return entry.changed;
182 } ).map( function ( entry ) {
188 * return the list of files
189 * @method normalizeEntries
193 normalizeEntries: function ( files ) {
194 files = 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 } );
240 _getMetaForFileUsingMtimeAndSize: function ( cacheEntry ) {
241 var stat = fs.statSync( cacheEntry.key );
242 var meta = Object.assign( cacheEntry.meta, {
244 mtime: stat.mtime.getTime()
250 * Sync the files and persist them to the cache
253 reconcile: function ( noPrune ) {
254 removeNotFoundFiles();
256 noPrune = typeof noPrune === 'undefined' ? true : noPrune;
258 var entries = normalizedEntries;
259 var keys = Object.keys( entries );
261 if ( keys.length === 0 ) {
267 keys.forEach( function ( entryName ) {
268 var cacheEntry = entries[ entryName ];
271 var meta = useChecksum ? me._getMetaForFileUsingCheckSum( cacheEntry ) : me._getMetaForFileUsingMtimeAndSize( cacheEntry );
272 cache.setKey( entryName, meta );
274 // if the file does not exists we don't save it
275 // other errors are just thrown
276 if ( err.code !== 'ENOENT' ) {
282 cache.save( noPrune );