.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / utils / FileCache.js
1 /* @flow */
2 "use strict";
3
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");
8
9 const DEFAULT_CACHE_LOCATION = "./.stylelintcache";
10 const DEFAULT_HASH = "";
11
12 function FileCache(cacheLocation /*: ?string */, hashOfConfig /*: ?string */) {
13   const cacheFile = path.resolve(
14     getCacheFile(cacheLocation || DEFAULT_CACHE_LOCATION, process.cwd())
15   );
16   debug(`Cache file is created at ${cacheFile}`);
17   this._fileCache = fileEntryCache.create(cacheFile);
18   this._hashOfConfig = hashOfConfig || DEFAULT_HASH;
19 }
20
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 || {};
26   const changed =
27     descriptor.changed || meta.hashOfConfig !== this._hashOfConfig;
28   if (!changed) {
29     debug(`Skip linting ${absoluteFilepath}. File hasn't changed.`);
30   }
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;
35   }
36   return changed;
37 };
38
39 FileCache.prototype.reconcile = function() {
40   this._fileCache.reconcile();
41 };
42
43 FileCache.prototype.destroy = function() {
44   this._fileCache.destroy();
45 };
46
47 FileCache.prototype.removeEntry = function(absoluteFilepath) {
48   this._fileCache.removeEntry(absoluteFilepath);
49 };
50
51 module.exports = FileCache;