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

122 lines (121 loc) 5.21 kB
import fs from 'fs'; import path from 'path'; import { logger } from '../../lib'; import { CacheEntry } from './cache-entry'; export 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) => CacheEntry.fromJson(entry)) .filter((entry) => entry !== undefined); return nextInnerAcc; }, {}); return nextAcc; }, {}); } loadFromFile() { if (this.filePath === undefined) { logger.debug('The cache file path is not set. Skipping the cache load.'); return; } logger.debug(`Loading the cache from file '${this.filePath}'.`); if (!fs.existsSync(this.filePath)) { logger.warn(`The cache file does not exist: '${this.filePath}'. Skipping the cache load.`); return; } const data = fs.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) { logger.debug('The cache file path is not set. Skipping the cache save.'); return; } try { 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.existsSync(path.dirname(this.filePath))) { fs.mkdirSync(path.dirname(this.filePath), { recursive: true }); } fs.writeFileSync(this.filePath, jsonString, 'utf8'); logger.info(`The cache was saved successfully to '${this.filePath}'.`); } catch (error) { 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, }; } }