Skip to content

Instantly share code, notes, and snippets.

@brookjordan
Created August 4, 2020 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brookjordan/46f0dfd2f950608786bd6c4f2c7bc965 to your computer and use it in GitHub Desktop.
Save brookjordan/46f0dfd2f950608786bd6c4f2c7bc965 to your computer and use it in GitHub Desktop.
Pass in an entire yarn.lock file contents and this will spit out duplicate resolutions
function getYarnLockDupes(yarnLock) {
return yarnLock
.split('\n')
.filter(a => !a.startsWith(' ') && !a.startsWith('#'))
.map(a => a.replace(/[:"]/g, ''))
.flatMap(a => a.split(/, ?/g))
.filter(a => a)
.flatMap(a => {
const versions = a.split(' || ');
if (versions.length === 1) { return a; }
const name = versions[0].split('@');
versions[0] = versions[0].split('@')[1];
return versions.map(b => `${name}@${b}`);
})
.map(a => a.replace(/^@/g, '™'))
.map(a => a.split('@').map(a => a.replace(/™/g, '@')))
.filter((a, i, arr) =>
(arr.filter(([b]) => a[0] === b)).length > 1
)
.sort(([a], [b]) => a.localeCompare(b))
.reduce((acc, [name, version]) => {
acc[name] || (acc[name] = []);
acc[name].push(version);
acc[name] = acc[name].filter((a, i, arr) => arr.indexOf(a) === i);
return acc;
}, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment