Created
September 30, 2024 20:53
-
-
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
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
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