UNPKG

askui

Version:

Reliable, automated end-to-end-testing that depends on what is shown on your screen instead of the technology you are running on

129 lines (128 loc) 5.65 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CacheFile = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const lib_1 = require("../../lib"); const cache_entry_1 = require("./cache-entry"); class CacheFile { constructor(filePath) { this.createAt = new Date(); this.version = 1; this.type = 'AskUI-Cache'; this.data = {}; this.filePath = filePath; } loadFromFileVersion1(json) { if (json.version !== 1) { throw new Error(`Unsupported key 'version' in the cache file: '${json.version}'. Only version 1 is supported.`); } if (json.data === undefined) { throw new Error("Key 'data' is required in the cache file."); } if (json.createAt === undefined) { throw new Error("Key 'createAt' is required in the cache file."); } if (json.version === undefined) { throw new Error("Key 'version' is required in the cache file."); } if (json.type !== 'AskUI-Cache') { throw new Error(`Unsupported key 'type' in the cache file: '${json.type}'. Only 'AskUI-Cache' is supported.`); } this.createAt = new Date(json.createAt); this.version = json.version; this.type = json.type; this.data = Object.keys(json.data).reduce((acc, validationType) => { var _a, _b; const nextAcc = Object.assign({}, acc); const validationEntries = (_b = (_a = json.data) === null || _a === void 0 ? void 0 : _a[validationType]) !== null && _b !== void 0 ? _b : {}; nextAcc[validationType] = Object.keys(validationEntries).reduce((innerAcc, instruction) => { var _a; const nextInnerAcc = Object.assign({}, innerAcc); const rawEntries = (_a = validationEntries[instruction]) !== null && _a !== void 0 ? _a : []; nextInnerAcc[instruction] = rawEntries .map((entry) => cache_entry_1.CacheEntry.fromJson(entry)) .filter((entry) => entry !== undefined); return nextInnerAcc; }, {}); return nextAcc; }, {}); } loadFromFile() { if (this.filePath === undefined) { lib_1.logger.debug('The cache file path is not set. Skipping the cache load.'); return; } lib_1.logger.debug(`Loading the cache from file '${this.filePath}'.`); if (!fs_1.default.existsSync(this.filePath)) { lib_1.logger.warn(`The cache file does not exist: '${this.filePath}'. Skipping the cache load.`); return; } const data = fs_1.default.readFileSync(this.filePath, 'utf8'); const json = JSON.parse(data); if (json.version !== 1) { throw new Error(`Unsupported key 'version' in the cache file: '${json.version}'. Only version 1 is supported.`); } this.loadFromFileVersion1(json); } getDataForValidationType(validationType) { var _a; return (_a = this.data[validationType]) !== null && _a !== void 0 ? _a : {}; } getData() { return this.data; } saveToFile(validationType, relevantData) { if (this.filePath === undefined) { lib_1.logger.debug('The cache file path is not set. Skipping the cache save.'); return; } try { lib_1.logger.debug(`Saving the cache to file '${this.filePath}'.`); this.setRelevantData(validationType, relevantData); const jsonObject = this.asJsonObject(); const jsonString = JSON.stringify(jsonObject, null, 2); // create the directory if it doesn't exist if (!fs_1.default.existsSync(path_1.default.dirname(this.filePath))) { fs_1.default.mkdirSync(path_1.default.dirname(this.filePath), { recursive: true }); } fs_1.default.writeFileSync(this.filePath, jsonString, 'utf8'); lib_1.logger.info(`The cache was saved successfully to '${this.filePath}'.`); } catch (error) { lib_1.logger.error(`An error occurred while saving the cache to file '${this.filePath}': '${error}'`); throw error; } } setRelevantData(validationType, relevantData) { this.data[validationType] = relevantData; } asJsonObject() { return { createAt: this.createAt.toISOString(), data: Object.keys(this.data).reduce((acc, validationType) => { const validationData = this.data[validationType]; if (validationData === undefined) { return acc; } const nextAcc = Object.assign({}, acc); nextAcc[validationType] = Object.keys(validationData).reduce((innerAcc, instruction) => { const entries = validationData[instruction]; if (entries !== undefined) { const nextInnerAcc = Object.assign({}, innerAcc); nextInnerAcc[instruction] = entries; return nextInnerAcc; } return innerAcc; }, {}); return nextAcc; }, {}), type: this.type, version: this.version, }; } } exports.CacheFile = CacheFile;