UNPKG

@sap/cli-core

Version:

Command-Line Interface (CLI) Core Module

143 lines (142 loc) 5.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.checkVersion = exports.compareEtags = exports.init = exports.clear = exports.addMetadata = exports.getMetadata = exports.getPathToDiscoveryDocument = void 0; const path_1 = __importDefault(require("path")); const fs_extra_1 = __importDefault(require("fs-extra")); const cache_1 = require("../cache"); const utils_1 = require("./utils"); const utils_2 = require("../utils/utils"); const logger_1 = require("../logger"); const config_1 = require("../config"); const constants_1 = require("../constants"); const core_1 = require("../config/core"); const getLogger = () => (0, logger_1.get)("discovery"); let initialized = false; let document; let metadata; function getHash(tenant) { return (0, utils_2.sha256)(`${tenant}${JSON.stringify((0, core_1.getDiscoveryPaths)())}`).replace(/[/\\]/g, "_"); } const getDocumentName = () => { const { trace } = getLogger(); const config = (0, config_1.get)(); const hash = getHash(config.publicfqdn); const name = `${constants_1.DISCOVERY_DOCUMENT_PREFIX}${hash}.json`; trace(`calculating document name for host ${config.host}, name ${name}`); return name; }; const getPathToDiscoveryDocument = () => (0, cache_1.getPath)(getDocumentName()); exports.getPathToDiscoveryDocument = getPathToDiscoveryDocument; const initMetadata = async () => { const { error } = getLogger(); if (!metadata) { try { metadata = JSON.parse(await (0, cache_1.readFile)(constants_1.DISCOVERY_METADATA_PATH)); } catch (err) { error("error while reading discovery metadata", err.stack); metadata = []; } } }; const getMetadata = async () => { await initMetadata(); return metadata; }; exports.getMetadata = getMetadata; const addMetadata = async ({ tenant, addedAt, }) => { await initMetadata(); metadata = metadata.filter((t) => t.tenant !== tenant); const hash = getHash(tenant); metadata.push({ tenant, addedAt, hash }); await (0, cache_1.writeFile)(constants_1.DISCOVERY_METADATA_PATH, JSON.stringify(metadata)); }; exports.addMetadata = addMetadata; const clear = () => { initialized = false; document = undefined; metadata = undefined; }; exports.clear = clear; /* jscpd:ignore-start */ const init = async () => { const { trace, error, debug } = getLogger(); if (initialized) { return document; } initialized = true; /* jscpd:ignore-end */ let schema; try { trace("reading discovery document"); const file = await (0, cache_1.readFile)(getDocumentName()); document = JSON.parse(file); } catch (err) { error("error while reading discovery document", err.stack); throw err; } try { trace("reading schema document"); try { const file = await fs_extra_1.default.readFile(path_1.default.join(__dirname, "..", "..", "schemas", "discovery.json"), "utf-8"); schema = JSON.parse(file); } catch (err) { debug("failed reading schema, trying again", err.stack); // path changes after build const file = await fs_extra_1.default.readFile(path_1.default.join(__dirname, "..", "schemas", "discovery.json"), "utf-8"); schema = JSON.parse(file); } } catch (err) { error("error while reading schema document", err.stack); throw err; } debug("validating discovery document against schema"); const result = await (0, utils_1.validate)(schema, document); if (result.result === "INVALID") { error("discovery document contains invalid data", result.errors); throw new Error("discovery document contains invalid data"); } return document; }; exports.init = init; const compareEtags = async () => { const { debug, error } = getLogger(); try { const doc = await (0, exports.init)(); const config = (0, config_1.get)(); if (config.etag && doc) { const etag = (0, utils_2.sha256)(JSON.stringify(doc)); if (config.etag === etag) { debug("etags match: %s", config.etag); return true; } debug("etags do not match. server: %s, client: %s", config.etag, etag); return false; } debug("etag is not available"); } catch (err) { error("failed to compare etags", err.stack); } return true; }; exports.compareEtags = compareEtags; const checkVersion = async () => { const { debug } = getLogger(); const doc = await (0, exports.init)(); const local = (0, utils_2.parseVersion)((0, core_1.getVersion)()); const server = (0, utils_2.parseVersion)(doc.info["x-document-version"]); if (local.major < server.major || (local.major === server.major && local.minor < server.minor)) { debug(`CLI is outdated (local: ${JSON.stringify(local)}, server: ${JSON.stringify(server)})`); return { status: "OUTDATED" }; } return { status: "UPTODATE" }; }; exports.checkVersion = checkVersion;