Repository navigation
Expand file tree
/
Copy pathcli.ts
More file actions
112 lines (87 loc) · 4.56 KB
/
Copy pathcli.ts
File metadata and controls
112 lines (87 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import * as clipboardy from "clipboardy";
import * as opn from "open";
import { Diff2Html } from "@nullcc/diff2html";
import * as http from "./http-utils";
import * as log from "./logger";
import { Configuration, InputType, DiffyType } from "./types";
import * as utils from "./utils";
function runGitDiff(gitArgsArr: string[], ignore: string[]): string {
const baseArgs = gitArgsArr.length > 0 ? gitArgsArr.map((arg) => `"${arg}"`) : ["-M", "-C", "HEAD"];
const colorArgs = gitArgsArr.indexOf("--no-color") < 0 ? ["--no-color"] : [];
const ignoreArgs = ignore.map((file) => `":(exclude)${file}"`);
const diffCommand = `git diff ${baseArgs.join(" ")} ${colorArgs.join(" ")} ${ignoreArgs.join(" ")}`;
return utils.execute(diffCommand);
}
function prepareHTML(fileList: string, diffHTMLContents: string[], config: Configuration): string[] {
let template = utils.readFile(config.htmlWrapperTemplate);
const diff2htmlPath = path.join(path.dirname(require.resolve("@nullcc/diff2html")), "..");
const cssFilePath = path.resolve(diff2htmlPath, "dist", "diff2html.min.css");
const cssContent = utils.readFile(cssFilePath);
const jsUiFilePath = path.resolve(diff2htmlPath, "dist", "diff2html-ui.min.js");
const 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});`)
const sep = "<!--diff2html-diff-->";
const headIdx = template.indexOf(sep);
const head = template.substring(0, headIdx);
const tail = template.substring(headIdx + sep.length);
return [head, fileList, ...diffHTMLContents, tail];
}
/**
* Get unified diff input from type
* @param inputType - a string `file`, `stdin`, or `command`
* @param inputArgs - a string array
* @param ignore - a string array
*/
export async function getInput(inputType: InputType, inputArgs: string[], ignore: string[]): Promise<string> {
switch (inputType) {
case "file":
return utils.readFile(inputArgs[0]);
case "stdin":
return utils.readStdin();
default:
return runGitDiff(inputArgs, ignore);
}
}
export function getOutput(options: Diff2Html.Options, config: Configuration, input: string, coverage: any, coverageFilePatterns: string[][], workspace: string, customFileNameGeneratorFile: string, displayMode: string, strict: boolean, caseInsensitive: boolean): string | string[] {
if (config.htmlWrapperTemplate && !fs.existsSync(config.htmlWrapperTemplate)) {
throw new Error(`Template ('${config.htmlWrapperTemplate}') not found!`);
}
const diffJson = Diff2Html.getJsonFromDiff(input, options);
if (config.formatType === "html") {
const dir = config.outputDestinationFile ? path.dirname(config.outputDestinationFile) : ".";
const diffCoverageDataFilename = path.join(dir, "diff-coverage-data.json");
const diffCoverageUniCoverageFilename = path.join(dir, "diff-coverage-uni-coverage.json");
const { fileList, htmlContents, metadata } = Diff2Html.getPrettyHtml(diffJson, { ...options, inputFormat: "json" }, coverage.getCoverageData(), coverageFilePatterns, workspace, customFileNameGeneratorFile, displayMode, strict, caseInsensitive);
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}'!`);
}
export function preview(content: string, format: string): void {
const filename = `diff.${format}`;
const filePath: string = path.resolve(os.tmpdir(), filename);
utils.writeFile(filePath, content);
opn(filePath, { wait: false });
}
export async function postToDiffy(diff: string, diffyOutput: DiffyType): Promise<string> {
const response = await http.put<{ id: string }>("https://diffy.org/api/diff/", { diff: diff });
const 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 url;
}