-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use git diff instead of diff package. Too much dependencies for a sim…
…ple task.
- Loading branch information
Showing
5 changed files
with
95 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
import diffStrings from './diff_strings'; | ||
|
||
describe('diffStrings', () => { | ||
it('should return the diff between two strings', async () => { | ||
const string1 = 'Hello, world!'; | ||
const string2 = 'Hello, brave!'; | ||
const displayFileName1 = 'file1.txt'; | ||
const displayFileName2 = 'file2.txt'; | ||
|
||
const result = await diffStrings( | ||
string1, | ||
string2, | ||
displayFileName1, | ||
displayFileName2, | ||
); | ||
|
||
expect(result).toContain('-Hello, world!'); | ||
expect(result).toContain('+Hello, brave!'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
import { exec } from 'child_process'; | ||
import fs from 'fs/promises'; | ||
import os from 'os'; | ||
import path from 'path'; | ||
|
||
export default async function diffStrings( | ||
string1: string, | ||
string2: string, | ||
displayFileName1: string, | ||
displayFileName2: string, | ||
): Promise<string> { | ||
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'diffString-')); | ||
|
||
const tmpFile1 = path | ||
.join(tmpDir, `file1-${Date.now()}.txt`) | ||
.replaceAll('\\', '/'); | ||
const tmpFile2 = path | ||
.join(tmpDir, `file2-${Date.now()}.txt`) | ||
.replaceAll('\\', '/'); | ||
|
||
// Write strings to temporary files. | ||
await fs.writeFile(tmpFile1, string1); | ||
await fs.writeFile(tmpFile2, string2); | ||
|
||
return await new Promise<string>((resolve, reject) => { | ||
// Use git diff to generate readable diff. | ||
exec( | ||
`git diff --no-index --src-prefix= --dst-prefix= -- ${tmpFile1} ${tmpFile2}`, | ||
{ encoding: 'utf8' }, | ||
(error, stdout, stderr) => { | ||
if (error !== null && error.code !== 1) { | ||
// git diff returns 1 if there are differences | ||
reject(new Error(stderr)); | ||
return; | ||
} | ||
|
||
const result = stdout | ||
.replaceAll(tmpFile1, displayFileName1) | ||
.replaceAll(tmpFile2, displayFileName2); | ||
|
||
resolve(result); | ||
}, | ||
); | ||
}).finally(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
(async () => { | ||
// Clean up temporary directory. | ||
await fs.rm(tmpDir, { recursive: true }); | ||
})(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters