Clean up diff output a bit

This commit is contained in:
ewin 2025-03-28 00:50:22 -04:00
parent 8840d1a61f
commit d77d0d1c59
Signed by: erin
SSH key fingerprint: SHA256:swjoHhREbZPbWe+gyJNi24d4NAxJSyUIm3fpZj4z3wc

View file

@ -7,9 +7,7 @@ import {execSync} from 'node:child_process';
* @param {string} str
* @returns {string}
*/
function toBase64 (str) {
return Buffer.from(str, 'utf-8').toString('base64');
}
const toBase64 = str => Buffer.from(str, 'utf-8').toString('base64');
/**
* terrible terrible terrible string diff helper for debugging
@ -19,13 +17,14 @@ function toBase64 (str) {
export function diff (a, b) {
// base64 input strings before passing to shell to avoid escaping issues
// https://stackoverflow.com/a/60221847
// also use `|| true` to not throw an error when `diff` returns non-zero
execSync(`bash -c '
diff --color -u <(echo ${toBase64(a)} | base64 -d) <(echo ${toBase64(b)} | base64 -d)
' || true`, {
// use tail to cut off useless file info lines
execSync(String.raw`bash <<- EOF
diff --color=always -u \
<(echo "${toBase64(a)}" | base64 -d) \
<(echo "${toBase64(b)}" | base64 -d) \
| tail -n +3
EOF`, {
// display result directly in terminal
stdio: 'inherit',
});
}
'yeet'