@wix-pilot/core
Version:
A flexible plugin that drives your tests with human-written commands, enhanced by the power of large language models (LLMs)
64 lines (63 loc) • 2.15 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StepPerformerCacheHandler = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
class StepPerformerCacheHandler {
cache = new Map();
temporaryCache = new Map();
cacheFilePath;
constructor(cacheFileName = "wix_pilot_cache.json") {
this.cacheFilePath = path_1.default.resolve(process.cwd(), cacheFileName);
}
loadCacheFromFile() {
try {
if (fs_1.default.existsSync(this.cacheFilePath)) {
const data = fs_1.default.readFileSync(this.cacheFilePath, "utf-8");
const json = JSON.parse(data);
this.cache = new Map(Object.entries(json));
}
else {
this.cache.clear(); // Ensure cache is empty if file doesn't exist
}
}
catch (error) {
console.warn("Error loading cache from file:", error);
this.cache.clear(); // Clear cache on error to avoid stale data
}
}
saveCacheToFile() {
try {
const json = Object.fromEntries(this.cache);
fs_1.default.writeFileSync(this.cacheFilePath, JSON.stringify(json, null, 2), {
flag: "w+",
});
}
catch (error) {
console.error("Error saving cache to file:", error);
}
}
getStepFromCache(key) {
return this.cache.get(key);
}
addToTemporaryCache(key, value) {
this.temporaryCache.set(key, [
...(this.temporaryCache.get(key) ?? []),
value,
]);
}
flushTemporaryCache() {
this.temporaryCache.forEach((value, key) => {
this.cache.set(key, value);
});
this.saveCacheToFile();
this.clearTemporaryCache();
}
clearTemporaryCache() {
this.temporaryCache.clear();
}
}
exports.StepPerformerCacheHandler = StepPerformerCacheHandler;