textlint
Version:
The pluggable linting tool for text and markdown.
98 lines • 3.77 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_path_1 = __importDefault(require("node:path"));
const node_fs_1 = __importDefault(require("node:fs"));
const debug = (0, debug_1.default)("textlint:CacheBacker");
const createFileEntryCache = (cacheLocation) => {
const filename = node_path_1.default.basename(cacheLocation);
const cacheDir = node_path_1.default.dirname(cacheLocation);
try {
// use the metadata for cache instead of the file content
// TODO: if we want to reuse the cache in CI, we should use the file content cache and save relative path into the cache
return file_entry_cache_1.default.create(filename, cacheDir, false);
}
catch (error) {
debug(`Failed to create fileEntryCache, filename: ${filename}, cacheDir: ${cacheDir}`, error);
// remove old cache file and retry
try {
node_fs_1.default.unlinkSync(node_path_1.default.join(cacheDir, filename));
}
catch (error) {
debug(`Failed to remove cache file, filename: ${filename}, cacheDir: ${cacheDir}`, error);
}
return file_entry_cache_1.default.create(filename, cacheDir, false);
}
};
class CacheBacker {
constructor(config) {
this.config = config;
this.isEnabled = config.cache;
this.fileCache = createFileEntryCache(config.cacheLocation);
}
/**
* @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