Skip to content

Commit

Permalink
feat(groups): pin a version group to a specific version
Browse files Browse the repository at this point in the history
Closes #44
Closes #53
Closes #63
Closes #64
  • Loading branch information
JamieMason committed Jan 3, 2022
1 parent c64a109 commit 3de6f90
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/bin-fix-mismatches/fix-mismatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function fixMismatches(input: ProgramInput, disk: Disk): void {

groups.forEach(({ hasMismatches, instances, name }) => {
if (hasMismatches) {
const nextVersion = getExpectedVersion(input, name, instances);
const nextVersion = getExpectedVersion(name, versionGroup, input);
instances.forEach(({ dependencyType, version, wrapper }) => {
const root: any = wrapper.contents;
if (version !== nextVersion) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { getExpectedVersion } from '.';
import type { Instance } from '../../lib/get-input/get-instances';

it('applies pinned versions first', () => {
expect(
getExpectedVersion(
'foo',
{ instances: [], pinVersion: '2.2.2' },
{ wrappers: [] },
),
).toEqual('2.2.2');
});

it('applies matching local package versions second', () => {
expect(
getExpectedVersion(
'foo',
{ instances: [] },
{
wrappers: [
{
contents: { name: 'bar', version: '0.1.0' },
filePath: '',
json: '',
},
{
contents: { name: 'foo', version: '1.2.3' },
filePath: '',
json: '',
},
],
},
),
).toEqual('1.2.3');
});

it('applies the highest installed version third', () => {
expect(
getExpectedVersion(
'foo',
{
instances: [
{ version: '2.0.0' },
{ version: '3.0.0' },
{ version: '1.0.0' },
] as Instance[],
},
{ wrappers: [] },
),
).toEqual('3.0.0');
});

it('returns an empty string if nothing matches', () => {
expect(
getExpectedVersion('foo', { instances: [] }, { wrappers: [] }),
).toEqual('');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { IndexedVersionGroup } from '../../lib/get-input/get-instances';

export function getPinnedVersion(
versionGroup: Pick<IndexedVersionGroup, 'pinVersion'>,
): string {
return versionGroup.pinVersion || '';
}
10 changes: 6 additions & 4 deletions src/bin-fix-mismatches/get-expected-version/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import type { ProgramInput } from '../../lib/get-input';
import type { Instance } from '../../lib/get-input/get-instances';
import type { IndexedVersionGroup } from '../../lib/get-input/get-instances';
import { getHighestVersion } from './get-highest-version';
import { getPinnedVersion } from './get-pinned-version';
import { getWorkspaceVersion } from './get-workspace-version';

export function getExpectedVersion(
input: ProgramInput,
name: string,
instances: Instance[],
versionGroup: Pick<IndexedVersionGroup, 'instances' | 'pinVersion'>,
input: Pick<ProgramInput, 'wrappers'>,
): string {
return (
getPinnedVersion(versionGroup) ||
getWorkspaceVersion(name, input.wrappers) ||
getHighestVersion(instances.map(({ version }) => version))
getHighestVersion(versionGroup.instances.map(({ version }) => version))
);
}
7 changes: 5 additions & 2 deletions src/bin-list-mismatches/list-mismatches.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import chalk from 'chalk';
import { version } from 'os';
import { getExpectedVersion } from '../bin-fix-mismatches/get-expected-version';
import { listVersionGroups } from '../bin-list/list-version-groups';
import type { ProgramInput } from '../lib/get-input';

Expand All @@ -20,10 +22,11 @@ export function listMismatches(input: ProgramInput): void {

groups.forEach(({ hasMismatches, instances, name }) => {
if (hasMismatches) {
console.log(chalk`{red ✕ ${name}}`);
const expectedVersion = getExpectedVersion(name, versionGroup, input);
console.log(chalk`{dim -} ${name} {green.dim ${expectedVersion}}`);
instances.forEach(({ dependencyType, version, wrapper }) => {
console.log(
chalk`{dim -} ${version} {dim in ${dependencyType} of} ${wrapper.contents.name}`,
chalk`{red ${version} {dim in ${dependencyType} of ${wrapper.contents.name}}}`,
);
});
}
Expand Down
9 changes: 8 additions & 1 deletion src/bin-list/list-version-groups.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isNonEmptyString } from 'expect-more';
import type {
IndexedVersionGroup,
Instance,
Expand All @@ -20,9 +21,15 @@ export function listVersionGroups(
versionGroup.instances.sort(sortByName),
);
return Object.entries(instancesByName).map(([name, instances]) => {
const pinnedVersion = versionGroup.pinVersion;
const hasPinnedVersion = isNonEmptyString(pinnedVersion);
const versions = instances.map(({ version }) => version);
const uniques = Array.from(new Set(versions));
const hasMismatches = uniques.length > 1;
const hasMismatches = versions.some(
(version, i) =>
(hasPinnedVersion && version !== pinnedVersion) ||
(i > 0 && version !== versions[i - 1]),
);
return {
hasMismatches,
instances,
Expand Down
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export interface VersionGroup {
* the names of the dependencies (eg. "lodash") which belong to this group
*/
dependencies: string[];
/**
* optionally force all dependencies in this group to have this version
*/
pinVersion?: string;
}

export type SyncpackConfig = Readonly<{
Expand Down

0 comments on commit 3de6f90

Please sign in to comment.