import { check } from './check.js'; import { dependenciesToFixedSummary, dependenciesToMismatchSummary, } from './output.js'; import type { Dependencies, Options } from './types.js'; /** Relevant public data about a dependency. */ type Dependency = { name: string; isFixable: boolean; isMismatching: boolean; versions: readonly { version: string; packages: readonly { pathRelative: string }[]; }[]; }; export class CDVC { /** An object mapping each dependency in the workspace to information including the versions found of it. */ private readonly dependencies: Dependencies; /** * @param path - path to the workspace root * @param options * @param options.fix - Whether to autofix inconsistencies (using latest version present) * @param options.ignoreDep - Dependency(s) to ignore mismatches for * @param options.ignoreDepPattern - RegExp(s) of dependency names to ignore mismatches for * @param options.ignorePackage - Workspace package(s) to ignore mismatches for * @param options.ignorePackagePattern - RegExp(s) of package names to ignore mismatches for * @param options.ignorePath - Workspace-relative path(s) of packages to ignore mismatches for * @param options.ignorePathPattern - RegExp(s) of workspace-relative path of packages to ignore mismatches for */ constructor(path: string, options?: Options) { const { dependencies } = check(path, options); this.dependencies = dependencies; } public toMismatchSummary(): string { return dependenciesToMismatchSummary(this.dependencies); } public toFixedSummary(): string { return dependenciesToFixedSummary(this.dependencies); } public getDependencies(): readonly Dependency[] { return Object.keys(this.dependencies).map((dependency) => this.getDependency(dependency), ); } public getDependency(name: string): Dependency { const dep = this.dependencies[name]; if (!dep) { throw new Error(`Dependency "${name}" not found`); } // Convert underlying dependency data object with relevant public data. return { name, isFixable: dep.isFixable, isMismatching: dep.isMismatching, versions: dep.versions.map((version) => ({ version: version.version, packages: version.packages.map((package_) => ({ pathRelative: package_.pathRelative, })), })), }; } public get hasMismatchingDependencies(): boolean { return Object.values(this.dependencies).some((dep) => dep.isMismatching); } public get hasMismatchingDependenciesFixable(): boolean { return Object.values(this.dependencies).some( (dep) => dep.isMismatching && dep.isFixable, ); } public get hasMismatchingDependenciesNotFixable(): boolean { return Object.values(this.dependencies).some( (dep) => dep.isMismatching && !dep.isFixable, ); } }