Skip to content

Commit

Permalink
refactor(filter): filter packages in one place
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMason committed Jan 3, 2022
1 parent ea59d4b commit c64a109
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 51 deletions.
5 changes: 1 addition & 4 deletions src/bin-fix-mismatches/fix-mismatches.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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 { getExpectedVersion } from './get-expected-version';

Expand All @@ -15,8 +13,7 @@ export function fixMismatches(input: ProgramInput, disk: Disk): void {
*/
input.instances.versionGroups.reverse().forEach((versionGroup, i) => {
const isVersionGroup = i > 0;
const filtered = versionGroup.instances.filter(matchesFilter(input));
const groups = listVersionGroups(filtered);
const groups = listVersionGroups(versionGroup);

if (isVersionGroup) {
console.log(chalk`{dim = Version Group ${i} ${'='.repeat(63)}}`);
Expand Down
6 changes: 1 addition & 5 deletions src/bin-lint-semver-ranges/lint-semver-ranges.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import chalk from 'chalk';
import type { ProgramInput } from '../lib/get-input';
import { matchesFilter } from '../lib/matches-filter';
import { setSemverRange } from '../lib/set-semver-range';
import { listSemverGroupMismatches } from './list-semver-group-mismatches';

Expand All @@ -14,10 +13,7 @@ export function lintSemverRanges(input: ProgramInput): void {
*/
input.instances.semverGroups.reverse().forEach((semverGroup, i) => {
const isSemverGroup = i > 0;
const mismatches = listSemverGroupMismatches({
...semverGroup,
instances: semverGroup.instances.filter(matchesFilter(input)),
});
const mismatches = listSemverGroupMismatches(semverGroup);

if (isSemverGroup) {
console.log(chalk`{dim = Semver Group ${i} ${'='.repeat(63)}}`);
Expand Down
6 changes: 2 additions & 4 deletions src/bin-list-mismatches/list-mismatches.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import chalk from 'chalk';
import { listVersionGroups } from '../bin-list/list-version-groups';
import type { ProgramInput } from '../lib/get-input';
import { matchesFilter } from '../lib/matches-filter';

export function listMismatches(input: ProgramInput): void {
const isInvalid = false;
Expand All @@ -13,14 +12,13 @@ export function listMismatches(input: ProgramInput): void {
*/
input.instances.versionGroups.reverse().forEach((versionGroup, i) => {
const isVersionGroup = i > 0;
const filtered = versionGroup.instances.filter(matchesFilter(input));
const ungrouped = listVersionGroups(filtered);
const groups = listVersionGroups(versionGroup);

if (isVersionGroup) {
console.log(chalk`{dim = Version Group ${i} ${'='.repeat(63)}}`);
}

ungrouped.forEach(({ hasMismatches, instances, name }) => {
groups.forEach(({ hasMismatches, instances, name }) => {
if (hasMismatches) {
console.log(chalk`{red ✕ ${name}}`);
instances.forEach(({ dependencyType, version, wrapper }) => {
Expand Down
20 changes: 12 additions & 8 deletions src/bin-list/list-version-groups.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { listVersionGroups } from './list-version-groups';
describe('listVersionGroups', () => {
it('returns a sorted list of every dependency in the project', () => {
expect(
listVersionGroups([
{ name: 'foo', version: '1.0.0' },
{ name: 'bar', version: '0.5.0' },
] as any),
listVersionGroups({
instances: [
{ name: 'foo', version: '1.0.0' },
{ name: 'bar', version: '0.5.0' },
],
} as any),
).toEqual([
{
hasMismatches: false,
Expand All @@ -24,10 +26,12 @@ describe('listVersionGroups', () => {
});
it('recognises mismatched dependency versions', () => {
expect(
listVersionGroups([
{ name: 'foo', version: '1.0.0' },
{ name: 'foo', version: '1.1.0' },
] as any),
listVersionGroups({
instances: [
{ name: 'foo', version: '1.0.0' },
{ name: 'foo', version: '1.1.0' },
],
} as any),
).toEqual([
{
hasMismatches: true,
Expand Down
22 changes: 18 additions & 4 deletions src/bin-list/list-version-groups.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import type { Instance } from '../lib/get-input/get-instances';
import type {
IndexedVersionGroup,
Instance,
} from '../lib/get-input/get-instances';
import { groupBy } from '../lib/group-by';
import { sortByName } from '../lib/sort-by-name';
import type { ListItem } from './list';

export function listVersionGroups(instances: Instance[]): ListItem[] {
const instancesByName = groupBy<Instance>('name', instances.sort(sortByName));
interface ListItem {
hasMismatches: boolean;
instances: Instance[];
name: string;
uniques: string[];
}

export function listVersionGroups(
versionGroup: IndexedVersionGroup,
): ListItem[] {
const instancesByName = groupBy<Instance>(
'name',
versionGroup.instances.sort(sortByName),
);
return Object.entries(instancesByName).map(([name, instances]) => {
const versions = instances.map(({ version }) => version);
const uniques = Array.from(new Set(versions));
Expand Down
16 changes: 3 additions & 13 deletions src/bin-list/list.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import chalk from 'chalk';
import type { ProgramInput } from '../lib/get-input';
import type { Instance } from '../lib/get-input/get-instances';
import { matchesFilter } from '../lib/matches-filter';
import { listVersionGroups } from './list-version-groups';

export interface ListItem {
hasMismatches: boolean;
instances: Instance[];
name: string;
uniques: string[];
}

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

Expand All @@ -21,14 +12,13 @@ export function list(input: ProgramInput): void {
*/
input.instances.versionGroups.reverse().forEach((versionGroup, i) => {
const isVersionGroup = i > 0;
const filtered = versionGroup.instances.filter(matchesFilter(input));
const ungrouped = listVersionGroups(filtered);
const groups = listVersionGroups(versionGroup);

if (isVersionGroup) {
console.log(chalk`{dim = Version Group ${i} ${'='.repeat(63)}}`);
}

ungrouped.forEach(({ hasMismatches, name, uniques }) => {
groups.forEach(({ hasMismatches, name, uniques }) => {
const versionList = uniques.sort().join(', ');
console.log(
hasMismatches
Expand All @@ -37,7 +27,7 @@ export function list(input: ProgramInput): void {
);
});

if (ungrouped.some(({ hasMismatches }) => hasMismatches)) {
if (groups.some(({ hasMismatches }) => hasMismatches)) {
isInvalid = true;
}
});
Expand Down
6 changes: 1 addition & 5 deletions src/bin-set-semver-ranges/set-semver-ranges.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { listSemverGroupMismatches } from '../bin-lint-semver-ranges/list-semver-group-mismatches';
import type { Disk } from '../lib/disk';
import type { ProgramInput } from '../lib/get-input';
import { matchesFilter } from '../lib/matches-filter';
import { setSemverRange } from '../lib/set-semver-range';
import { writeIfChanged } from '../lib/write-if-changed';

export const setSemverRanges = (input: ProgramInput, disk: Disk): void => {
input.instances.semverGroups.reverse().forEach((semverGroup) => {
const mismatches = listSemverGroupMismatches({
...semverGroup,
instances: semverGroup.instances.filter(matchesFilter(input)),
});
const mismatches = listSemverGroupMismatches(semverGroup);
mismatches.forEach(({ dependencyType, name, version, wrapper }) => {
const root: any = wrapper.contents;
root[dependencyType][name] = setSemverRange(semverGroup.range, version);
Expand Down
1 change: 1 addition & 0 deletions src/lib/get-input/get-instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function getInstances(
const pkgs = Object.entries(versionsByName);
for (const [name, version] of pkgs) {
if (!isNonEmptyString(name)) continue;
if (name.search(new RegExp(options.filter)) === -1) continue;
if (!isNonEmptyString(version)) continue;
const instance = { dependencyType, name, version, wrapper };
allInstances.all.push(instance);
Expand Down
8 changes: 0 additions & 8 deletions src/lib/matches-filter.ts

This file was deleted.

0 comments on commit c64a109

Please sign in to comment.