Skip to content

Instantly share code, notes, and snippets.

@n1ru4l
Last active March 11, 2021 23:00
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save n1ru4l/70e496d743e4530692400a816dbbf6f6 to your computer and use it in GitHub Desktop.
Find conflicting package.json ranges in mono repository
/**
* This is a simple script for detecting conflicting
* version ranges across packages inside a mono repository.
*/
import fs from "fs";
import glob from "glob"; // yarn add -W -E -E -D glob@7.1.6
import { promisify } from "util";
const globP = promisify(glob);
const main = async () => {
const filePaths = await globP("**/package.json", {
root: process.cwd(),
ignore: ["**/node_modules/**", "**/dist/**"],
});
const moduleMap = new Map<string, Set<string>>();
const gatherDependencies = (record: { [key: string]: string }) => {
for (const [packageName, version] of Object.entries(record)) {
let moduleVersions = moduleMap.get(packageName);
if (moduleVersions === undefined) {
const newSet = new Set<string>();
moduleMap.set(packageName, newSet);
moduleVersions = newSet;
}
moduleVersions.add(version);
}
};
for (const filePath of filePaths) {
const obj = JSON.parse(fs.readFileSync(filePath, "utf-8"));
if (obj.dependencies) {
gatherDependencies(obj.dependencies);
}
if (obj.devDependencies) {
gatherDependencies(obj.devDependencies);
}
}
for (const [packageName, versions] of moduleMap.entries()) {
if (versions.size <= 1) {
continue;
}
console.log(`${packageName}`, Array.from(versions).join(", "));
}
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment