-
Notifications
You must be signed in to change notification settings - Fork 6
Add Node API #591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Add Node API #591
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,94 @@ | ||
| import { check } from './check.js'; | ||
| import { | ||
| dependenciesToFixedSummary, | ||
| dependenciesToMismatchSummary, | ||
| } from './output.js'; | ||
| import { Dependencies } from './types.js'; | ||
|
|
||
| /** Relevant public data about a dependency. */ | ||
| type Dependency = { | ||
| name: string; | ||
| isFixable: boolean; | ||
| isMismatching: boolean; | ||
| versions: readonly { | ||
| version: string; | ||
| /** Relative path to each package.*/ | ||
| packages: readonly 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?: { | ||
| fix?: boolean; | ||
| ignoreDep?: readonly string[]; | ||
| ignoreDepPattern?: readonly string[]; | ||
| ignorePackage?: readonly string[]; | ||
| ignorePackagePattern?: readonly string[]; | ||
| ignorePath?: readonly string[]; | ||
| ignorePathPattern?: readonly 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(): readonly Dependency[] { | ||
| return Object.keys(this.dependencies).map((dependency) => | ||
| this.getDependency(dependency) | ||
| ); | ||
| } | ||
|
|
||
| public getDependency(name: string): Dependency { | ||
| // Convert underlying dependency data object with relevant public data. | ||
| return { | ||
| name, | ||
| isFixable: this.dependencies[name].isFixable, | ||
| isMismatching: this.dependencies[name].isMismatching, | ||
| versions: this.dependencies[name].versions.map((version) => ({ | ||
| version: version.version, | ||
| packages: version.packages.map((package_) => 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 | ||
| ); | ||
| } | ||
| } |
This file contains hidden or 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?: readonly string[]; | ||
| ignoreDepPattern?: readonly string[]; | ||
| ignorePackage?: readonly string[]; | ||
| ignorePackagePattern?: readonly string[]; | ||
| ignorePath?: readonly string[]; | ||
| ignorePathPattern?: readonly 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.