UNPKG

cypress-contract-stubs

Version:

Cypress plugin for Spring Contract Stubs

52 lines 1.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.archiveMapping = void 0; const promise_retry_1 = require("../utils/promise-retry"); const ls_archive_1 = require("ls-archive"); const debug_1 = require("../utils/debug"); const path_1 = require("path"); const util_1 = require("util"); /** * Map all json stub entries. * * @param archivePath A valid path for the file. * @param entry An json archive entry */ async function readJsonContent(archivePath, entry) { const fileContent = await (0, util_1.promisify)(ls_archive_1.readFile)(archivePath, entry.path); const stub = JSON.parse(fileContent.toString()); const stubPath = (0, path_1.parse)(entry.path); stub.name = stubPath.name; stub.consumer = (0, path_1.basename)(stubPath.dir); stub.id = `${stub.name}:${stub.consumer}`; try { stub.response.body = JSON.parse(stub.response.body); // eslint-disable-next-line no-empty } catch (e) { } return stub; } /** * Extract all stub entries from an archive file. * * @param archivePath A valid path for the file. * @returns A Promise for the completion of all stub entries. */ async function archiveMapping(archivePath) { const entries = []; const archiveList = () => (0, util_1.promisify)(ls_archive_1.list)(archivePath); const archiveEntries = (await (0, promise_retry_1.promiseRetry)(archiveList, 100, 10)).filter((entry) => (0, path_1.extname)(entry.path) === '.json'); for (const entry of archiveEntries) { try { const stubContent = () => readJsonContent(archivePath, entry); const stubEntry = await (0, promise_retry_1.promiseRetry)(stubContent, 100, 10); entries.push(stubEntry); } catch (e) { debug_1.logger.error(`Archive not mapped "${entry.path}" ${e}`); } } return entries; } exports.archiveMapping = archiveMapping; //# sourceMappingURL=archive-mapping.js.map