Skip to content

Commit

Permalink
refactor(versions): parse specifiers with npm-package-arg
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMason committed Jul 30, 2023
1 parent 4474a73 commit ce58e3a
Show file tree
Hide file tree
Showing 51 changed files with 709 additions and 315 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"fs-extra": "11.1.1",
"globby": "11.1.0",
"minimatch": "9.0.2",
"npm-package-arg": "10.1.0",
"ora": "5.4.1",
"prompts": "2.4.2",
"read-yaml-file": "2.1.0",
Expand All @@ -54,6 +55,7 @@
"@types/fs-extra": "11.0.1",
"@types/jest": "29.5.2",
"@types/node": "14.18.36",
"@types/npm-package-arg": "6.1.1",
"@types/prompts": "2.4.4",
"@types/semver": "7.5.0",
"@typescript-eslint/eslint-plugin": "5.60.1",
Expand Down
12 changes: 6 additions & 6 deletions src/bin-fix-mismatches/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ export const fixMismatchesEffects: VersionEffects<void> = {
onSnappedToMismatch(input) {
return Effect.sync(() => setVersions(input));
},
onUnsupportedMismatch(input) {
return Effect.sync(() => pipe(input, logHeader, logUnsupportedMismatch));
onNonSemverMismatch(input) {
return Effect.sync(() => pipe(input, logHeader, logNonSemverMismatch));
},
onWorkspaceMismatch(input) {
onLocalPackageMismatch(input) {
return Effect.sync(() => setVersions(input));
},
onComplete(ctx) {
Expand All @@ -59,13 +59,13 @@ function logHeader<T extends VersionGroupReport.Any>(input: Input<T>) {

function setVersions({ report }: Input<VersionGroupReport.FixableCases>) {
report.instances.forEach((instance) => {
instance.setVersion(report.expectedVersion);
instance.setSpecifier(report.expectedVersion);
});
}

function removeVersions({ report }: Input<VersionGroupReport.Banned>) {
report.instances.forEach((instance) => {
instance.setVersion(DELETE);
instance.setSpecifier(DELETE);
});
}

Expand All @@ -79,7 +79,7 @@ function logSameRangeMismatch({ ctx, report }: Input<VersionGroupReport.SameRang
);
}

function logUnsupportedMismatch({ ctx, report }: Input<VersionGroupReport.UnsupportedMismatch>) {
function logNonSemverMismatch({ ctx, report }: Input<VersionGroupReport.NonSemverMismatch>) {
ctx.isInvalid = true;
console.log(
chalk`{yellow %s %s} {dim has mismatched unsupported versions which syncpack cannot auto fix}%s`,
Expand Down
12 changes: 6 additions & 6 deletions src/bin-lint-semver-ranges/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export const lintSemverRangesEffects: SemverRangeEffects<void> = {
onSemverRangeMismatch(input) {
return Effect.sync(() => pipe(input, logHeader, logRangeMismatch));
},
onUnsupportedVersion(input) {
return Effect.sync(() => pipe(input, logHeader, logUnsupportedVersion));
onNonSemverVersion(input) {
return Effect.sync(() => pipe(input, logHeader, logNonSemverVersion));
},
onWorkspaceSemverRangeMismatch(input) {
onLocalPackageSemverRangeMismatch(input) {
return Effect.sync(() => pipe(input, logHeader, logRangeMismatch));
},
onComplete() {
Expand All @@ -46,19 +46,19 @@ function logRangeMismatch({ report, ctx }: Input<SemverGroupReport.FixableCases>
chalk`{red %s} %s {red %s} %s {green %s} {dim in %s of %s}`,
ICON.cross,
report.name,
report.instance.version,
report.instance.specifier,
ICON.rightArrow,
report.expectedVersion,
report.instance.strategy.path,
report.instance.packageJsonFile.shortPath,
);
}

function logUnsupportedVersion({ report }: Input<SemverGroupReport.UnsupportedVersion>) {
function logNonSemverVersion({ report }: Input<SemverGroupReport.NonSemverVersion>) {
console.log(
chalk`{yellow %s} %s {yellow %s} {dim ignored as a format which syncpack cannot apply semver ranges to}`,
ICON.panic,
report.name,
report.instance.version,
report.instance.specifier,
);
}
36 changes: 18 additions & 18 deletions src/bin-list-mismatches/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ export const listMismatchesEffects: VersionEffects<void> = {
onSnappedToMismatch(input) {
return Effect.sync(() => pipe(input, logHeader, logSnappedToMismatch));
},
onUnsupportedMismatch(input) {
return Effect.sync(() => pipe(input, logHeader, logUnsupportedMismatch));
onNonSemverMismatch(input) {
return Effect.sync(() => pipe(input, logHeader, logNonSemverMismatch));
},
onWorkspaceMismatch(input) {
return Effect.sync(() => pipe(input, logHeader, logWorkspaceMismatch));
onLocalPackageMismatch(input) {
return Effect.sync(() => pipe(input, logHeader, logLocalPackageMismatch));
},
onComplete() {
return Effect.unit();
Expand All @@ -59,7 +59,7 @@ function logBanned({ report, ctx }: Input<VersionGroupReport.Banned>) {
report.instances.forEach((instance) => {
console.log(
chalk` {red %s} {dim in %s of %s}`,
instance.version,
instance.specifier,
instance.strategy.path,
instance.packageJsonFile.shortPath,
);
Expand All @@ -79,10 +79,10 @@ function logHighLowSemverMismatch({
report._tag === 'LowestSemverMismatch' ? 'lowest' : 'highest',
);
report.instances.forEach((instance) => {
if (instance.version !== report.expectedVersion) {
if (instance.specifier !== report.expectedVersion) {
console.log(
chalk` {red %s} {dim in %s of %s}`,
instance.version,
instance.specifier,
instance.strategy.path,
instance.packageJsonFile.shortPath,
);
Expand All @@ -99,10 +99,10 @@ function logPinnedMismatch({ report, ctx }: Input<VersionGroupReport.PinnedMisma
report.expectedVersion,
);
report.instances.forEach((instance) => {
if (instance.version !== report.expectedVersion) {
if (instance.specifier !== report.expectedVersion) {
console.log(
chalk` {red %s} {dim in %s of %s}`,
instance.version,
instance.specifier,
instance.strategy.path,
instance.packageJsonFile.shortPath,
);
Expand All @@ -120,10 +120,10 @@ function logSnappedToMismatch({ report, ctx }: Input<VersionGroupReport.SnappedT
report.snapTo.join(' || '),
);
report.instances.forEach((instance) => {
if (instance.version !== report.expectedVersion) {
if (instance.specifier !== report.expectedVersion) {
console.log(
chalk` {red %s} {dim in %s of %s}`,
instance.version,
instance.specifier,
instance.strategy.path,
instance.packageJsonFile.shortPath,
);
Expand All @@ -142,14 +142,14 @@ function logSameRangeMismatch({ report, ctx }: Input<VersionGroupReport.SameRang
report.instances.forEach((instance) => {
console.log(
chalk` {yellow %s} {dim in %s of %s}`,
instance.version,
instance.specifier,
instance.strategy.path,
instance.packageJsonFile.shortPath,
);
});
}

function logUnsupportedMismatch({ report, ctx }: Input<VersionGroupReport.UnsupportedMismatch>) {
function logNonSemverMismatch({ report, ctx }: Input<VersionGroupReport.NonSemverMismatch>) {
ctx.isInvalid = true;
console.log(
chalk`{yellow %s %s} {dim has mismatched unsupported versions which syncpack cannot auto fix}%s`,
Expand All @@ -160,27 +160,27 @@ function logUnsupportedMismatch({ report, ctx }: Input<VersionGroupReport.Unsupp
report.instances.forEach((instance) => {
console.log(
chalk` {yellow %s} {dim in %s of %s}`,
instance.version,
instance.specifier,
instance.strategy.path,
instance.packageJsonFile.shortPath,
);
});
}

function logWorkspaceMismatch({ report, ctx }: Input<VersionGroupReport.WorkspaceMismatch>) {
function logLocalPackageMismatch({ report, ctx }: Input<VersionGroupReport.LocalPackageMismatch>) {
ctx.isInvalid = true;
console.log(
chalk`{red %s} %s {green %s} {dim is developed in this repo at %s}`,
ICON.cross,
report.name,
report.expectedVersion,
report.workspaceInstance.packageJsonFile.shortPath,
report.localPackageInstance.packageJsonFile.shortPath,
);
report.instances.forEach((instance) => {
if (instance.version !== report.expectedVersion) {
if (instance.specifier !== report.expectedVersion) {
console.log(
chalk` {red %s} {dim in %s of %s}`,
instance.version,
instance.specifier,
instance.strategy.path,
instance.packageJsonFile.shortPath,
);
Expand Down
29 changes: 16 additions & 13 deletions src/bin-list/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import chalk from 'chalk';
import { uniq } from 'tightrope/array/uniq';
import { ICON } from '../constants';
import type { VersionEffectInput as Input, VersionEffects } from '../create-program/effects';
import type { Instance } from '../get-package-json-files/instance';
import type { VersionGroupReport } from '../get-version-groups';
import { getUniqueVersions } from '../get-version-groups/lib/get-unique-versions';
import { isSupported } from '../guards/is-supported';
import { getUniqueSpecifiers } from '../get-version-groups/lib/get-unique-specifiers';
import type { Instance } from '../instance';
import { logGroupHeader } from '../lib/log-group-header';

export const listEffects: VersionEffects<void> = {
Expand Down Expand Up @@ -38,10 +37,10 @@ export const listEffects: VersionEffects<void> = {
onSnappedToMismatch(input) {
return Effect.sync(() => pipe(input, logHeader, logFixableMismatch));
},
onUnsupportedMismatch(input) {
onNonSemverMismatch(input) {
return Effect.sync(() => pipe(input, logHeader, logUnfixableMismatch));
},
onWorkspaceMismatch(input) {
onLocalPackageMismatch(input) {
return Effect.sync(() => pipe(input, logHeader, logFixableMismatch));
},
onComplete() {
Expand All @@ -62,7 +61,7 @@ function logFixableMismatch<T extends VersionGroupReport.FixableCases>({ report,
chalk`{red %s %s} %s`,
ICON.cross,
report.name,
listColouredVersions(report.expectedVersion, report.instances),
listColouredSpecifiers(report.expectedVersion, report.instances),
);
}

Expand All @@ -75,8 +74,12 @@ function logUnfixableMismatch<T extends VersionGroupReport.UnfixableCases>({
chalk`{red %s %s} %s`,
ICON.cross,
report.name,
getUniqueVersions(report.instances)
.map((version) => (isSupported(version) ? chalk.red(version) : chalk.yellow(version)))
getUniqueSpecifiers(report.instances)
.map((instance) =>
instance.getSemverSpecifier() !== null
? chalk.red(instance.specifier)
: chalk.yellow(instance.specifier),
)
.join(chalk.dim(', ')),
);
}
Expand All @@ -95,11 +98,11 @@ function logIgnored({ report }: Input<VersionGroupReport.Ignored>) {
}

function logValid({ report }: Input<VersionGroupReport.Valid>) {
console.log(chalk`{dim -} {white %s} {dim %s}`, report.name, report.instances?.[0]?.version);
console.log(chalk`{dim -} {white %s} {dim %s}`, report.name, report.instances?.[0]?.specifier);
}

function listColouredVersions(pinVersion: string, instances: Instance[]) {
return getAllVersions(pinVersion, instances)
function listColouredSpecifiers(pinVersion: string, instances: Instance.Any[]) {
return getAllSpecifiers(pinVersion, instances)
.map((version) => withColour(pinVersion, version))
.join(chalk.dim(', '));
}
Expand All @@ -108,6 +111,6 @@ function withColour(pinVersion: string, version: string) {
return version === pinVersion ? chalk.green(version) : chalk.red(version);
}

function getAllVersions(pinVersion: string, instances: Instance[]) {
return uniq([pinVersion].concat(instances.map((i) => i.version)));
function getAllSpecifiers(pinVersion: string, instances: Instance.Any[]) {
return uniq([pinVersion].concat(instances.map((i) => i.specifier)));
}
10 changes: 5 additions & 5 deletions src/bin-prompt/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ICON } from '../constants';
import type { VersionEffectInput as Input, VersionEffects } from '../create-program/effects';
import { EnvTag } from '../env/tags';
import type { VersionGroupReport } from '../get-version-groups';
import { getUniqueVersions } from '../get-version-groups/lib/get-unique-versions';
import { getUniqueSpecifiers } from '../get-version-groups/lib/get-unique-specifiers';
import { logGroupHeader } from '../lib/log-group-header';

export const promptEffects: VersionEffects<void> = {
Expand Down Expand Up @@ -39,13 +39,13 @@ export const promptEffects: VersionEffects<void> = {
onSnappedToMismatch() {
return Effect.unit();
},
onUnsupportedMismatch(input) {
onNonSemverMismatch(input) {
return pipe(
Effect.sync(() => logHeader(input)),
Effect.flatMap(askForNextVersion),
);
},
onWorkspaceMismatch() {
onLocalPackageMismatch() {
return Effect.unit();
},
onComplete() {
Expand All @@ -69,7 +69,7 @@ function askForNextVersion({ report }: Input<VersionGroupReport.UnfixableCases>)
const choice = yield* $(
env.askForChoice({
message: chalk`${report.name} {dim Choose a version to replace the others}`,
choices: [...getUniqueVersions(report.instances), OTHER, SKIP],
choices: [...getUniqueSpecifiers(report.instances).map((i) => i.specifier), OTHER, SKIP],
}),
);
if (choice === SKIP) return;
Expand All @@ -84,7 +84,7 @@ function askForNextVersion({ report }: Input<VersionGroupReport.UnfixableCases>)
yield* $(
Effect.sync(() => {
report.instances.forEach((instance) => {
instance.setVersion(nextVersion);
instance.setSpecifier(nextVersion);
});
}),
);
Expand Down
12 changes: 6 additions & 6 deletions src/bin-set-semver-ranges/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export const setSemverRangesEffects: SemverRangeEffects<void> = {
onSemverRangeMismatch(input) {
return Effect.sync(() => setVersions(input));
},
onUnsupportedVersion(input) {
return Effect.sync(() => logUnsupportedVersion(input));
onNonSemverVersion(input) {
return Effect.sync(() => logNonSemverVersion(input));
},
onWorkspaceSemverRangeMismatch(input) {
onLocalPackageSemverRangeMismatch(input) {
return Effect.sync(() => setVersions(input));
},
onComplete() {
Expand All @@ -32,14 +32,14 @@ export const setSemverRangesEffects: SemverRangeEffects<void> = {
};

function setVersions({ report }: Input<SemverGroupReport.FixableCases>) {
report.instance.setVersion(report.expectedVersion);
report.instance.setSpecifier(report.expectedVersion);
}

function logUnsupportedVersion({ report }: Input<SemverGroupReport.UnsupportedVersion>) {
function logNonSemverVersion({ report }: Input<SemverGroupReport.NonSemverVersion>) {
console.log(
chalk`{yellow %s} %s {yellow %s} {dim ignored as a format which syncpack cannot apply semver ranges to}`,
ICON.panic,
report.name,
report.instance.version,
report.instance.specifier,
);
}
14 changes: 8 additions & 6 deletions src/bin-update/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ICON } from '../constants';
import type { VersionEffectInput as Input, VersionEffects } from '../create-program/effects';
import type { VersionGroupReport } from '../get-version-groups';
import { getHighestVersion } from '../get-version-groups/lib/get-highest-version';
import { getUniqueVersions } from '../get-version-groups/lib/get-unique-versions';
import { getUniqueSpecifiers } from '../get-version-groups/lib/get-unique-specifiers';
import { logVerbose } from '../lib/log-verbose';
import { getSemverRange, setSemverRange } from '../lib/set-semver-range';

Expand Down Expand Up @@ -57,10 +57,10 @@ export const updateEffects: VersionEffects<InputWithVersions | void> = {
onSnappedToMismatch() {
return Effect.unit();
},
onUnsupportedMismatch() {
onNonSemverMismatch() {
return Effect.unit();
},
onWorkspaceMismatch() {
onLocalPackageMismatch() {
return Effect.unit();
},
onComplete(ctx, results) {
Expand Down Expand Up @@ -131,7 +131,9 @@ function promptForUpdates(results: Array<InputWithVersions | void>) {

const input = result.input;
const latestVersion = result.versions.latest;
const uniqueVersions = getUniqueVersions(input.report.instances);
const uniqueVersions = getUniqueSpecifiers(input.report.instances).map(
(i) => i.specifier,
);
const highestVersion = unwrap(getHighestVersion(uniqueVersions));
const exactHighestVersion = setSemverRange('', highestVersion);

Expand Down Expand Up @@ -177,9 +179,9 @@ function promptForUpdates(results: Array<InputWithVersions | void>) {
Effect.sync(() => {
chosenUpdates.forEach(({ input, versions }) => {
input.report.instances.forEach((instance) => {
const semverRange = getSemverRange(instance.version);
const semverRange = getSemverRange(instance.specifier);
const latestWithRange = setSemverRange(semverRange, versions.latest);
instance.setVersion(latestWithRange);
instance.setSpecifier(latestWithRange);
});
});
}),
Expand Down

0 comments on commit ce58e3a

Please sign in to comment.