UNPKG

@broadcom/endevor-bridge-for-git-for-zowe-cli

Version:

Endevor Bridge for Git plug-in for Zowe CLI

98 lines (94 loc) 3.27 kB
'use strict'; var crypto = require('crypto'); var fs = require('fs'); var path = require('path'); var imperative = require('@zowe/imperative'); /* * Copyright (c) 2026 Broadcom. All Rights Reserved. The term * "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. * * This software and all information contained therein is * confidential and proprietary and shall not be duplicated, * used, disclosed, or disseminated in any way except as * authorized by the applicable license agreement, without the * express written permission of Broadcom. All authorized * reproductions must be marked with this language. * * EXCEPT AS SET FORTH IN THE APPLICABLE LICENSE AGREEMENT, TO * THE EXTENT PERMITTED BY APPLICABLE LAW, BROADCOM PROVIDES THIS * SOFTWARE WITHOUT WARRANTY OF ANY KIND, INCLUDING WITHOUT * LIMITATION, ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL BROADCOM * BE LIABLE TO THE END USER OR ANY THIRD PARTY FOR ANY LOSS OR * DAMAGE, DIRECT OR INDIRECT, FROM THE USE OF THIS SOFTWARE, * INCLUDING WITHOUT LIMITATION, LOST PROFITS, BUSINESS * INTERRUPTION, GOODWILL, OR LOST DATA, EVEN IF BROADCOM IS * EXPRESSLY ADVISED OF SUCH LOSS OR DAMAGE. * */ const FINGERPRINTS_FILENAME = "ebg-hook-fingerprints.json"; const REPO_FINGERPRINT_FILENAME = "ebg-hook-fingerprint"; function getZoweFingerprintsPath() { return path.join(imperative.ConfigUtils.getZoweDir(), FINGERPRINTS_FILENAME); } function getRepoFingerprintPath(repoPath) { return path.join(repoPath, ".git", REPO_FINGERPRINT_FILENAME); } function readAllFromZowe() { const path = getZoweFingerprintsPath(); if (!fs.existsSync(path)) { return {}; } try { return JSON.parse(fs.readFileSync(path, "utf8")); } catch { return {}; } } function computeHash(content) { return crypto.createHash("sha256").update(content, "utf8").digest("hex"); } function readFingerprint(serverUrl, repoPath) { const zoweHash = readAllFromZowe()[serverUrl]; if (zoweHash !== undefined) { return zoweHash; } const repoFilePath = getRepoFingerprintPath(repoPath); if (!fs.existsSync(repoFilePath)) { return undefined; } try { const content = fs.readFileSync(repoFilePath, "utf8").trim(); return content || undefined; } catch { return undefined; } } function writeFingerprint(serverUrl, hash, repoPath) { try { const dir = imperative.ConfigUtils.getZoweDir(); if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } const fp = getZoweFingerprintsPath(); const current = readAllFromZowe(); current[serverUrl] = hash; fs.writeFileSync(fp, JSON.stringify(current, null, 2), { encoding: "utf8", mode: 0o600, }); return; } catch { // if the call throws we cannot store fingerprint to neither zowe nor local fs.writeFileSync(getRepoFingerprintPath(repoPath), hash, { encoding: "utf8", mode: 0o600, }); } } exports.computeHash = computeHash; exports.readFingerprint = readFingerprint; exports.writeFingerprint = writeFingerprint;