FileCache.cjs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // NOTICE: This file is generated by Rollup. To modify it,
  2. // please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
  3. 'use strict';
  4. const node_fs = require('node:fs');
  5. const process = require('node:process');
  6. const node_path = require('node:path');
  7. const createDebug = require('debug');
  8. const fileEntryCache = require('file-entry-cache');
  9. const constants = require('../constants.cjs');
  10. const hash = require('./hash.cjs');
  11. const resolveFilePath = require('./resolveFilePath.cjs');
  12. var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
  13. const debug = createDebug('stylelint:file-cache');
  14. const pkg = JSON.parse(node_fs.readFileSync(new URL('../../package.json', (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('lib/utils/FileCache.cjs', document.baseURI).href))), 'utf8'));
  15. class FileCache {
  16. constructor(
  17. cacheLocation = constants.DEFAULT_CACHE_LOCATION,
  18. cacheStrategy = constants.DEFAULT_CACHE_STRATEGY,
  19. cwd = process.cwd(),
  20. ) {
  21. if (![constants.CACHE_STRATEGY_METADATA, constants.CACHE_STRATEGY_CONTENT].includes(cacheStrategy)) {
  22. throw new Error(
  23. `"${cacheStrategy}" cache strategy is unsupported. Specify either "${constants.CACHE_STRATEGY_METADATA}" or "${constants.CACHE_STRATEGY_CONTENT}"`,
  24. );
  25. }
  26. const cacheFile = node_path.resolve(resolveFilePath(cacheLocation, cwd, `.stylelintcache_${hash(cwd)}`));
  27. const useCheckSum = cacheStrategy === constants.CACHE_STRATEGY_CONTENT;
  28. debug(`Cache file is created at ${cacheFile}`);
  29. try {
  30. this._fileCache = fileEntryCache.createFromFile(cacheFile, useCheckSum, undefined);
  31. } catch {
  32. debug(`Cache file might be corrupt, attempting to remove and recreate the cache file`);
  33. node_fs.rmSync(cacheFile, { force: true });
  34. this._fileCache = fileEntryCache.createFromFile(cacheFile, useCheckSum, undefined);
  35. }
  36. this._hashOfConfig = '';
  37. this._useCheckSum = useCheckSum;
  38. }
  39. /**
  40. * @param {import('stylelint').Config} config
  41. */
  42. calcHashOfConfig(config) {
  43. if (this._hashOfConfig) return;
  44. const stylelintVersion = pkg.version;
  45. const configString = JSON.stringify(config || {});
  46. this._hashOfConfig = hash(`${stylelintVersion}_${configString}`);
  47. }
  48. /**
  49. * @param {string} absoluteFilepath
  50. * @returns {boolean}
  51. */
  52. hasFileChanged(absoluteFilepath) {
  53. // Get file descriptor compares current metadata against cached
  54. // one and stores the result to "changed" prop.w
  55. /** @type {import('file-entry-cache').FileDescriptorMeta | undefined} */
  56. const metaCache = this._fileCache.cache.getKey(this._fileCache.createFileKey(absoluteFilepath));
  57. const descriptor = this._fileCache.getFileDescriptor(absoluteFilepath);
  58. /** @type {{ hashOfConfig?: string; }} */
  59. const metadata = (descriptor.meta.data ??= {});
  60. const configChanged = metadata.hashOfConfig !== this._hashOfConfig;
  61. let changed;
  62. if (this._useCheckSum) {
  63. changed = configChanged || !metaCache?.hash || metaCache.hash !== descriptor.meta.hash;
  64. } else {
  65. changed = configChanged || Boolean(descriptor.changed);
  66. }
  67. if (!changed) {
  68. debug(`Skip linting ${absoluteFilepath}. File hasn't changed.`);
  69. }
  70. // Mutate file descriptor object and store config hash to each file.
  71. // Running lint with different config should invalidate the cache.
  72. if (metadata.hashOfConfig !== this._hashOfConfig) {
  73. metadata.hashOfConfig = this._hashOfConfig;
  74. }
  75. return changed;
  76. }
  77. reconcile() {
  78. this._fileCache.reconcile();
  79. }
  80. destroy() {
  81. this._fileCache.destroy();
  82. }
  83. /**
  84. * @param {string} absoluteFilepath
  85. */
  86. removeEntry(absoluteFilepath) {
  87. this._fileCache.removeEntry(absoluteFilepath);
  88. }
  89. }
  90. module.exports = FileCache;