UNPKG

testplane

Version:

Tests framework based on mocha and wdio

80 lines 3.77 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getHashWriter = exports.HashWriter = void 0; const lodash_1 = require("lodash"); const node_path_1 = __importDefault(require("node:path")); const hash_provider_1 = require("./hash-provider"); const utils_1 = require("./utils"); const json_utils_1 = require("./json-utils"); class HashWriter { constructor(testDependenciesPath, compression) { this._hashProvider = new hash_provider_1.HashProvider(); // "Promise<string>" - file/module hash, "Promise<Error>" - calculating hash error this._stagedFileHashes = new Map(); this._stagedModuleHashes = new Map(); this._stagedPatternHashes = new Map(); this._selectivityHashesPath = (0, utils_1.getSelectivityHashesPath)(testDependenciesPath); this._compresion = compression; } _addFileDependency(filePath) { if (this._stagedFileHashes.has(filePath)) { return; } const value = this._hashProvider.calculateForFile(filePath).catch(err => err); this._stagedFileHashes.set(filePath, value); } _addPatternDependency(pattern) { if (this._stagedPatternHashes.has(pattern)) { return; } const value = this._hashProvider.calculateForPattern(pattern).catch(err => err); this._stagedPatternHashes.set(pattern, value); } _addModuleDependency(modulePath) { if (this._stagedModuleHashes.has(modulePath)) { return; } const filePath = node_path_1.default.join(modulePath, "package.json"); const value = this._hashProvider.calculateForFile(filePath).catch(err => err); this._stagedModuleHashes.set(modulePath, value); } addPatternDependencyHash(dependencyPattern) { return this._addPatternDependency(dependencyPattern); } addTestDependencyHashes(dependencies) { dependencies.css?.forEach(dependency => this._addFileDependency(dependency)); dependencies.js?.forEach(dependency => this._addFileDependency(dependency)); dependencies.png?.forEach(dependency => this._addFileDependency(dependency)); dependencies.modules?.forEach(dependency => this._addModuleDependency(dependency)); } async save() { const hasStaged = Boolean(this._stagedFileHashes.size || this._stagedModuleHashes.size || this._stagedPatternHashes.size); if (!hasStaged) { return; } const writeTo = async (src, dest) => { const keys = Array.from(src.keys()); for (const key of keys) { const hash = await src.get(key); if (hash instanceof Error) { throw hash; } dest[key] = hash; } (0, utils_1.shallowSortObject)(dest); }; const fileContents = await (0, utils_1.readHashFileContents)(this._selectivityHashesPath, this._compresion); await writeTo(this._stagedFileHashes, fileContents.files); await writeTo(this._stagedModuleHashes, fileContents.modules); await writeTo(this._stagedPatternHashes, fileContents.patterns); await (0, json_utils_1.writeJsonWithCompression)(this._selectivityHashesPath, fileContents, this._compresion); } } exports.HashWriter = HashWriter; exports.getHashWriter = (0, lodash_1.memoize)((testDependenciesPath, compression) => { return new HashWriter(testDependenciesPath, compression); }, (testDependenciesPath, compression) => `${testDependenciesPath}#${compression}`); //# sourceMappingURL=hash-writer.js.map