Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gtkatakura/de72c34b3a7a92959641adf86bc87ebc to your computer and use it in GitHub Desktop.
Save gtkatakura/de72c34b3a7a92959641adf86bc87ebc 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 [email protected]
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