Yang Guo | 4fd355c | 2019-09-19 08:59:03 | [diff] [blame] | 1 | var path = require( 'path' ); |
| 2 | var crypto = require( 'crypto' ); |
| 3 | |
| 4 | module.exports = { |
| 5 | createFromFile: function ( filePath, useChecksum ) { |
| 6 | var fname = path.basename( filePath ); |
| 7 | var dir = path.dirname( filePath ); |
| 8 | return this.create( fname, dir, useChecksum ); |
| 9 | }, |
| 10 | |
| 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 = { }; |
| 16 | |
| 17 | var removeNotFoundFiles = function removeNotFoundFiles() { |
| 18 | const cachedEntries = cache.keys(); |
| 19 | // remove not found entries |
| 20 | cachedEntries.forEach( function remover( fPath ) { |
| 21 | try { |
| 22 | fs.statSync( fPath ); |
| 23 | } catch (err) { |
| 24 | if ( err.code === 'ENOENT' ) { |
| 25 | cache.removeKey( fPath ); |
| 26 | } |
| 27 | } |
| 28 | } ); |
| 29 | }; |
| 30 | |
| 31 | removeNotFoundFiles(); |
| 32 | |
| 33 | return { |
| 34 | /** |
| 35 | * the flat cache storage used to persist the metadata of the `files |
| 36 | * @type {Object} |
| 37 | */ |
| 38 | cache: cache, |
| 39 | |
| 40 | /** |
| 41 | * Given a buffer, calculate md5 hash of its content. |
| 42 | * @method getHash |
| 43 | * @param {Buffer} buffer buffer to calculate hash on |
| 44 | * @return {String} content hash digest |
| 45 | */ |
| 46 | getHash: function ( buffer ) { |
| 47 | return crypto |
| 48 | .createHash( 'md5' ) |
| 49 | .update( buffer ) |
| 50 | .digest( 'hex' ); |
| 51 | }, |
| 52 | |
| 53 | /** |
| 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 |
| 58 | */ |
| 59 | hasFileChanged: function ( file ) { |
| 60 | return this.getFileDescriptor( file ).changed; |
| 61 | }, |
| 62 | |
| 63 | /** |
| 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 |
| 68 | * |
| 69 | * @param {Array} files the files to analyze and compare to the previous seen files |
| 70 | * @return {[type]} [description] |
| 71 | */ |
| 72 | analyzeFiles: function ( files ) { |
| 73 | var me = this; |
| 74 | files = files || [ ]; |
| 75 | |
| 76 | var res = { |
| 77 | changedFiles: [], |
| 78 | notFoundFiles: [], |
| 79 | notChangedFiles: [] |
| 80 | }; |
| 81 | |
| 82 | me.normalizeEntries( files ).forEach( function ( entry ) { |
| 83 | if ( entry.changed ) { |
| 84 | res.changedFiles.push( entry.key ); |
| 85 | return; |
| 86 | } |
| 87 | if ( entry.notFound ) { |
| 88 | res.notFoundFiles.push( entry.key ); |
| 89 | return; |
| 90 | } |
| 91 | res.notChangedFiles.push( entry.key ); |
| 92 | } ); |
| 93 | return res; |
| 94 | }, |
| 95 | |
| 96 | getFileDescriptor: function ( file ) { |
| 97 | var fstat; |
| 98 | |
| 99 | try { |
| 100 | fstat = fs.statSync( file ); |
| 101 | } catch (ex) { |
| 102 | this.removeEntry( file ); |
| 103 | return { key: file, notFound: true, err: ex }; |
| 104 | } |
| 105 | |
| 106 | if ( useChecksum ) { |
| 107 | return this._getFileDescriptorUsingChecksum( file ); |
| 108 | } |
| 109 | |
| 110 | return this._getFileDescriptorUsingMtimeAndSize( file, fstat ); |
| 111 | }, |
| 112 | |
| 113 | _getFileDescriptorUsingMtimeAndSize: function ( file, fstat ) { |
| 114 | var meta = cache.getKey( file ); |
| 115 | var cacheExists = !!meta; |
| 116 | |
| 117 | var cSize = fstat.size; |
| 118 | var cTime = fstat.mtime.getTime(); |
| 119 | |
| 120 | var isDifferentDate; |
| 121 | var isDifferentSize; |
| 122 | |
| 123 | if ( !meta ) { |
| 124 | meta = { size: cSize, mtime: cTime }; |
| 125 | } else { |
| 126 | isDifferentDate = cTime !== meta.mtime; |
| 127 | isDifferentSize = cSize !== meta.size; |
| 128 | } |
| 129 | |
| 130 | var nEntry = normalizedEntries[ file ] = { |
| 131 | key: file, |
| 132 | changed: !cacheExists || isDifferentDate || isDifferentSize, |
| 133 | meta: meta |
| 134 | }; |
| 135 | |
| 136 | return nEntry; |
| 137 | }, |
| 138 | |
| 139 | _getFileDescriptorUsingChecksum: function ( file ) { |
| 140 | var meta = cache.getKey( file ); |
| 141 | var cacheExists = !!meta; |
| 142 | |
| 143 | var contentBuffer; |
| 144 | try { |
| 145 | contentBuffer = fs.readFileSync( file ); |
| 146 | } catch (ex) { |
| 147 | contentBuffer = ''; |
| 148 | } |
| 149 | |
| 150 | var isDifferent = true; |
| 151 | var hash = this.getHash( contentBuffer ); |
| 152 | |
| 153 | if ( !meta ) { |
| 154 | meta = { hash: hash }; |
| 155 | } else { |
| 156 | isDifferent = hash !== meta.hash; |
| 157 | } |
| 158 | |
| 159 | var nEntry = normalizedEntries[ file ] = { |
| 160 | key: file, |
| 161 | changed: !cacheExists || isDifferent, |
| 162 | meta: meta |
| 163 | }; |
| 164 | |
| 165 | return nEntry; |
| 166 | }, |
| 167 | |
| 168 | /** |
| 169 | * Return the list o the files that changed compared |
| 170 | * against the ones stored in the cache |
| 171 | * |
| 172 | * @method getUpdated |
| 173 | * @param files {Array} the array of files to compare against the ones in the cache |
| 174 | * @returns {Array} |
| 175 | */ |
| 176 | getUpdatedFiles: function ( files ) { |
| 177 | var me = this; |
| 178 | files = files || [ ]; |
| 179 | |
| 180 | return me.normalizeEntries( files ).filter( function ( entry ) { |
| 181 | return entry.changed; |
| 182 | } ).map( function ( entry ) { |
| 183 | return entry.key; |
| 184 | } ); |
| 185 | }, |
| 186 | |
| 187 | /** |
| 188 | * return the list of files |
| 189 | * @method normalizeEntries |
| 190 | * @param files |
| 191 | * @returns {*} |
| 192 | */ |
| 193 | normalizeEntries: function ( files ) { |
| 194 | files = files || [ ]; |
| 195 | |
| 196 | var me = this; |
| 197 | var nEntries = files.map( function ( file ) { |
| 198 | return me.getFileDescriptor( file ); |
| 199 | } ); |
| 200 | |
| 201 | //normalizeEntries = nEntries; |
| 202 | return nEntries; |
| 203 | }, |
| 204 | |
| 205 | /** |
| 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 |
| 208 | * |
| 209 | * @method removeEntry |
| 210 | * @param entryName |
| 211 | */ |
| 212 | removeEntry: function ( entryName ) { |
| 213 | delete normalizedEntries[ entryName ]; |
| 214 | cache.removeKey( entryName ); |
| 215 | }, |
| 216 | |
| 217 | /** |
| 218 | * Delete the cache file from the disk |
| 219 | * @method deleteCacheFile |
| 220 | */ |
| 221 | deleteCacheFile: function () { |
| 222 | cache.removeCacheFile(); |
| 223 | }, |
| 224 | |
| 225 | /** |
| 226 | * remove the cache from the file and clear the memory cache |
| 227 | */ |
| 228 | destroy: function () { |
| 229 | normalizedEntries = { }; |
| 230 | cache.destroy(); |
| 231 | }, |
| 232 | |
| 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 } ); |
| 237 | return meta; |
| 238 | }, |
| 239 | |
| 240 | _getMetaForFileUsingMtimeAndSize: function ( cacheEntry ) { |
| 241 | var stat = fs.statSync( cacheEntry.key ); |
| 242 | var meta = Object.assign( cacheEntry.meta, { |
| 243 | size: stat.size, |
| 244 | mtime: stat.mtime.getTime() |
| 245 | } ); |
| 246 | return meta; |
| 247 | }, |
| 248 | |
| 249 | /** |
| 250 | * Sync the files and persist them to the cache |
| 251 | * @method reconcile |
| 252 | */ |
| 253 | reconcile: function ( noPrune ) { |
| 254 | removeNotFoundFiles(); |
| 255 | |
| 256 | noPrune = typeof noPrune === 'undefined' ? true : noPrune; |
| 257 | |
| 258 | var entries = normalizedEntries; |
| 259 | var keys = Object.keys( entries ); |
| 260 | |
| 261 | if ( keys.length === 0 ) { |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | var me = this; |
| 266 | |
| 267 | keys.forEach( function ( entryName ) { |
| 268 | var cacheEntry = entries[ entryName ]; |
| 269 | |
| 270 | try { |
| 271 | var meta = useChecksum ? me._getMetaForFileUsingCheckSum( cacheEntry ) : me._getMetaForFileUsingMtimeAndSize( cacheEntry ); |
| 272 | cache.setKey( entryName, meta ); |
| 273 | } catch (err) { |
| 274 | // if the file does not exists we don't save it |
| 275 | // other errors are just thrown |
| 276 | if ( err.code !== 'ENOENT' ) { |
| 277 | throw err; |
| 278 | } |
| 279 | } |
| 280 | } ); |
| 281 | |
| 282 | cache.save( noPrune ); |
| 283 | } |
| 284 | }; |
| 285 | } |
| 286 | }; |