Skip to content

Instantly share code, notes, and snippets.

@Northernside
Created July 5, 2023 16:15
Show Gist options
  • Save Northernside/16c1e9152d024551e9bef7d5848d6d34 to your computer and use it in GitHub Desktop.
Save Northernside/16c1e9152d024551e9bef7d5848d6d34 to your computer and use it in GitHub Desktop.
diffs the labymod launcher code (very basic, not the best but i wanted it quick)
import fs from "node:fs";
import { compare } from "dir-compare";
const path1 = "apps/app-1.0.15";
const path2 = "apps/app-1.0.16";
const options = { compareSize: true };
compare(path1, path2, options)
.then(res => {
for (let diff of res.diffSet)
if (diff.reason) {
console.log(`${diff.path1}/${diff.name1}`, `${diff.path2}/${diff.name2}`);
const diffing = compareFiles(`${diff.path1}/${diff.name1}`, `${diff.path2}/${diff.name2}`);
fs.writeFileSync(`diff/${diff.name1}.json`, JSON.stringify(diffing, null, 2), "utf-8");
}
})
.catch(error => console.error(error));
function compareFiles(file1, file2) {
const content1 = fs.readFileSync(file1, "utf-8");
const content2 = fs.readFileSync(file2, "utf-8");
const lines1 = content1.split("\n");
const lines2 = content2.split("\n");
const diff = [];
lines1.forEach((line, index) => {
if (line !== lines2[index]) {
const diffLine = {
line: index + 1,
content1: "",
content2: ""
};
const diffIndices = getDiffIndices(line, lines2[index]);
if (diffIndices.length > 0) {
let minIndex = diffIndices[0];
let maxIndex = diffIndices[0];
for (let i = 1; i < diffIndices.length; i++) {
minIndex = Math.min(minIndex, diffIndices[i]);
maxIndex = Math.max(maxIndex, diffIndices[i]);
}
const startIndex = Math.max(minIndex - 5, 0);
const endIndex = Math.min(maxIndex + 5, line.length);
diffLine.content1 = line.substring(startIndex, endIndex);
diffLine.content2 = lines2[index].substring(startIndex, endIndex);
}
diff.push(diffLine);
}
});
return diff;
}
function getDiffIndices(str1, str2) {
const indices = [];
for (let i = 0; i < str1.length; i++)
if (str1[i] !== str2[i])
indices.push(i);
return indices;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment