Skip to content

Commit

Permalink
Use latest version instead of highest version (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddzz authored Jan 23, 2022
1 parent c06622d commit 96f93b4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Found 1 dependency with mismatching versions across the workspace. Fix with `--f

| Name | Description |
| --- | --- |
| `--fix` | Whether to autofix inconsistencies (using highest version present). |
| `--fix` | Whether to autofix inconsistencies (using latest version present). |
| `--ignore-dep` | Dependency to ignore mismatches for (option can be repeated). |
| `--ignore-dep-pattern` | RegExp of dependency names to ignore mismatches for (option can be repeated). |
| `--ignore-package` | Workspace package to ignore mismatches for (option can be repeated). |
Expand Down
2 changes: 1 addition & 1 deletion bin/check-dependency-version-consistency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function run() {
.addArgument(new Argument('[path]', 'path to workspace root').default('.'))
.option(
'--fix',
'Whether to autofix inconsistencies (using highest version present)',
'Whether to autofix inconsistencies (using latest version present)',
false
)
.option(
Expand Down
6 changes: 3 additions & 3 deletions lib/dependency-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export function fixMismatchingVersions(
);
let fixedVersion;
try {
fixedVersion = getHighestVersion(versions);
fixedVersion = getLatestVersion(versions);
} catch {
// Skip this dependency.
notFixed.push(mismatchingVersion);
Expand Down Expand Up @@ -295,7 +295,7 @@ export function compareRanges(a: string, b: string): 0 | -1 | 1 {
return semver.gt(aVersion, bVersion) ? 1 : -1;
}

export function getHighestVersion(versions: string[]): string {
export function getLatestVersion(versions: string[]): string {
const sortedVersions = versions.sort(compareRanges);
return sortedVersions[sortedVersions.length - 1]; // Highest version will be sorted to end of list.
return sortedVersions[sortedVersions.length - 1]; // Latest version will be sorted to end of list.
}
12 changes: 6 additions & 6 deletions lib/output.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk';
import type { MismatchingDependencyVersions } from './dependency-versions.js';
import { compareRanges, getHighestVersion } from './dependency-versions.js';
import { compareRanges, getLatestVersion } from './dependency-versions.js';
import { table } from 'table';

export function mismatchingVersionsToOutput(
Expand All @@ -17,9 +17,9 @@ export function mismatchingVersionsToOutput(
const usageCounts = object.versions.map(
(versionObject) => versionObject.packages.length
);
const highestUsageCount = Math.max(...usageCounts);
const latestUsageCount = Math.max(...usageCounts);
const hasMultipleUsageCounts = !usageCounts.every(
(count) => count === highestUsageCount
(count) => count === latestUsageCount
);

const rows = object.versions
Expand All @@ -43,8 +43,8 @@ export function mismatchingVersionsToOutput(
: packageNames.join(', ');
return [
chalk.redBright(versionObject.version),
// Bold the usage count if it's the highest, as long as it's not the only usage count present.
usageCount === highestUsageCount && hasMultipleUsageCounts
// Bold the usage count if it's the latest, as long as it's not the only usage count present.
usageCount === latestUsageCount && hasMultipleUsageCounts
? chalk.bold(usageCount)
: usageCount,
packageListSentence,
Expand Down Expand Up @@ -73,7 +73,7 @@ export function mismatchingVersionsFixedToOutput(
.flatMap((mismatchingVersion) => {
let version;
try {
version = getHighestVersion(
version = getLatestVersion(
mismatchingVersion.versions.map((v) => v.version)
);
} catch {
Expand Down
28 changes: 14 additions & 14 deletions test/lib/dependency-versions-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
filterOutIgnoredDependencies,
fixMismatchingVersions,
compareRanges,
getHighestVersion,
getLatestVersion,
} from '../../lib/dependency-versions.js';
import { getPackages } from '../../lib/workspace.js';
import {
Expand Down Expand Up @@ -331,15 +331,15 @@ describe('Utils | dependency-versions', function () {
const packageJson2: PackageJson = JSON.parse(packageJson2Contents);

// foo
// updates the package1 `foo` version to the highest version
// updates the package1 `foo` version to the latest version
expect(
packageJson1.dependencies && packageJson1.dependencies.foo
).toStrictEqual('^2.0.0');
// does not change package2 `foo` version since already at highest version
// does not change package2 `foo` version since already at latest version
expect(
packageJson2.dependencies && packageJson2.dependencies.foo
).toStrictEqual('^2.0.0');
// updates the root package `foo` version to the highest version
// updates the root package `foo` version to the latest version
expect(
packageJsonRoot.devDependencies && packageJsonRoot.devDependencies.foo
).toStrictEqual('^2.0.0');
Expand All @@ -355,34 +355,34 @@ describe('Utils | dependency-versions', function () {
).toStrictEqual('invalidVersion');

// a.b.c
// updates the package1 `a.b.c` version to the highest version
// updates the package1 `a.b.c` version to the latest version
expect(
packageJson1.dependencies && packageJson1.dependencies['a.b.c']
).toStrictEqual('~5.5.0');
// does not change package2 `a.b.c` version since already at highest version
// does not change package2 `a.b.c` version since already at latest version
expect(
packageJson2.dependencies && packageJson2.dependencies['a.b.c']
).toStrictEqual('~5.5.0');

// one.two.three
// does not change package1 `one.two.three` version since already at highest version
// does not change package1 `one.two.three` version since already at latest version
expect(
packageJson1.devDependencies &&
packageJson1.devDependencies['one.two.three']
).toStrictEqual('^4.1.0');
// updates the package2 `one.two.three` version to the highest version
// updates the package2 `one.two.three` version to the latest version
expect(
packageJson2.devDependencies &&
packageJson2.devDependencies['one.two.three']
).toStrictEqual('^4.1.0');

// @types/one
// does not change package1 `@types/one` version since already at highest version
// does not change package1 `@types/one` version since already at latest version
expect(
packageJson1.devDependencies &&
packageJson1.devDependencies['@types/one']
).toStrictEqual('1.0.1');
// updates the package2 `@types/one` version to the highest version
// updates the package2 `@types/one` version to the latest version
expect(
packageJson2.devDependencies &&
packageJson2.devDependencies['@types/one']
Expand Down Expand Up @@ -548,16 +548,16 @@ describe('Utils | dependency-versions', function () {
});
});

describe('#getHighestVersion', function () {
describe('#getLatestVersion', function () {
it('correctly chooses the higher range', function () {
// Just basic sanity checks to ensure the data is passed through to `compareRanges` which has extensive tests.
expect(getHighestVersion(['1.2.3', '1.2.2'])).toStrictEqual('1.2.3');
expect(getHighestVersion(['1.2.2', '1.2.3'])).toStrictEqual('1.2.3');
expect(getLatestVersion(['1.2.3', '1.2.2'])).toStrictEqual('1.2.3');
expect(getLatestVersion(['1.2.2', '1.2.3'])).toStrictEqual('1.2.3');
});

it('throws with invalid version', function () {
expect(() =>
getHighestVersion(['1.2.3', 'foo'])
getLatestVersion(['1.2.3', 'foo'])
).toThrowErrorMatchingInlineSnapshot('"Invalid Version: foo"');
});
});
Expand Down

0 comments on commit 96f93b4

Please sign in to comment.