-
Notifications
You must be signed in to change notification settings - Fork 6
/
cdvc.ts
84 lines (73 loc) · 2.76 KB
/
cdvc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { check } from './check.js';
import {
dependenciesToFixedSummary,
dependenciesToMismatchSummary,
} from './output.js';
import { 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 {
// 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_) => ({
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,
);
}
}