UNPKG

cypress-contract-stubs

Version:

Cypress plugin for Spring Contract Stubs

68 lines 3.31 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.downloadArtifact = void 0; const stubs_config_1 = require("../stubs/stubs-config"); const debug_1 = require("../utils/debug"); const nexus_url_1 = require("./nexus-url"); const file_pattern_1 = require("../utils/file-pattern"); const path_1 = require("path"); const download_1 = __importDefault(require("download")); const globby_1 = __importDefault(require("globby")); const got_1 = __importDefault(require("got")); /** * Stub item search request * * @param url * @param config */ async function stubItemSearch(url, config) { var _a, _b, _c, _d; try { const response = await (0, got_1.default)(url, { json: true, retries: 0 }); if ((_b = (_a = response.body) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.repositoryPath) { const path = response.body.data.repositoryPath; const downloadUrl = (0, nexus_url_1.nexusDownloadUrl)(path, config).toString(); return { path, downloadUrl }; } else { return (_d = (_c = response.body) === null || _c === void 0 ? void 0 : _c.items) === null || _d === void 0 ? void 0 : _d.shift(); } } catch (err) { debug_1.logger.debug('stubs:remote:error', err.message); return; } } /** * Download remote artifact * * @param config */ async function downloadArtifact(config) { const stubUrl = config.type === 'nexus' ? (0, nexus_url_1.nexusUrl)(config) : (0, nexus_url_1.nexus3Url)(config); const { artifactId } = (0, stubs_config_1.stubCoordinate)(config.id); debug_1.logger.debug('stubs:remote', `Search remote stub ${stubUrl} (${config.type || 'nexus3'})`); const stubItem = await stubItemSearch(stubUrl.toString(), config); const stubPattern = (0, file_pattern_1.filePattern)((stubItem === null || stubItem === void 0 ? void 0 : stubItem.path) ? (0, path_1.resolve)((0, file_pattern_1.workdir)(), `${stubs_config_1.configVars.cachePath}/${stubItem.path}`) : (0, path_1.resolve)((0, file_pattern_1.workdir)(), `${stubs_config_1.configVars.cachePath}/**/*${artifactId}*`)); const stubPath = (await (0, globby_1.default)(stubPattern, { objectMode: true, stats: true })) .sort((a, b) => b.stats.ctime - a.stats.ctime) // eslint-disable-line .shift(); if (stubPath) { debug_1.logger.debug('stubs:remote', `Stub available at ${stubPath.path}`); return stubPath.path; } if (stubItem === null || stubItem === void 0 ? void 0 : stubItem.downloadUrl) { debug_1.logger.debug('stubs:remote', `Download remote stub ${config.id} ${stubItem.downloadUrl}`); const downloadConfig = { directory: (0, path_1.dirname)(stubPattern), filename: (0, path_1.basename)(stubPattern), retries: 0 }; await (0, download_1.default)(stubItem.downloadUrl, downloadConfig.directory, downloadConfig); debug_1.logger.debug('stubs:remote', `Stub downloaded at ${stubPattern}`); return stubPattern; } } exports.downloadArtifact = downloadArtifact; //# sourceMappingURL=download-artifact.js.map