Skip to content

Commit

Permalink
feat(groups): output groups in order they're defined
Browse files Browse the repository at this point in the history
Closes #120
  • Loading branch information
JamieMason committed Feb 17, 2023
1 parent 27b2ffe commit 88950f1
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 35 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 @@ -2,7 +2,7 @@ import { isObject, isUndefined } from 'expect-more';
import type { Syncpack } from '../types';

export function fixMismatches(ctx: Syncpack.Ctx): Syncpack.Ctx {
ctx.versionGroups.reverse().forEach((versionGroup) => {
ctx.versionGroups.forEach((versionGroup) => {
const invalidGroups = versionGroup.getInvalidInstanceGroups();

// Nothing to do if there are no mismatches
Expand Down
21 changes: 8 additions & 13 deletions src/bin-lint-semver-ranges/lint-semver-ranges-cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('lintSemverRanges', () => {
const a = normalize('packages/a/package.json');
lintSemverRangesCli(scenario.config, scenario.disk);
expect(scenario.log.mock.calls).toEqual([
[expect.stringMatching(/Default Semver Group/)],
['✘ foo'],
[` 0.1.0 → ~0.1.0 in dependencies of ${a}`],
]);
Expand All @@ -62,21 +63,15 @@ describe('lintSemverRanges', () => {
const a = normalize('packages/a/package.json');
lintSemverRangesCli(scenario.config, scenario.disk);
expect(scenario.log.mock.calls).toEqual([
[
'= Semver Group 1 ===============================================================',
],
['✘ yarn'],
[` ~2.0.0 → 2.0.0 in packageManager of ${a}`],
[
'= Semver Group 2 ===============================================================',
],
['✘ npm'],
[` 7.24.2 → ^7.24.2 in engines.npm of ${a}`],
[
'= Semver Group 3 ===============================================================',
],
[expect.stringMatching(/Semver Group 1/)],
['✘ node'],
[` 16.16.0 → >=16.16.0 in engines.node of ${a}`],
[expect.stringMatching(/Semver Group 2/)],
['✘ npm'],
[` 7.24.2 → ^7.24.2 in engines.npm of ${a}`],
[expect.stringMatching(/Semver Group 3/)],
['✘ yarn'],
[` ~2.0.0 → 2.0.0 in packageManager of ${a}`],
]);
});
});
8 changes: 5 additions & 3 deletions src/bin-lint-semver-ranges/lint-semver-ranges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import * as log from '../lib/log';
import type { Syncpack } from '../types';

export function lintSemverRanges(ctx: Syncpack.Ctx): Syncpack.Ctx {
ctx.semverGroups.reverse().forEach((semverGroup, i) => {
const hasUserGroups = ctx.semverGroups.length > 1;

ctx.semverGroups.forEach((semverGroup, i) => {
// Nothing to do if there are no mismatches
if (!semverGroup.hasMismatches()) return;

Expand All @@ -16,8 +18,8 @@ export function lintSemverRanges(ctx: Syncpack.Ctx): Syncpack.Ctx {

// Log each group which has mismatches
semverGroup.getMismatches().forEach(([name, mismatches]) => {
// Annotate user-defined version groups
if (!semverGroup.isDefault) log.semverGroupHeader(i);
// Annotate each group
hasUserGroups && log.semverGroupHeader(semverGroup, i);

// Log the dependency name
log.invalid(name);
Expand Down
1 change: 1 addition & 0 deletions src/bin-list-mismatches/list-mismatches-cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ describe('listMismatches', () => {
const a = 'packages/a/package.json';
const c = 'packages/c/package.json';
listMismatchesCli(scenario.config, scenario.disk);

expect(scenario.log.mock.calls).toEqual([
[`${ICON.cross} bar 0.3.0 is the highest valid semver version in use`],
[` 0.2.0 in dependencies of ${normalize(a)}`],
Expand Down
8 changes: 5 additions & 3 deletions src/bin-list-mismatches/list-mismatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import * as log from '../lib/log';
import type { Syncpack } from '../types';

export function listMismatches(ctx: Syncpack.Ctx): Syncpack.Ctx {
ctx.versionGroups.reverse().forEach((versionGroup, i) => {
const hasUserGroups = ctx.versionGroups.length > 1;

ctx.versionGroups.forEach((versionGroup, i) => {
const invalidGroups = versionGroup.getInvalidInstanceGroups();

// Nothing to do if there are no mismatches
Expand All @@ -15,8 +17,8 @@ export function listMismatches(ctx: Syncpack.Ctx): Syncpack.Ctx {
// with the correct status code.
ctx.isInvalid = true;

// Annotate user-defined version groups
if (!versionGroup.isDefault) log.versionGroupHeader(i);
// Annotate each group
hasUserGroups && log.versionGroupHeader(versionGroup, i);

// Log the mismatches
invalidGroups.forEach((instanceGroup) => {
Expand Down
6 changes: 4 additions & 2 deletions src/bin-list/list-cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ describe('list', () => {
const scenario = dependencyIsBanned();
listCli(scenario.config, scenario.disk);
expect(scenario.log.mock.calls).toEqual([
['- foo 0.1.0'],
[expect.stringMatching(/Version Group 1/)],
['✘ bar is banned in this version group'],
[expect.stringMatching(/Default Version Group/)],
['- foo 0.1.0'],
]);
expect(scenario.disk.process.exit).toHaveBeenCalledWith(1);
});
Expand All @@ -92,9 +93,10 @@ describe('list', () => {
const scenario = versionIsIgnored();
listCli(scenario.config, scenario.disk);
expect(scenario.log.mock.calls).toEqual([
['- foo 0.1.0'],
[expect.stringMatching(/Version Group 1/)],
['- bar is ignored in this version group'],
[expect.stringMatching(/Default Version Group/)],
['- foo 0.1.0'],
]);
expect(scenario.disk.process.exit).not.toHaveBeenCalled();
});
Expand Down
15 changes: 11 additions & 4 deletions src/bin-list/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ import * as log from '../lib/log';
import type { Syncpack } from '../types';

export function list(ctx: Syncpack.Ctx): Syncpack.Ctx {
ctx.versionGroups.reverse().forEach((versionGroup, i) => {
// Annotate user-defined version groups
if (!versionGroup.isDefault) log.versionGroupHeader(i);
const hasUserGroups = ctx.versionGroups.length > 1;

versionGroup.getAllInstanceGroups().forEach((instanceGroup) => {
ctx.versionGroups.forEach((versionGroup, i) => {
const instanceGroups = versionGroup.getAllInstanceGroups();

// Nothing to do if empty
if (instanceGroups.length === 0) return;

// Annotate each group
hasUserGroups && log.versionGroupHeader(versionGroup, i);

instanceGroups.forEach((instanceGroup) => {
// Record that this project has mismatches, so that eg. the CLI can exit
// with the correct status code.
if (instanceGroup.isInvalid()) ctx.isInvalid = true;
Expand Down
2 changes: 1 addition & 1 deletion src/bin-set-semver-ranges/set-semver-ranges.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Syncpack } from '../types';

export const setSemverRanges = (ctx: Syncpack.Ctx): Syncpack.Ctx => {
ctx.semverGroups.reverse().forEach((semverGroup) => {
ctx.semverGroups.forEach((semverGroup) => {
semverGroup.instances.forEach((instance) => {
instance.setVersion(semverGroup.getExpectedVersion(instance));
});
Expand Down
30 changes: 22 additions & 8 deletions src/lib/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import chalk from 'chalk';
import { isString } from 'expect-more';
import { inspect } from 'util';
import { ICON } from '../constants';
import type { SemverGroup } from '../get-context/get-groups/semver-group';
import type { VersionGroup } from '../get-context/get-groups/version-group';

export function verbose(...values: unknown[]): void {
if (process.env.SYNCPACK_VERBOSE) {
Expand All @@ -24,14 +26,6 @@ export function skip(message: string): void {
console.log(chalk.dim(ICON.skip), chalk.dim(message));
}

export function semverGroupHeader(order: number): void {
console.log(chalk`{dim = Semver Group ${order} ${'='.repeat(63)}}`);
}

export function versionGroupHeader(order: number): void {
console.log(chalk`{dim = Version Group ${order} ${'='.repeat(63)}}`);
}

export function valid(message: string, comment?: string): void {
if (comment) {
console.log(chalk`{dim ${ICON.skip}} ${message} {dim ${comment}}`);
Expand All @@ -47,3 +41,23 @@ export function invalid(message: string, comment?: string): void {
console.log(chalk`{red ${ICON.cross}} ${message}`);
}
}

export function semverGroupHeader(semverGroup: SemverGroup, i: number): void {
logHeader(
semverGroup.isDefault ? 'Default Semver Group' : `Semver Group ${i + 1}`,
);
}

export function versionGroupHeader(
versionGroup: VersionGroup,
i: number,
): void {
logHeader(
versionGroup.isDefault ? 'Default Version Group' : `Version Group ${i + 1}`,
);
}

function logHeader(label: string) {
const lead = `= ${label} `;
console.log(chalk`{blue ${lead}${'='.repeat(80 - lead.length)}}`);
}

0 comments on commit 88950f1

Please sign in to comment.