From 7e4dc7b890a74c627c11219b3257a59f63ef8467 Mon Sep 17 00:00:00 2001 From: Rodrigo Fernandes Date: Sat, 5 Oct 2019 18:46:10 +0100 Subject: [PATCH] fix: Allow $ in diff --- src/__tests__/utils-tests.ts | 13 +++++++++++++ src/cli.ts | 32 +++++++++++++++++++++++++------- src/utils.ts | 4 ++++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/__tests__/utils-tests.ts b/src/__tests__/utils-tests.ts index 8a46baa..1c145a4 100644 --- a/src/__tests__/utils-tests.ts +++ b/src/__tests__/utils-tests.ts @@ -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"); + }); }); diff --git a/src/cli.ts b/src/cli.ts index e206de0..a23d64c 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -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("", ``) - .replace("", ``) - .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("", 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: "", replaceValue: `` }, + { searchValue: "", replaceValue: `` }, + { + 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: "", replaceValue: diffHTMLContent } + ].reduce( + (previousValue, replacement) => + utils.replaceExactly(previousValue, replacement.searchValue, replacement.replaceValue), + template + ); } /** diff --git a/src/utils.ts b/src/utils.ts index e52c78e..f1fe235 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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); +}