Skip to content

Instantly share code, notes, and snippets.

@felipecsl
Created September 30, 2024 20:53
Show Gist options
  • Save felipecsl/c167504f14f8386503d1c30cd65a1757 to your computer and use it in GitHub Desktop.
Save felipecsl/c167504f14f8386503d1c30cd65a1757 to your computer and use it in GitHub Desktop.
Given `before-yarn.lock` and `after-yarn.lock`, prints the dependencies changed in a human readable list
const fs = require('fs');
const yarnLockfile = require('@yarnpkg/lockfile');
function parseLockfile(filePath) {
const file = fs.readFileSync(filePath, 'utf8');
return yarnLockfile.parse(file).object;
}
function getVersionDifferences(beforeLockfile, afterLockfile) {
const diffs: Record<string, { before: string, after: string }> = {};
const allKeys = new Set<string>([
...Object.keys(beforeLockfile),
...Object.keys(afterLockfile),
]);
allKeys.forEach((key) => {
const dep = key.split("@")[0];
const beforeVersion = beforeLockfile[key]?.vearsion;
const afterVersion = afterLockfile[key]?.version;
if (!diffs[dep]) {
diffs[dep] = { before: beforeVersion, after: afterVersion };
} else {
if (beforeVersion) diffs[dep].before = beforeVersion;
if (afterVersion) diffs[dep].after = afterVersion;
}
});
return diffs;
}
const beforeLockfile = parseLockfile('before-yarn.lock');
const afterLockfile = parseLockfile('after-yarn.lock');
const versionDifferences = getVersionDifferences(beforeLockfile, afterLockfile);
console.log('Dependency Version Changes:\n');
Object.entries(versionDifferences)
.filter(([_, v]) => v.before !== v.after)
.sort(([a], [b]) => a.localeCompare(b))
.forEach(([k, v]) => console.log(`- ${k}: ${v.before || 'none'} -> ${v.after || 'removed'}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment