4 const debug = require("debug")("stylelint:file-cache");
5 const fileEntryCache = require("file-entry-cache");
6 const getCacheFile = require("./getCacheFile");
7 const path = require("path");
9 const DEFAULT_CACHE_LOCATION = "./.stylelintcache";
10 const DEFAULT_HASH = "";
12 function FileCache(cacheLocation /*: ?string */, hashOfConfig /*: ?string */) {
13 const cacheFile = path.resolve(
14 getCacheFile(cacheLocation || DEFAULT_CACHE_LOCATION, process.cwd())
16 debug(`Cache file is created at ${cacheFile}`);
17 this._fileCache = fileEntryCache.create(cacheFile);
18 this._hashOfConfig = hashOfConfig || DEFAULT_HASH;
21 FileCache.prototype.hasFileChanged = function(absoluteFilepath) {
22 // Get file descriptor compares current metadata against cached
23 // one and stores the result to "changed" prop.w
24 const descriptor = this._fileCache.getFileDescriptor(absoluteFilepath);
25 const meta = descriptor.meta || {};
27 descriptor.changed || meta.hashOfConfig !== this._hashOfConfig;
29 debug(`Skip linting ${absoluteFilepath}. File hasn't changed.`);
31 // Mutate file descriptor object and store config hash to each file.
32 // Running lint with different config should invalidate the cache.
33 if (meta.hashOfConfig !== this._hashOfConfig) {
34 meta.hashOfConfig = this._hashOfConfig;
39 FileCache.prototype.reconcile = function() {
40 this._fileCache.reconcile();
43 FileCache.prototype.destroy = function() {
44 this._fileCache.destroy();
47 FileCache.prototype.removeEntry = function(absoluteFilepath) {
48 this._fileCache.removeEntry(absoluteFilepath);
51 module.exports = FileCache;