@zowe/imperative
Version:
framework for building configurable CLIs
180 lines (175 loc) • 7.32 kB
JavaScript
"use strict";
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebDiffManager = void 0;
const fs = require("fs");
const path = require("path");
const Constants_1 = require("../../../constants/src/Constants");
const ProcessUtils_1 = require("../../../utilities/src/ProcessUtils");
const ImperativeConfig_1 = require("../../../utilities/src/ImperativeConfig");
const WebDiffGenerator_1 = require("./WebDiffGenerator");
const error_1 = require("../../../error");
const diff2html_1 = require("diff2html");
/**
* Imperative Web Differences Manager handles the opening of diffs and
* constructs the dirs and files if necessary
* @export
* @class WebDiffManager
*/
class WebDiffManager {
/**
* Return a singleton instance of this class
* @static
* @readonly
*/
static get instance() {
if (this.mInstance == null) {
this.mInstance = new WebDiffManager();
}
return this.mInstance;
}
/**
* Launch diffs page in browser.
* @memberof WebDiffManager
*/
openDiffs(patchDiff) {
return __awaiter(this, void 0, void 0, function* () {
const doWeHaveGui = ProcessUtils_1.ProcessUtils.isGuiAvailable();
if (doWeHaveGui !== ProcessUtils_1.GuiResult.GUI_AVAILABLE) {
let errMsg = "You are running in an environment with no graphical interface." +
"\nAlternatively, you can run '" + ImperativeConfig_1.ImperativeConfig.instance.findPackageBinName() +
" --help' for text-based help.";
if (doWeHaveGui === ProcessUtils_1.GuiResult.NO_GUI_NO_DISPLAY) {
errMsg += "\n\nIf you are running in an X Window environment," +
"\nensure that your DISPLAY environment variable is set." +
"\nFor example, type the following:" +
"\n echo $DISPLAY" +
"\nIf it is not set, assign a valid value. For example:" +
"\n export DISPLAY=:0.0" +
"\nThen try the --help-web option again.";
throw new error_1.ImperativeError({
msg: errMsg
});
}
return;
}
if (!fs.existsSync(this.webDiffDir))
yield new WebDiffGenerator_1.default(ImperativeConfig_1.ImperativeConfig.instance, this.webDiffDir).buildDiffDir();
const htmlDiff = (0, diff2html_1.html)(patchDiff, {
outputFormat: "side-by-side",
matching: "lines",
diffStyle: "char",
});
if (htmlDiff != null) {
// writing the diff content into a text file
fs.writeFileSync(path.join(this.webDiffDir, 'index.html'), this.genHtmlForDiffs(htmlDiff, patchDiff));
try {
ProcessUtils_1.ProcessUtils.openInDefaultApp(`file://${this.webDiffDir}/index.html`);
}
catch (e) {
throw new error_1.ImperativeError({
msg: "Failed to launch web diff, try running -h for console help instead",
causeErrors: [e]
});
}
}
});
}
/**
* Gets the directory where built copy of web diff launcher is stored
* @readonly
* @private
* @returns {string} Absolute path of directory
*/
get webDiffDir() {
return path.join(ImperativeConfig_1.ImperativeConfig.instance.cliHome, Constants_1.Constants.WEB_DIFF_DIR);
}
/**
* Returns header HTML for web diff page
* @private
* @param htmlDiff - html diffs of the file changes
* @param unifiedStringDiff - unified string of difference between two files
*/
genHtmlForDiffs(htmlDiff, unifiedStringDiff) {
return `<!DOCTYPE html>
<html>
<head>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.1/styles/github.min.css"
integrity="sha384-yaOEEinoAKxVHv1ZCY3vqJeRItlRVwZ9pyTCCJLHlyHNndGZIF+S30C1+8oRQ2sz"
crossorigin="anonymous"
/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/diff2html@3.4.17/bundles/css/diff2html.min.css"
integrity="sha384-SqVaGvqd1A6pQfywL1yrJwftrR6C959ImMNnuqO5DbCpiRI4OepQ9eGhnFlj02Sw"
crossorigin="anonymous"
/>
<script
src="https://cdn.jsdelivr.net/npm/diff2html@3.4.17/bundles/js/diff2html-ui.min.js"
integrity="sha384-TIP1pakMMiVbwLawU7P/eb6fvLfX981YfIqvX9kc7EPlXcmDH8swvWieaA5kfv/q"
crossorigin="anonymous">
</script>
<script>
const fr = new FileReader()
fr.onload(()=>{
document.getElementById('diffOutput').textContent = fr.result
})
fr.readAsText()
const diffString = ${unifiedStringDiff}
document.addEventListener('DOMContentLoaded', function () {
var targetElement = document.getElementsByClassName('d2h-file-list-wrapper')[0];
var configuration = {
drawFileList: true,
fileListToggle: false,
fileListStartVisible: false,
fileContentToggle: false,
matching: 'none',
outputFormat: 'side-by-side',
synchronisedScroll: true,
highlight: true,
renderNothingWhenEmpty: false,
};
var diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration);
diff2htmlUi.draw();
diff2htmlUi.highlightCode();
});
</script>
<meta content="0; url=diff.html?p=" />
</head>
<body>
${htmlDiff}
</body>
</html>
`;
}
}
exports.WebDiffManager = WebDiffManager;
/**
* Singleton instance of this class
* @private
* @static
* @type {WebDiffManager}
* @memberof WebDiffManager
*/
WebDiffManager.mInstance = null;
//# sourceMappingURL=WebDiffManager.js.map