forked from stylelint/stylelint
/
FileCache.js
84 lines (69 loc) · 2.35 KB
/
FileCache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict';
const debug = require('debug')('stylelint:file-cache');
const fileEntryCache = require('file-entry-cache');
const getCacheFile = require('./getCacheFile');
const path = require('path');
const CACHE_STRATEGY_METADATA = 'metadata';
const CACHE_STRATEGY_CONTENT = 'content';
const DEFAULT_CACHE_LOCATION = './.stylelintcache';
const DEFAULT_CACHE_STRATEGY = CACHE_STRATEGY_METADATA;
const DEFAULT_HASH = '';
/** @typedef {import('file-entry-cache').FileDescriptor["meta"] & { hashOfConfig?: string }} CacheMetadata */
/**
* @param {string} [cacheLocation]
* @param {string} [hashOfConfig]
* @constructor
*/
class FileCache {
constructor(
cacheLocation = DEFAULT_CACHE_LOCATION,
cacheStrategy = DEFAULT_CACHE_STRATEGY,
cwd = process.cwd(),
hashOfConfig = DEFAULT_HASH,
) {
if (![CACHE_STRATEGY_METADATA, CACHE_STRATEGY_CONTENT].includes(cacheStrategy)) {
throw new Error(
`"${cacheStrategy}" cache strategy is unsupported. Specify either "${CACHE_STRATEGY_METADATA}" or "${CACHE_STRATEGY_CONTENT}"`,
);
}
const cacheFile = path.resolve(getCacheFile(cacheLocation, cwd));
const useCheckSum = cacheStrategy === CACHE_STRATEGY_CONTENT;
debug(`Cache file is created at ${cacheFile}`);
this._fileCache = fileEntryCache.create(cacheFile, undefined, useCheckSum);
this._hashOfConfig = hashOfConfig;
}
/**
* @param {string} absoluteFilepath
* @return {boolean}
*/
hasFileChanged(absoluteFilepath) {
// Get file descriptor compares current metadata against cached
// one and stores the result to "changed" prop.w
const descriptor = this._fileCache.getFileDescriptor(absoluteFilepath);
/** @type {CacheMetadata} */
const meta = descriptor.meta || {};
const changed = descriptor.changed || meta.hashOfConfig !== this._hashOfConfig;
if (!changed) {
debug(`Skip linting ${absoluteFilepath}. File hasn't changed.`);
}
// Mutate file descriptor object and store config hash to each file.
// Running lint with different config should invalidate the cache.
if (meta.hashOfConfig !== this._hashOfConfig) {
meta.hashOfConfig = this._hashOfConfig;
}
return changed;
}
reconcile() {
this._fileCache.reconcile();
}
destroy() {
this._fileCache.destroy();
}
/**
* @param {string} absoluteFilepath
*/
removeEntry(absoluteFilepath) {
this._fileCache.removeEntry(absoluteFilepath);
}
}
module.exports = FileCache;