Skip to content

Commit d593a87

Browse files
author
Nicholas Pape
authored
Adds a 'check-versions' command which can be used to locate inconsistencies in dependency versions in a repo (microsoft#101)
* Adds a 'check-versions' command, which finds any versions in the package.json which do not match * Changefile * Whitespace * PR Feedback
1 parent de3e3bf commit d593a87

8 files changed

Lines changed: 454 additions & 2 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/rush-lib",
5+
"comment": "Adds an extra utility (VersionMismatchFinder), which can locate inconsistencies in versions of dependencies across the repo.",
6+
"type": "minor"
7+
},
8+
{
9+
"packageName": "@microsoft/rush",
10+
"comment": "Adds an extra command (rush check-versions), which can find inconsistencies in package.json dependency versions across a repository.",
11+
"type": "minor"
12+
}
13+
],
14+
"email": "nickpape@users.noreply.github.com"
15+
}

‎rush.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@
114114
"packageName": "@microsoft/rush",
115115
"projectFolder": "rush/rush",
116116
"reviewCategory": "libraries",
117-
"shouldPublish": false
117+
"shouldPublish": true
118118
},
119119
{
120120
"packageName": "@microsoft/rush-lib",
121121
"projectFolder": "rush/rush-lib",
122122
"reviewCategory": "libraries",
123-
"shouldPublish": false
123+
"shouldPublish": true
124124
}
125125
]
126126
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import RushConfigurationProject from './RushConfigurationProject';
2+
3+
export class VersionMismatchFinder {
4+
private _projects: RushConfigurationProject[];
5+
/* store it like this:
6+
* {
7+
* "@types/node": {
8+
* "1.0.0": [ '@ms/rush' ]
9+
* }
10+
* }
11+
*/
12+
private _mismatches: Map<string, Map<string, string[]>>;
13+
14+
constructor(projects: RushConfigurationProject[]) {
15+
this._projects = projects;
16+
this._mismatches = new Map<string, Map<string, string[]>>();
17+
this._analyze();
18+
}
19+
20+
public get numberOfMismatches(): number {
21+
return this._mismatches.size;
22+
}
23+
24+
public getMismatches(): Array<string> {
25+
return this._iterableToArray<string>(this._mismatches.keys());
26+
}
27+
28+
public getVersionsOfMismatch(mismatch: string): Array<string> {
29+
return this._mismatches.has(mismatch)
30+
? this._iterableToArray<string>(this._mismatches.get(mismatch).keys())
31+
: undefined;
32+
}
33+
34+
public getConsumersOfMismatch(mismatch: string, version: string): Array<string> {
35+
const mismatchedPackage: Map<string, string[]> = this._mismatches.get(mismatch);
36+
if (!mismatchedPackage) {
37+
return undefined;
38+
}
39+
40+
const mismatchedVersion: string[] = mismatchedPackage.get(version);
41+
return mismatchedVersion;
42+
}
43+
44+
private _analyze(): void {
45+
this._projects.forEach((project: RushConfigurationProject) => {
46+
this._addDependenciesToList(project.packageName, project.packageJson.dependencies);
47+
this._addDependenciesToList(project.packageName, project.packageJson.devDependencies);
48+
// tslint:disable:no-any
49+
this._addDependenciesToList(project.packageName, project.packageJson.peerDependencies);
50+
this._addDependenciesToList(project.packageName, project.packageJson.optionalDependencies);
51+
// tslint:enable:no-any
52+
});
53+
54+
this._mismatches.forEach((mismatches: Map<string, string[]>, project: string) => {
55+
if (mismatches.size <= 1) {
56+
this._mismatches.delete(project);
57+
}
58+
});
59+
}
60+
61+
private _addDependenciesToList(project: string, depMap: { [dependency: string]: string }): void {
62+
Object.keys(depMap || {}).forEach((dependency: string) => {
63+
const version: string = depMap[dependency];
64+
if (!this._mismatches.has(dependency)) {
65+
this._mismatches.set(dependency, new Map<string, string[]>());
66+
}
67+
if (!this._mismatches.get(dependency).has(version)) {
68+
this._mismatches.get(dependency).set(version, []);
69+
}
70+
this._mismatches.get(dependency).get(version).push(project);
71+
});
72+
}
73+
74+
private _iterableToArray<T>(iterable: Iterator<T>): T[] {
75+
const b: T[] = [];
76+
let a: IteratorResult<T>;
77+
while ((a = iterable.next()) && !a.done) {
78+
b.push(a.value);
79+
}
80+
return b;
81+
}
82+
}

0 commit comments

Comments
 (0)