Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cache.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. var path = require( 'path' );
  2. module.exports = {
  3. createFromFile: function ( filePath ) {
  4. var fname = path.basename( filePath );
  5. var dir = path.dirname( filePath );
  6. return this.create( fname, dir );
  7. },
  8. create: function ( cacheId, _path ) {
  9. var fs = require( 'fs' );
  10. var flatCache = require( 'flat-cache' );
  11. var cache = flatCache.load( cacheId, _path );
  12. var assign = require( 'object-assign' );
  13. var normalizedEntries = { };
  14. var removeNotFoundFiles = function removeNotFoundFiles() {
  15. const cachedEntries = cache.keys();
  16. // remove not found entries
  17. cachedEntries.forEach( function remover( fPath ) {
  18. try {
  19. fs.statSync( fPath );
  20. } catch (err) {
  21. if ( err.code === 'ENOENT' ) {
  22. cache.removeKey( fPath );
  23. }
  24. }
  25. } );
  26. };
  27. removeNotFoundFiles();
  28. return {
  29. /**
  30. * the flat cache storage used to persist the metadata of the `files
  31. * @type {Object}
  32. */
  33. cache: cache,
  34. /**
  35. * Return whether or not a file has changed since last time reconcile was called.
  36. * @method hasFileChanged
  37. * @param {String} file the filepath to check
  38. * @return {Boolean} wheter or not the file has changed
  39. */
  40. hasFileChanged: function ( file ) {
  41. return this.getFileDescriptor( file ).changed;
  42. },
  43. /**
  44. * given an array of file paths it return and object with three arrays:
  45. * - changedFiles: Files that changed since previous run
  46. * - notChangedFiles: Files that haven't change
  47. * - notFoundFiles: Files that were not found, probably deleted
  48. *
  49. * @param {Array} files the files to analyze and compare to the previous seen files
  50. * @return {[type]} [description]
  51. */
  52. analyzeFiles: function ( files ) {
  53. var me = this;
  54. files = files || [ ];
  55. var res = {
  56. changedFiles: [],
  57. notFoundFiles: [],
  58. notChangedFiles: []
  59. };
  60. me.normalizeEntries( files ).forEach( function ( entry ) {
  61. if ( entry.changed ) {
  62. res.changedFiles.push( entry.key );
  63. return;
  64. }
  65. if ( entry.notFound ) {
  66. res.notFoundFiles.push( entry.key );
  67. return;
  68. }
  69. res.notChangedFiles.push( entry.key );
  70. } );
  71. return res;
  72. },
  73. getFileDescriptor: function ( file ) {
  74. var meta = cache.getKey( file );
  75. var cacheExists = !!meta;
  76. var fstat;
  77. var me = this;
  78. try {
  79. fstat = fs.statSync( file );
  80. } catch (ex) {
  81. me.removeEntry( file );
  82. return { key: file, notFound: true, err: ex };
  83. }
  84. var cSize = fstat.size;
  85. var cTime = fstat.mtime.getTime();
  86. if ( !meta ) {
  87. meta = { size: cSize, mtime: cTime };
  88. } else {
  89. var isDifferentDate = cTime !== meta.mtime;
  90. var isDifferentSize = cSize !== meta.size;
  91. }
  92. var nEntry = normalizedEntries[ file ] = {
  93. key: file,
  94. changed: !cacheExists || isDifferentDate || isDifferentSize,
  95. meta: meta
  96. };
  97. return nEntry;
  98. },
  99. /**
  100. * Return the list o the files that changed compared
  101. * against the ones stored in the cache
  102. *
  103. * @method getUpdated
  104. * @param files {Array} the array of files to compare against the ones in the cache
  105. * @returns {Array}
  106. */
  107. getUpdatedFiles: function ( files ) {
  108. var me = this;
  109. files = files || [ ];
  110. return me.normalizeEntries( files ).filter( function ( entry ) {
  111. return entry.changed;
  112. } ).map( function ( entry ) {
  113. return entry.key;
  114. } );
  115. },
  116. /**
  117. * return the list of files
  118. * @method normalizeEntries
  119. * @param files
  120. * @returns {*}
  121. */
  122. normalizeEntries: function ( files ) {
  123. files = files || [ ];
  124. var me = this;
  125. var nEntries = files.map( function ( file ) {
  126. return me.getFileDescriptor( file );
  127. } );
  128. //normalizeEntries = nEntries;
  129. return nEntries;
  130. },
  131. /**
  132. * Remove an entry from the file-entry-cache. Useful to force the file to still be considered
  133. * modified the next time the process is run
  134. *
  135. * @method removeEntry
  136. * @param entryName
  137. */
  138. removeEntry: function ( entryName ) {
  139. delete normalizedEntries[ entryName ];
  140. cache.removeKey( entryName );
  141. },
  142. /**
  143. * Delete the cache file from the disk
  144. * @method deleteCacheFile
  145. */
  146. deleteCacheFile: function () {
  147. cache.removeCacheFile();
  148. },
  149. /**
  150. * remove the cache from the file and clear the memory cache
  151. */
  152. destroy: function () {
  153. normalizedEntries = { };
  154. cache.destroy();
  155. },
  156. /**
  157. * Sync the files and persist them to the cache
  158. * @method reconcile
  159. */
  160. reconcile: function () {
  161. removeNotFoundFiles();
  162. var entries = normalizedEntries;
  163. var keys = Object.keys( entries );
  164. if ( keys.length === 0 ) {
  165. return;
  166. }
  167. keys.forEach( function ( entryName ) {
  168. var cacheEntry = entries[ entryName ];
  169. try {
  170. var stat = fs.statSync( cacheEntry.key );
  171. var meta = assign( cacheEntry.meta, {
  172. size: stat.size,
  173. mtime: stat.mtime.getTime()
  174. } );
  175. cache.setKey( entryName, meta );
  176. } catch (err) {
  177. // if the file does not exists we don't save it
  178. // other errors are just thrown
  179. if ( err.code !== 'ENOENT' ) {
  180. throw err;
  181. }
  182. }
  183. } );
  184. cache.save( true );
  185. }
  186. };
  187. }
  188. };