-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
887 additions
and
308 deletions.
There are no files selected for viewing
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { check } from './check.js'; | ||
import { Dependency } from './dependency.js'; | ||
import { | ||
dependenciesToFixedSummary, | ||
dependenciesToMismatchSummary, | ||
} from './output.js'; | ||
import { Dependencies } from './types.js'; | ||
|
||
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?: { | ||
fix?: boolean; | ||
ignoreDep?: string[]; | ||
ignoreDepPattern?: string[]; | ||
ignorePackage?: string[]; | ||
ignorePackagePattern?: string[]; | ||
ignorePath?: string[]; | ||
ignorePathPattern?: string[]; | ||
} | ||
) { | ||
const { dependencies } = check(path, options); | ||
|
||
this.dependencies = dependencies; | ||
} | ||
|
||
public toMismatchSummary(): string { | ||
return dependenciesToMismatchSummary(this.dependencies); | ||
} | ||
|
||
public toFixedSummary(): string { | ||
return dependenciesToFixedSummary(this.dependencies); | ||
} | ||
|
||
public getDependencies(): Dependency[] { | ||
return Object.keys(this.dependencies).map((dependency) => | ||
this.getDependency(dependency) | ||
); | ||
} | ||
|
||
public getDependency(name: string): Dependency { | ||
// Convert underlying dependency data to Dependency class which only exposes relevant data publicly. | ||
return new Dependency( | ||
name, | ||
this.dependencies[name].isFixable, | ||
this.dependencies[name].versions.map((version) => ({ | ||
version: version.version, | ||
packages: version.packages.map((package_) => package_.pathRelative), | ||
})) | ||
); | ||
} | ||
|
||
public hasMismatchingDependencies(): boolean { | ||
return Object.values(this.dependencies).some((dep) => dep.isMismatching); | ||
} | ||
|
||
public hasMismatchingDependenciesFixable(): boolean { | ||
return Object.values(this.dependencies).some( | ||
(dep) => dep.isMismatching && dep.isFixable | ||
); | ||
} | ||
|
||
public hasMismatchingDependenciesNotFixable(): boolean { | ||
return Object.values(this.dependencies).some( | ||
(dep) => dep.isMismatching && !dep.isFixable | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { | ||
calculateVersionsForEachDependency, | ||
calculateDependenciesAndVersions, | ||
filterOutIgnoredDependencies, | ||
fixVersionsMismatching, | ||
} from './dependency-versions.js'; | ||
import { Dependencies } from './types.js'; | ||
import { getPackages } from './workspace.js'; | ||
|
||
/** | ||
* Checks for inconsistencies across a workspace. Optionally fixes them. | ||
* @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 | ||
* @returns an object with the following properties: | ||
* - `dependencies`: An object mapping each dependency in the workspace to information about it including the versions found of it. | ||
*/ | ||
export function check( | ||
path: string, | ||
options?: { | ||
fix?: boolean; | ||
ignoreDep?: string[]; | ||
ignoreDepPattern?: string[]; | ||
ignorePackage?: string[]; | ||
ignorePackagePattern?: string[]; | ||
ignorePath?: string[]; | ||
ignorePathPattern?: string[]; | ||
} | ||
): { | ||
dependencies: Dependencies; | ||
} { | ||
const optionsWithDefaults = { | ||
fix: false, | ||
ignoreDep: [], | ||
ignoreDepPattern: [], | ||
ignorePackage: [], | ||
ignorePackagePattern: [], | ||
ignorePath: [], | ||
ignorePathPattern: [], | ||
...options, | ||
}; | ||
|
||
// Calculate. | ||
const packages = getPackages( | ||
path, | ||
optionsWithDefaults.ignorePackage, | ||
optionsWithDefaults.ignorePackagePattern.map((s) => new RegExp(s)), | ||
optionsWithDefaults.ignorePath, | ||
optionsWithDefaults.ignorePathPattern.map((s) => new RegExp(s)) | ||
); | ||
|
||
const dependencies = calculateVersionsForEachDependency(packages); | ||
const dependenciesAndVersions = | ||
calculateDependenciesAndVersions(dependencies); | ||
const dependenciesAndVersionsWithMismatches = dependenciesAndVersions.filter( | ||
({ versions }) => versions.length > 1 | ||
); | ||
|
||
// Information about all dependencies. | ||
const dependenciesAndVersionsWithoutIgnored = filterOutIgnoredDependencies( | ||
dependenciesAndVersions, | ||
optionsWithDefaults.ignoreDep, | ||
optionsWithDefaults.ignoreDepPattern.map((s) => new RegExp(s)) | ||
); | ||
|
||
// Information about mismatches. | ||
const dependenciesAndVersionsMismatchesWithoutIgnored = | ||
filterOutIgnoredDependencies( | ||
dependenciesAndVersionsWithMismatches, | ||
optionsWithDefaults.ignoreDep, | ||
optionsWithDefaults.ignoreDepPattern.map((s) => new RegExp(s)) | ||
); | ||
const resultsAfterFix = fixVersionsMismatching( | ||
packages, | ||
dependenciesAndVersionsMismatchesWithoutIgnored, | ||
!optionsWithDefaults.fix // Do dry-run if not fixing. | ||
); | ||
const versionsMismatchingFixable = resultsAfterFix.fixable; | ||
|
||
return { | ||
// Information about all dependencies. | ||
dependencies: Object.fromEntries( | ||
dependenciesAndVersionsWithoutIgnored.map(({ dependency, versions }) => { | ||
return [ | ||
dependency, | ||
{ | ||
isFixable: versionsMismatchingFixable.some( | ||
(dep) => dep.dependency === dependency | ||
), | ||
isMismatching: dependenciesAndVersionsMismatchesWithoutIgnored.some( | ||
(dep) => dep.dependency === dependency | ||
), | ||
versions, | ||
}, | ||
]; | ||
}) | ||
), | ||
}; | ||
} |
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
Oops, something went wrong.