Skip to content

Commit 565c1e7

Browse files
committed
feat(groups): target specific dependency types
1 parent 75750dc commit 565c1e7

6 files changed

Lines changed: 43 additions & 23 deletions

File tree

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = {
55
coverageThreshold: {
66
global: {
77
branches: 69,
8-
functions: 84,
8+
functions: 83,
99
lines: 81,
1010
statements: 80,
1111
},

src/bin-fix-mismatches/get-expected-version/get-expected-version.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ it('applies the highest installed version third', () => {
4040
'foo',
4141
{
4242
instances: [
43-
{ version: '2.0.0' },
44-
{ version: '3.0.0' },
45-
{ version: '1.0.0' },
43+
{ name: 'foo', version: '2.0.0' },
44+
{ name: 'foo', version: '3.0.0' },
45+
{ name: 'foo', version: '1.0.0' },
4646
] as Instance[],
4747
},
4848
{ wrappers: [] },

src/bin-fix-mismatches/get-expected-version/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export function getExpectedVersion(
1212
return (
1313
getPinnedVersion(versionGroup) ||
1414
getWorkspaceVersion(name, input.wrappers) ||
15-
getHighestVersion(versionGroup.instances.map(({ version }) => version))
15+
getHighestVersion(
16+
versionGroup.instances
17+
.filter((instance) => instance.name === name)
18+
.map(({ version }) => version),
19+
)
1620
);
1721
}

src/bin-list-mismatches/list-mismatches.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { listVersionGroups } from '../bin-list/list-version-groups';
55
import type { ProgramInput } from '../lib/get-input';
66

77
export function listMismatches(input: ProgramInput): void {
8-
const isInvalid = false;
8+
let isInvalid = false;
99

1010
/**
1111
* Reverse the list so the default/ungrouped version group is rendered first
@@ -14,22 +14,26 @@ export function listMismatches(input: ProgramInput): void {
1414
*/
1515
input.instances.versionGroups.reverse().forEach((versionGroup, i) => {
1616
const isVersionGroup = i > 0;
17-
const groups = listVersionGroups(versionGroup);
17+
const groups = listVersionGroups(versionGroup).filter(
18+
({ hasMismatches }) => hasMismatches,
19+
);
1820

19-
if (isVersionGroup) {
20-
console.log(chalk`{dim = Version Group ${i} ${'='.repeat(63)}}`);
21-
}
21+
if (groups.length > 0) {
22+
isInvalid = true;
2223

23-
groups.forEach(({ hasMismatches, instances, name }) => {
24-
if (hasMismatches) {
25-
const expectedVersion = getExpectedVersion(name, versionGroup, input);
26-
console.log(chalk`{dim -} ${name} {green.dim ${expectedVersion}}`);
27-
instances.forEach(({ dependencyType, version, wrapper }) => {
28-
console.log(
29-
chalk`{red ${version} {dim in ${dependencyType} of ${wrapper.contents.name}}}`,
30-
);
31-
});
24+
if (isVersionGroup) {
25+
console.log(chalk`{dim = Version Group ${i} ${'='.repeat(63)}}`);
3226
}
27+
}
28+
29+
groups.forEach(({ instances, name }) => {
30+
const expectedVersion = getExpectedVersion(name, versionGroup, input);
31+
console.log(chalk`{dim -} ${name} {green.dim ${expectedVersion}}`);
32+
instances.forEach(({ dependencyType, version, wrapper }) => {
33+
console.log(
34+
chalk`{red ${version} {dim in ${dependencyType} of ${wrapper.contents.name}}}`,
35+
);
36+
});
3337
});
3438
});
3539

src/constants.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export interface SemverGroup {
3535
* the semver range which dependencies in this group should use
3636
*/
3737
range: ValidRange;
38+
/**
39+
* optionally only apply this group to dependencies of the provided types
40+
*/
41+
dependencyTypes?: DependencyType[];
3842
}
3943

4044
export interface VersionGroup {
@@ -51,6 +55,10 @@ export interface VersionGroup {
5155
* optionally force all dependencies in this group to have this version
5256
*/
5357
pinVersion?: string;
58+
/**
59+
* optionally only apply this group to dependencies of the provided types
60+
*/
61+
dependencyTypes?: DependencyType[];
5462
}
5563

5664
export type SyncpackConfig = Readonly<{

src/lib/get-input/get-instances.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isNonEmptyString, isObject } from 'expect-more';
1+
import { isNonEmptyArray, isNonEmptyString, isObject } from 'expect-more';
22
import minimatch from 'minimatch';
33
import type {
44
DependencyType,
@@ -52,8 +52,8 @@ export function getInstances(
5252
if (!isNonEmptyString(version)) continue;
5353
const instance = { dependencyType, name, version, wrapper };
5454
allInstances.all.push(instance);
55-
groupInstancesBy('semverGroups', pkgName, instance);
56-
groupInstancesBy('versionGroups', pkgName, instance);
55+
groupInstancesBy('semverGroups', dependencyType, pkgName, instance);
56+
groupInstancesBy('versionGroups', dependencyType, pkgName, instance);
5757
}
5858
}
5959
}
@@ -68,6 +68,7 @@ export function getInstances(
6868

6969
function groupInstancesBy(
7070
groupName: 'semverGroups' | 'versionGroups',
71+
dependencyType: DependencyType,
7172
pkgName: string,
7273
instance: Instance,
7374
): void {
@@ -76,7 +77,7 @@ export function getInstances(
7677
if (!groups.length) return;
7778
for (const i in groups) {
7879
const group = groups[i];
79-
if (matchesGroup(pkgName, name, group)) {
80+
if (matchesGroup(dependencyType, pkgName, name, group)) {
8081
if (!group.instancesByName[name]) {
8182
group.instancesByName[name] = [];
8283
}
@@ -89,11 +90,14 @@ export function getInstances(
8990
}
9091

9192
function matchesGroup(
93+
dependencyType: DependencyType,
9294
pkgName: string,
9395
dependencyName: string,
9496
group: SemverGroup | VersionGroup,
9597
): boolean {
9698
return (
99+
(!isNonEmptyArray(group.dependencyTypes) ||
100+
group.dependencyTypes.includes(dependencyType)) &&
97101
group.packages.some((pattern) => minimatch(pkgName, pattern)) &&
98102
group.dependencies.some((pattern) => minimatch(dependencyName, pattern))
99103
);

0 commit comments

Comments
 (0)