Skip to content

Commit fd387c5

Browse files
committed
feat: show count of each mismatching version for a dependency
Helpful to see the number of times each version has been seen across the workspace, especially when deciding what the correct version to use is.
1 parent 80c2625 commit fd387c5

File tree

6 files changed

+75
-12
lines changed

6 files changed

+75
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ If there are no dependency mismatches, the program will exit with success.
3232
If there are any dependency mismatches, the program will exit with failure and output the mismatching versions:
3333

3434
```pt
35-
eslint has more than one version: ^7.0.0, ^7.1.0
36-
sinon has more than one version: ^1.17.7, ^9.0.3
35+
eslint has more than one version: ^7.0.0 (1 usage), ^7.1.0 (5 usages)
36+
sinon has more than one version: ^1.17.7 (1 usage), ^9.0.3 (3 usages)
3737
```
3838

3939
## Options

lib/dependency-versions.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ export type DependenciesToVersionsSeen = Map<
88

99
export type MismatchingDependencyVersions = Array<{
1010
dependency: string;
11-
versions: string[];
11+
versions: {
12+
version: string;
13+
count: number;
14+
}[];
1215
}>;
1316

1417
/**
@@ -118,8 +121,13 @@ export function calculateMismatchingVersions(
118121
const uniqueVersions = [
119122
...new Set(versionList.map((obj) => obj.version)),
120123
].sort();
124+
const uniqueVersionsWithCounts = uniqueVersions.map((uniqueVersion) => ({
125+
version: uniqueVersion,
126+
count: versionList.filter((obj) => obj.version === uniqueVersion)
127+
.length,
128+
}));
121129
if (uniqueVersions.length > 1) {
122-
return { dependency, versions: uniqueVersions };
130+
return { dependency, versions: uniqueVersionsWithCounts };
123131
}
124132

125133
return undefined;

lib/output.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ export function mismatchingVersionsToOutputLines(
55
): string[] {
66
return mismatchingDependencyVersions.map(
77
(obj) =>
8-
`${obj.dependency} has more than one version: ${obj.versions.join(', ')}`
8+
`${obj.dependency} has more than one version: ${obj.versions
9+
.map(
10+
(versionObj) =>
11+
`${versionObj.version} (${versionObj.count} ${pluralizeUsage(
12+
versionObj.count
13+
)})`
14+
)
15+
.join(', ')}`
916
);
1017
}
18+
19+
function pluralizeUsage(count: number) {
20+
return count === 1 ? 'usage' : 'usages';
21+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"foo": "1.2.0"
4+
}
5+
}

test/lib/dependency-versions.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,29 @@ describe('Utils | dependency-versions', function () {
2828
deepStrictEqual(calculateMismatchingVersions(dependencyVersions), [
2929
{
3030
dependency: 'baz',
31-
versions: ['^7.8.9', '^8.0.0'],
31+
versions: [
32+
{
33+
count: 1,
34+
version: '^7.8.9',
35+
},
36+
{
37+
count: 1,
38+
version: '^8.0.0',
39+
},
40+
],
3241
},
3342
{
3443
dependency: 'foo',
35-
versions: ['1.2.0', '1.3.0'],
44+
versions: [
45+
{
46+
count: 2,
47+
version: '1.2.0',
48+
},
49+
{
50+
count: 1,
51+
version: '1.3.0',
52+
},
53+
],
3654
},
3755
]);
3856
});
@@ -46,7 +64,16 @@ describe('Utils | dependency-versions', function () {
4664
[
4765
{
4866
dependency: 'foo',
49-
versions: ['1.2.0', '1.3.0'],
67+
versions: [
68+
{
69+
count: 2,
70+
version: '1.2.0',
71+
},
72+
{
73+
count: 1,
74+
version: '1.3.0',
75+
},
76+
],
5077
},
5178
]
5279
);

test/lib/output.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,24 @@ describe('Utils | output', function () {
77
it('behaves correctly', function () {
88
deepStrictEqual(
99
mismatchingVersionsToOutputLines([
10-
{ dependency: 'foo', versions: ['1.2.3', '4.5.6'] },
11-
{ dependency: 'bar', versions: ['1.4.0', '2.0.0'] },
10+
{
11+
dependency: 'foo',
12+
versions: [
13+
{ version: '1.2.3', count: 1 },
14+
{ version: '4.5.6', count: 2 },
15+
],
16+
},
17+
{
18+
dependency: 'bar',
19+
versions: [
20+
{ version: '1.4.0', count: 3 },
21+
{ version: '2.0.0', count: 4 },
22+
],
23+
},
1224
]),
1325
[
14-
'foo has more than one version: 1.2.3, 4.5.6',
15-
'bar has more than one version: 1.4.0, 2.0.0',
26+
'foo has more than one version: 1.2.3 (1 usage), 4.5.6 (2 usages)',
27+
'bar has more than one version: 1.4.0 (3 usages), 2.0.0 (4 usages)',
1628
]
1729
);
1830
});

0 commit comments

Comments
 (0)