Skip to content

Commit

Permalink
refactor(core): extract a getExpectedVersion function
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMason committed Jan 1, 2022
1 parent fd7ef19 commit ea59d4b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 37 deletions.
8 changes: 3 additions & 5 deletions src/bin-fix-mismatches/fix-mismatches.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import chalk from 'chalk';
import { string } from 'fp-ts';
import { listVersionGroups } from '../bin-list/list-version-groups';
import type { Disk } from '../lib/disk';
import type { ProgramInput } from '../lib/get-input';
import { matchesFilter } from '../lib/matches-filter';
import { writeIfChanged } from '../lib/write-if-changed';
import { getHighestVersion } from './get-highest-version';
import { getWorkspaceVersion } from './get-workspace-version';
import { getExpectedVersion } from './get-expected-version';

export function fixMismatches(input: ProgramInput, disk: Disk): void {
/**
Expand All @@ -24,9 +24,7 @@ export function fixMismatches(input: ProgramInput, disk: Disk): void {

groups.forEach(({ hasMismatches, instances, name }) => {
if (hasMismatches) {
const nextVersion =
getWorkspaceVersion(name, input.wrappers) ||
getHighestVersion(instances.map(({ version }) => version));
const nextVersion = getExpectedVersion(input, name, instances);
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
@@ -1,3 +1,4 @@
import 'expect-more-jest';
import { getHighestVersion } from './get-highest-version';

const shuffle = (array: string[]): string[] => {
Expand All @@ -22,19 +23,19 @@ describe('getHighestVersion', () => {
const j = shuffle([...i, 'git+ssh://git@github.com:npm/cli.git#v1.0.27']);
const k = shuffle([...j, 'git+ssh://git@github.com:npm/cli#semver:^5.0']);
const l = shuffle([...k, 'git://github.com/npm/cli.git#v1.0.27']);
expect(getHighestVersion(a)).toBeNull();
expect(getHighestVersion(b)).toBeNull();
expect(getHighestVersion(c)).toBeNull();
expect(getHighestVersion(d)).toBeNull();
expect(getHighestVersion(e)).toBeNull();
expect(getHighestVersion(f)).toBeNull();
expect(getHighestVersion(g)).toBeNull();
expect(getHighestVersion(h)).toBeNull();
expect(getHighestVersion(i)).toBeNull();
expect(getHighestVersion(i)).toBeNull();
expect(getHighestVersion(j)).toBeNull();
expect(getHighestVersion(k)).toBeNull();
expect(getHighestVersion(l)).toBeNull();
expect(getHighestVersion(a)).toBeEmptyString();
expect(getHighestVersion(b)).toBeEmptyString();
expect(getHighestVersion(c)).toBeEmptyString();
expect(getHighestVersion(d)).toBeEmptyString();
expect(getHighestVersion(e)).toBeEmptyString();
expect(getHighestVersion(f)).toBeEmptyString();
expect(getHighestVersion(g)).toBeEmptyString();
expect(getHighestVersion(h)).toBeEmptyString();
expect(getHighestVersion(i)).toBeEmptyString();
expect(getHighestVersion(i)).toBeEmptyString();
expect(getHighestVersion(j)).toBeEmptyString();
expect(getHighestVersion(k)).toBeEmptyString();
expect(getHighestVersion(l)).toBeEmptyString();
});

it('returns the newest version from an array of versions', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@ import {
RANGE_LTE,
RANGE_MINOR,
RANGE_PATCH,
} from '../constants';
import { isSemver } from '../lib/is-semver';
} from '../../constants';
import { isSemver } from '../../lib/is-semver';

function getRange(version: string): string {
return version.slice(0, version.search(/[0-9]/));
export function getHighestVersion(versions: string[]): string {
return versions.reduce<string>((rawHighest, raw) => {
const version = valid(coerce(raw)) || '';
const highest = valid(coerce(rawHighest)) || '';
if (raw === '*' || rawHighest === '*') return '*';
if (!isSemver(raw) || version === '') return rawHighest;
if (highest === '') return raw;
if (gt(version, highest)) return raw;
if (eq(version, highest) && getRangeScore(raw) > getRangeScore(rawHighest))
return raw;
return rawHighest;
}, '');
}

function getRangeScore(version: string | null): number {
if (version === null) return 0;
function getRangeScore(version: string): number {
if (version === '') return 0;
if (version === RANGE_ANY) return 8;
const range = getRange(version);
if (range === RANGE_GT) return 7;
Expand All @@ -30,16 +40,6 @@ function getRangeScore(version: string | null): number {
return 0;
}

export function getHighestVersion(versions: string[]): string | null {
return versions.reduce<string | null>((rawHighest, raw) => {
const version = valid(coerce(raw));
const highest = valid(coerce(rawHighest));
if (raw === '*' || rawHighest === '*') return '*';
if (!isSemver(raw) || version === null) return rawHighest;
if (highest === null) return raw;
if (gt(version, highest)) return raw;
if (eq(version, highest) && getRangeScore(raw) > getRangeScore(rawHighest))
return raw;
return rawHighest;
}, null);
function getRange(version: string): string {
return version.slice(0, version.search(/[0-9]/));
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SourceWrapper } from '../lib/get-input/get-wrappers';
import type { SourceWrapper } from '../../lib/get-input/get-wrappers';

/**
* If the dependency `name` is a package developed locally in this monorepo, we
Expand Down
15 changes: 15 additions & 0 deletions src/bin-fix-mismatches/get-expected-version/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { ProgramInput } from '../../lib/get-input';
import type { Instance } from '../../lib/get-input/get-instances';
import { getHighestVersion } from './get-highest-version';
import { getWorkspaceVersion } from './get-workspace-version';

export function getExpectedVersion(
input: ProgramInput,
name: string,
instances: Instance[],
): string {
return (
getWorkspaceVersion(name, input.wrappers) ||
getHighestVersion(instances.map(({ version }) => version))
);
}

0 comments on commit ea59d4b

Please sign in to comment.