@nullcc/diff2html-cli
Version:
Fast Diff to colorized HTML
157 lines • 8.44 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
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());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var os = require("os");
var path = require("path");
var clipboardy = require("clipboardy");
var opn = require("open");
var diff2html_1 = require("@nullcc/diff2html");
var http = require("./http-utils");
var log = require("./logger");
var utils = require("./utils");
function runGitDiff(gitArgsArr, ignore) {
var baseArgs = gitArgsArr.length > 0 ? gitArgsArr.map(function (arg) { return "\"" + arg + "\""; }) : ["-M", "-C", "HEAD"];
var colorArgs = gitArgsArr.indexOf("--no-color") < 0 ? ["--no-color"] : [];
var ignoreArgs = ignore.map(function (file) { return "\":(exclude)" + file + "\""; });
var diffCommand = "git diff " + baseArgs.join(" ") + " " + colorArgs.join(" ") + " " + ignoreArgs.join(" ");
return utils.execute(diffCommand);
}
function prepareHTML(fileList, diffHTMLContents, config) {
var template = utils.readFile(config.htmlWrapperTemplate);
var diff2htmlPath = path.join(path.dirname(require.resolve("@nullcc/diff2html")), "..");
var cssFilePath = path.resolve(diff2htmlPath, "dist", "diff2html.min.css");
var cssContent = utils.readFile(cssFilePath);
var jsUiFilePath = path.resolve(diff2htmlPath, "dist", "diff2html-ui.min.js");
var jsUiContent = utils.readFile(jsUiFilePath);
template = template
.replace("<!--diff2html-css-->", "<style>\n" + cssContent + "\n</style>")
.replace("<!--diff2html-js-ui-->", "<script>\n" + jsUiContent + "\n</script>")
.replace("//diff2html-fileListCloseable", "diff2htmlUi.fileListCloseable(\"#diff\", " + config.showFilesOpen + ");")
.replace("//diff2html-synchronisedScroll", "diff2htmlUi.synchronisedScroll(\"#diff\", " + config.synchronisedScroll + ");");
var sep = "<!--diff2html-diff-->";
var headIdx = template.indexOf(sep);
var head = template.substring(0, headIdx);
var tail = template.substring(headIdx + sep.length);
return __spreadArrays([head, fileList], diffHTMLContents, [tail]);
}
function getInput(inputType, inputArgs, ignore) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (inputType) {
case "file":
return [2, utils.readFile(inputArgs[0])];
case "stdin":
return [2, utils.readStdin()];
default:
return [2, runGitDiff(inputArgs, ignore)];
}
return [2];
});
});
}
exports.getInput = getInput;
function getOutput(options, config, input, coverage, coverageFilePatterns, workspace, customFileNameGeneratorFile, displayMode, strict, caseInsensitive) {
if (config.htmlWrapperTemplate && !fs.existsSync(config.htmlWrapperTemplate)) {
throw new Error("Template ('" + config.htmlWrapperTemplate + "') not found!");
}
var diffJson = diff2html_1.Diff2Html.getJsonFromDiff(input, options);
if (config.formatType === "html") {
var dir = config.outputDestinationFile ? path.dirname(config.outputDestinationFile) : ".";
var diffCoverageDataFilename = path.join(dir, "diff-coverage-data.json");
var diffCoverageUniCoverageFilename = path.join(dir, "diff-coverage-uni-coverage.json");
var _a = diff2html_1.Diff2Html.getPrettyHtml(diffJson, __assign(__assign({}, options), { inputFormat: "json" }), coverage.getCoverageData(), coverageFilePatterns, workspace, customFileNameGeneratorFile, displayMode, strict, caseInsensitive), fileList = _a.fileList, htmlContents = _a.htmlContents, metadata = _a.metadata;
utils.writeFile(diffCoverageDataFilename, JSON.stringify(metadata, null, 2));
utils.writeFile(diffCoverageUniCoverageFilename, JSON.stringify(coverage.getCoverageData(), null, 2));
return prepareHTML(fileList, htmlContents, config);
}
else if (config.formatType === "json") {
return JSON.stringify(diffJson);
}
throw new Error("Wrong output format '" + config.formatType + "'!");
}
exports.getOutput = getOutput;
function preview(content, format) {
var filename = "diff." + format;
var filePath = path.resolve(os.tmpdir(), filename);
utils.writeFile(filePath, content);
opn(filePath, { wait: false });
}
exports.preview = preview;
function postToDiffy(diff, diffyOutput) {
return __awaiter(this, void 0, void 0, function () {
var response, url;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, http.put("https://diffy.org/api/diff/", { diff: diff })];
case 1:
response = _a.sent();
url = "https://diffy.org/diff/" + response.id;
log.print("Link powered by https://diffy.org");
log.print(url);
if (diffyOutput === "browser") {
opn(url);
}
else if (diffyOutput === "pbcopy") {
clipboardy.writeSync(url);
}
return [2, url];
}
});
});
}
exports.postToDiffy = postToDiffy;
//# sourceMappingURL=cli.js.map