Last active
March 11, 2021 23:00
-
-
Save n1ru4l/70e496d743e4530692400a816dbbf6f6 to your computer and use it in GitHub Desktop.
Find conflicting package.json ranges in mono repository
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
/** | |
* 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