2022-06-11 23:32:32 +09:00
|
|
|
// Inspired by LintResultsCache from ESLint
|
|
|
|
|
// https://github.com/eslint/eslint/blob/c2d0a830754b6099a3325e6d3348c3ba983a677a/lib/cli-engine/lint-result-cache.js
|
|
|
|
|
|
2022-06-19 11:28:28 +08:00
|
|
|
import stringify from "fast-json-stable-stringify";
|
2023-12-17 19:34:12 +00:00
|
|
|
import fileEntryCache from "file-entry-cache";
|
|
|
|
|
|
2022-06-19 11:28:28 +08:00
|
|
|
import { version as prettierVersion } from "../index.js";
|
|
|
|
|
import { createHash } from "./utils.js";
|
2022-06-11 23:32:32 +09:00
|
|
|
|
|
|
|
|
const optionsHashCache = new WeakMap();
|
2022-11-14 19:43:08 +08:00
|
|
|
const nodeVersion = process.version;
|
2022-06-11 23:32:32 +09:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {*} options
|
|
|
|
|
* @returns {string}
|
|
|
|
|
*/
|
|
|
|
|
function getHashOfOptions(options) {
|
|
|
|
|
if (optionsHashCache.has(options)) {
|
|
|
|
|
return optionsHashCache.get(options);
|
|
|
|
|
}
|
|
|
|
|
const hash = createHash(
|
|
|
|
|
`${prettierVersion}_${nodeVersion}_${stringify(options)}`,
|
|
|
|
|
);
|
|
|
|
|
optionsHashCache.set(options, hash);
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @typedef {{ hashOfOptions?: string }} OurMeta
|
|
|
|
|
* @typedef {import("file-entry-cache").FileDescriptor} FileDescriptor
|
|
|
|
|
*
|
|
|
|
|
* @param {import("file-entry-cache").FileDescriptor} fileDescriptor
|
|
|
|
|
* @returns {FileDescriptor["meta"] & OurMeta}
|
|
|
|
|
*/
|
|
|
|
|
function getMetadataFromFileDescriptor(fileDescriptor) {
|
|
|
|
|
return fileDescriptor.meta;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class FormatResultsCache {
|
2022-08-06 11:13:37 +08:00
|
|
|
#fileEntryCache;
|
|
|
|
|
|
2022-06-11 23:32:32 +09:00
|
|
|
/**
|
2022-10-24 23:55:36 +09:00
|
|
|
* @param {string} cacheFileLocation The path of cache file location. (default: `node_modules/.cache/prettier/.prettier-cache`)
|
2022-06-11 23:32:32 +09:00
|
|
|
* @param {string} cacheStrategy
|
|
|
|
|
*/
|
|
|
|
|
constructor(cacheFileLocation, cacheStrategy) {
|
|
|
|
|
const useChecksum = cacheStrategy === "content";
|
|
|
|
|
|
2022-08-06 11:13:37 +08:00
|
|
|
this.#fileEntryCache = fileEntryCache.create(
|
2022-06-11 23:32:32 +09:00
|
|
|
/* cacheId */ cacheFileLocation,
|
|
|
|
|
/* directory */ undefined,
|
|
|
|
|
useChecksum,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {string} filePath
|
|
|
|
|
* @param {any} options
|
|
|
|
|
*/
|
|
|
|
|
existsAvailableFormatResultsCache(filePath, options) {
|
2022-08-06 11:13:37 +08:00
|
|
|
const fileDescriptor = this.#fileEntryCache.getFileDescriptor(filePath);
|
2022-06-11 23:32:32 +09:00
|
|
|
|
2022-10-22 23:33:08 +08:00
|
|
|
/* c8 ignore next 3 */
|
2022-06-11 23:32:32 +09:00
|
|
|
if (fileDescriptor.notFound) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-24 23:55:36 +09:00
|
|
|
const hashOfOptions = getHashOfOptions(options);
|
|
|
|
|
const meta = getMetadataFromFileDescriptor(fileDescriptor);
|
|
|
|
|
const changed =
|
|
|
|
|
fileDescriptor.changed || meta.hashOfOptions !== hashOfOptions;
|
2022-06-11 23:32:32 +09:00
|
|
|
|
2022-10-24 23:55:36 +09:00
|
|
|
return !changed;
|
2022-06-11 23:32:32 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {string} filePath
|
|
|
|
|
* @param {any} options
|
|
|
|
|
*/
|
|
|
|
|
setFormatResultsCache(filePath, options) {
|
2022-08-06 11:13:37 +08:00
|
|
|
const fileDescriptor = this.#fileEntryCache.getFileDescriptor(filePath);
|
2022-06-11 23:32:32 +09:00
|
|
|
const meta = getMetadataFromFileDescriptor(fileDescriptor);
|
|
|
|
|
if (fileDescriptor && !fileDescriptor.notFound) {
|
|
|
|
|
meta.hashOfOptions = getHashOfOptions(options);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-24 23:55:36 +09:00
|
|
|
/**
|
|
|
|
|
* @param {string} filePath
|
|
|
|
|
*/
|
|
|
|
|
removeFormatResultsCache(filePath) {
|
2022-10-25 01:42:41 +09:00
|
|
|
this.#fileEntryCache.removeEntry(filePath);
|
2022-10-24 23:55:36 +09:00
|
|
|
}
|
|
|
|
|
|
2022-06-11 23:32:32 +09:00
|
|
|
reconcile() {
|
2022-08-06 11:13:37 +08:00
|
|
|
this.#fileEntryCache.reconcile();
|
2022-06-11 23:32:32 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-19 11:28:28 +08:00
|
|
|
export default FormatResultsCache;
|