Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

288 строки
7.6 KiB

4 лет назад
  1. var path = require('path');
  2. var crypto = require('crypto');
  3. module.exports = {
  4. createFromFile: function (filePath, useChecksum) {
  5. var fname = path.basename(filePath);
  6. var dir = path.dirname(filePath);
  7. return this.create(fname, dir, useChecksum);
  8. },
  9. create: function (cacheId, _path, useChecksum) {
  10. var fs = require('fs');
  11. var flatCache = require('flat-cache');
  12. var cache = flatCache.load(cacheId, _path);
  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. * Given a buffer, calculate md5 hash of its content.
  36. * @method getHash
  37. * @param {Buffer} buffer buffer to calculate hash on
  38. * @return {String} content hash digest
  39. */
  40. getHash: function (buffer) {
  41. return crypto.createHash('md5').update(buffer).digest('hex');
  42. },
  43. /**
  44. * Return whether or not a file has changed since last time reconcile was called.
  45. * @method hasFileChanged
  46. * @param {String} file the filepath to check
  47. * @return {Boolean} wheter or not the file has changed
  48. */
  49. hasFileChanged: function (file) {
  50. return this.getFileDescriptor(file).changed;
  51. },
  52. /**
  53. * given an array of file paths it return and object with three arrays:
  54. * - changedFiles: Files that changed since previous run
  55. * - notChangedFiles: Files that haven't change
  56. * - notFoundFiles: Files that were not found, probably deleted
  57. *
  58. * @param {Array} files the files to analyze and compare to the previous seen files
  59. * @return {[type]} [description]
  60. */
  61. analyzeFiles: function (files) {
  62. var me = this;
  63. files = files || [];
  64. var res = {
  65. changedFiles: [],
  66. notFoundFiles: [],
  67. notChangedFiles: [],
  68. };
  69. me.normalizeEntries(files).forEach(function (entry) {
  70. if (entry.changed) {
  71. res.changedFiles.push(entry.key);
  72. return;
  73. }
  74. if (entry.notFound) {
  75. res.notFoundFiles.push(entry.key);
  76. return;
  77. }
  78. res.notChangedFiles.push(entry.key);
  79. });
  80. return res;
  81. },
  82. getFileDescriptor: function (file) {
  83. var fstat;
  84. try {
  85. fstat = fs.statSync(file);
  86. } catch (ex) {
  87. this.removeEntry(file);
  88. return { key: file, notFound: true, err: ex };
  89. }
  90. if (useChecksum) {
  91. return this._getFileDescriptorUsingChecksum(file);
  92. }
  93. return this._getFileDescriptorUsingMtimeAndSize(file, fstat);
  94. },
  95. _getFileDescriptorUsingMtimeAndSize: function (file, fstat) {
  96. var meta = cache.getKey(file);
  97. var cacheExists = !!meta;
  98. var cSize = fstat.size;
  99. var cTime = fstat.mtime.getTime();
  100. var isDifferentDate;
  101. var isDifferentSize;
  102. if (!meta) {
  103. meta = { size: cSize, mtime: cTime };
  104. } else {
  105. isDifferentDate = cTime !== meta.mtime;
  106. isDifferentSize = cSize !== meta.size;
  107. }
  108. var nEntry = (normalizedEntries[file] = {
  109. key: file,
  110. changed: !cacheExists || isDifferentDate || isDifferentSize,
  111. meta: meta,
  112. });
  113. return nEntry;
  114. },
  115. _getFileDescriptorUsingChecksum: function (file) {
  116. var meta = cache.getKey(file);
  117. var cacheExists = !!meta;
  118. var contentBuffer;
  119. try {
  120. contentBuffer = fs.readFileSync(file);
  121. } catch (ex) {
  122. contentBuffer = '';
  123. }
  124. var isDifferent = true;
  125. var hash = this.getHash(contentBuffer);
  126. if (!meta) {
  127. meta = { hash: hash };
  128. } else {
  129. isDifferent = hash !== meta.hash;
  130. }
  131. var nEntry = (normalizedEntries[file] = {
  132. key: file,
  133. changed: !cacheExists || isDifferent,
  134. meta: meta,
  135. });
  136. return nEntry;
  137. },
  138. /**
  139. * Return the list o the files that changed compared
  140. * against the ones stored in the cache
  141. *
  142. * @method getUpdated
  143. * @param files {Array} the array of files to compare against the ones in the cache
  144. * @returns {Array}
  145. */
  146. getUpdatedFiles: function (files) {
  147. var me = this;
  148. files = files || [];
  149. return me
  150. .normalizeEntries(files)
  151. .filter(function (entry) {
  152. return entry.changed;
  153. })
  154. .map(function (entry) {
  155. return entry.key;
  156. });
  157. },
  158. /**
  159. * return the list of files
  160. * @method normalizeEntries
  161. * @param files
  162. * @returns {*}
  163. */
  164. normalizeEntries: function (files) {
  165. files = files || [];
  166. var me = this;
  167. var nEntries = files.map(function (file) {
  168. return me.getFileDescriptor(file);
  169. });
  170. //normalizeEntries = nEntries;
  171. return nEntries;
  172. },
  173. /**
  174. * Remove an entry from the file-entry-cache. Useful to force the file to still be considered
  175. * modified the next time the process is run
  176. *
  177. * @method removeEntry
  178. * @param entryName
  179. */
  180. removeEntry: function (entryName) {
  181. delete normalizedEntries[entryName];
  182. cache.removeKey(entryName);
  183. },
  184. /**
  185. * Delete the cache file from the disk
  186. * @method deleteCacheFile
  187. */
  188. deleteCacheFile: function () {
  189. cache.removeCacheFile();
  190. },
  191. /**
  192. * remove the cache from the file and clear the memory cache
  193. */
  194. destroy: function () {
  195. normalizedEntries = {};
  196. cache.destroy();
  197. },
  198. _getMetaForFileUsingCheckSum: function (cacheEntry) {
  199. var contentBuffer = fs.readFileSync(cacheEntry.key);
  200. var hash = this.getHash(contentBuffer);
  201. var meta = Object.assign(cacheEntry.meta, { hash: hash });
  202. return meta;
  203. },
  204. _getMetaForFileUsingMtimeAndSize: function (cacheEntry) {
  205. var stat = fs.statSync(cacheEntry.key);
  206. var meta = Object.assign(cacheEntry.meta, {
  207. size: stat.size,
  208. mtime: stat.mtime.getTime(),
  209. });
  210. return meta;
  211. },
  212. /**
  213. * Sync the files and persist them to the cache
  214. * @method reconcile
  215. */
  216. reconcile: function (noPrune) {
  217. removeNotFoundFiles();
  218. noPrune = typeof noPrune === 'undefined' ? true : noPrune;
  219. var entries = normalizedEntries;
  220. var keys = Object.keys(entries);
  221. if (keys.length === 0) {
  222. return;
  223. }
  224. var me = this;
  225. keys.forEach(function (entryName) {
  226. var cacheEntry = entries[entryName];
  227. try {
  228. var meta = useChecksum
  229. ? me._getMetaForFileUsingCheckSum(cacheEntry)
  230. : me._getMetaForFileUsingMtimeAndSize(cacheEntry);
  231. cache.setKey(entryName, meta);
  232. } catch (err) {
  233. // if the file does not exists we don't save it
  234. // other errors are just thrown
  235. if (err.code !== 'ENOENT') {
  236. throw err;
  237. }
  238. }
  239. });
  240. cache.save(noPrune);
  241. },
  242. };
  243. },
  244. };