Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/__tests__/utils-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,17 @@ describe("utils", () => {

expect(stringToCompare).toBe(contentRead);
});

test("should execute command in shell", () => {
const echoedValue = "echoed string";
const result = utils.execute(`echo "${echoedValue}"`);

expect(result).toBe(`${echoedValue}\n`);
});

test("should replace exactly string", () => {
const result = utils.replaceExactly("my long and nice text", "long", "$&beautiful");

expect(result).toBe("my $&beautiful and nice text");
});
});
32 changes: 25 additions & 7 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,31 @@ function prepareHTML(diffHTMLContent: string, config: Configuration): string {
const jsUiFilePath = path.resolve(diff2htmlPath, "dist", "diff2html-ui.min.js");
const jsUiContent = utils.readFile(jsUiFilePath);

return 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});`)
.replace("//diff2html-highlightCode", config.highlightCode ? `diff2htmlUi.highlightCode("#diff");` : "")
.replace("<!--diff2html-diff-->", diffHTMLContent);
/* HACK:
* Replace needs to receive a function as the second argument to perform an exact replacement.
* This will avoid the replacements from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter
*/
return [
{ searchValue: "<!--diff2html-css-->", replaceValue: `<style>\n${cssContent}\n</style>` },
{ searchValue: "<!--diff2html-js-ui-->", replaceValue: `<script>\n${jsUiContent}\n</script>` },
{
searchValue: "//diff2html-fileListCloseable",
replaceValue: `diff2htmlUi.fileListCloseable("#diff", ${config.showFilesOpen});`
},
{
searchValue: "//diff2html-synchronisedScroll",
replaceValue: `diff2htmlUi.synchronisedScroll("#diff", ${config.synchronisedScroll});`
},
{
searchValue: "//diff2html-highlightCode",
replaceValue: config.highlightCode ? `diff2htmlUi.highlightCode("#diff");` : ""
},
{ searchValue: "<!--diff2html-diff-->", replaceValue: diffHTMLContent }
].reduce(
(previousValue, replacement) =>
utils.replaceExactly(previousValue, replacement.searchValue, replacement.replaceValue),
template
);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ export function writeFile(filePath: string, content: string): void {
export function execute(cmd: string): string {
return childProcess.execSync(cmd).toString("utf8");
}

export function replaceExactly(value: string, searchValue: string, replaceValue: string): string {
return value.replace(searchValue, () => replaceValue);
}