@zowe/imperative
Version:
framework for building configurable CLIs
99 lines • 4.28 kB
JavaScript
/*
* 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.DiffUtils = void 0;
/* eslint-disable no-console */
const jest_diff_1 = require("jest-diff");
const diff_1 = require("diff");
const TextUtils_1 = require("../TextUtils");
const diff2html_1 = require("diff2html");
const WebDiffManager_1 = require("./WebDiffManager");
/**
* Utilities to get the diff strings and open the diff strings in terminal and in browser
* @export
* @class DiffUtils
*/
class DiffUtils {
/**
* Get the difference between two string in the form of html, unifiedString and terminal output depending upon the
* options passed into the functions
* @param {string} string1
* @param {string} string2
* @param {IDiffOptions} options
* @returns {Promise<string>}
*/
static getDiffString(string1, string2, options) {
return __awaiter(this, void 0, void 0, function* () {
if (options.outputFormat === 'terminal') {
let expandflag = true;
if (options.contextLinesArg >= 0) {
expandflag = false;
}
return (0, jest_diff_1.diff)(string1, string2, {
aAnnotation: "Removed",
bAnnotation: "Added",
aColor: TextUtils_1.TextUtils.chalk.red,
bColor: TextUtils_1.TextUtils.chalk.green,
contextLines: options.contextLinesArg,
expand: expandflag
});
}
if (options.outputFormat === 'unifiedstring' || options.outputFormat === "html") {
let patchDiff;
if (options.name1 && options.name2) {
patchDiff = (0, diff_1.createTwoFilesPatch)(options.name1, options.name2, string1, string2);
}
else {
patchDiff = (0, diff_1.createTwoFilesPatch)('file-a', 'file-b', string1, string2);
}
if (options.outputFormat === 'html') {
return (0, diff2html_1.html)(patchDiff, {
outputFormat: "side-by-side",
matching: "lines",
diffStyle: "char",
});
}
return patchDiff;
}
});
}
/**
* Get the difference between two string in browser
* @param {string} string1
* @param {string} string2
* @param {IDiffOptions} options
* @return {Promise<void>}
*/
static openDiffInbrowser(string1, string2, options) {
return __awaiter(this, void 0, void 0, function* () {
let patchDiff;
if ((options === null || options === void 0 ? void 0 : options.name1) && (options === null || options === void 0 ? void 0 : options.name2)) {
patchDiff = (0, diff_1.createTwoFilesPatch)(options.name1, options.name2, string1, string2);
}
else {
patchDiff = (0, diff_1.createTwoFilesPatch)('file-a', 'file-b', string1, string2);
}
yield WebDiffManager_1.WebDiffManager.instance.openDiffs(patchDiff);
});
}
}
exports.DiffUtils = DiffUtils;
//# sourceMappingURL=DiffUtils.js.map
;