textlint
Version:
The pluggable linting tool for natural language.
102 lines • 3.8 kB
JavaScript
// MIT © 2016 azu
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacheBacker = void 0;
const file_entry_cache_1 = __importDefault(require("file-entry-cache"));
const debug_1 = __importDefault(require("debug"));
const node_fs_1 = __importDefault(require("node:fs"));
const debug = (0, debug_1.default)("textlint:CacheBacker");
const createFileEntryCache = (cacheLocation, strategy) => {
const useCheckSum = strategy === "content";
const createCache = () => {
const fileCache = file_entry_cache_1.default.createFromFile(cacheLocation, useCheckSum);
// file-entry-cache defaults `useModifiedTime` to true, but the "content" strategy must
// detect changes by content hash only. Comparing mtime would invalidate the whole cache
// in CI because checkout changes mtime while the content stays the same.
fileCache.useModifiedTime = !useCheckSum;
return fileCache;
};
try {
return createCache();
}
catch (error) {
debug(`Failed to create fileEntryCache, cacheLocation: ${cacheLocation}`, error);
// remove old cache file and retry
try {
node_fs_1.default.unlinkSync(cacheLocation);
}
catch (error) {
debug(`Failed to remove cache file, cacheLocation: ${cacheLocation}`, error);
}
return createCache();
}
};
class CacheBacker {
constructor(config) {
this.config = config;
this.isEnabled = config.cache;
this.fileCache = createFileEntryCache(config.cacheLocation, config.cacheStrategy);
}
/**
* @param {string} filePath
* @returns {boolean}
*/
shouldExecute({ filePath }) {
if (!this.isEnabled) {
return true;
}
try {
const descriptor = this.fileCache.getFileDescriptor(filePath);
const meta = descriptor.meta || {};
// if the config is changed or file is changed, should execute return true
const isChanged = descriptor.changed || meta.data.hashOfConfig !== this.config.hash;
debug(`Skipping file since hasn't changed: ${filePath}`);
return isChanged;
}
catch (error) {
debug(`shouldExecute: Failed to read cache file: ${filePath}`, error);
return true; // if cache file version is changed, it may throw an error
}
}
didExecute({ result }) {
if (!this.isEnabled) {
return;
}
const filePath = result.filePath;
try {
const descriptor = this.fileCache.getFileDescriptor(filePath);
const meta = descriptor.meta || {};
/*
* if a file contains messages we don't want to store the file in the cache
* so we can guarantee that next execution will also operate on this file
*/
if (result.messages.length > 0) {
debug(`File has problems, skipping it: ${filePath}`);
// remove the entry from the cache
this.fileCache.removeEntry(filePath);
}
else {
// cache `config.hash`
meta.data = { hashOfConfig: this.config.hash };
}
}
catch (error) {
debug(`didExecute: Failed to read cache file: ${filePath}`, error);
}
}
/**
* destroy all cache
*/
destroyCache() {
this.fileCache.destroy();
}
afterAll() {
// persist cache
this.fileCache.reconcile();
}
}
exports.CacheBacker = CacheBacker;
//# sourceMappingURL=cache-backer.js.map