@hyperse/dependency-sync
Version:
A comprehensive Node.js utility for managing dependencies in monorepo environments, specifically designed for Hyperse plugin ecosystems.
43 lines (42 loc) • 1.62 kB
JavaScript
import { fileWalk } from '../utils/fileWalk.js';
import { readJson } from '../utils/readJson.js';
/**
* Check for conflicting packages in the node_modules directory.
* @example
* ```ts
* const files = await checkConflicts([
* '@vendure/*',
* '@hyperse-hub/*',
* '@nestjs/common',
* ]);
* ```
* @param nodeModulesPath - Path to node_modules directory
* @param pkgPatterns - The modules to check for conflicts.
* @param ignorePackages - Packages to ignore when checking for conflicts
* @returns Map of package names to their conflicting versions
*/
export const checkConflicts = async (nodeModulesPath, pkgPatterns, ignorePackages = []) => {
const packageJsonFiles = await findPackageJsonFiles(nodeModulesPath, pkgPatterns);
const versionMap = await buildVersionMap(packageJsonFiles);
return filterConflicts(versionMap, ignorePackages);
};
async function findPackageJsonFiles(nodeModulesPath, pkgPatterns) {
const patterns = pkgPatterns.map((scoped) => `**/${scoped}/package.json`);
return fileWalk(patterns, {
cwd: nodeModulesPath,
unique: true,
});
}
async function buildVersionMap(files) {
const versionMap = new Map();
for (const file of files) {
const { name, version } = readJson(file);
const versions = versionMap.get(name) ?? [];
versions.push(version);
versionMap.set(name, versions);
}
return versionMap;
}
function filterConflicts(versionMap, ignorePackages) {
return new Map(Array.from(versionMap.entries()).filter(([name, versions]) => versions.length > 1 && !ignorePackages.includes(name)));
}