Skip to content

Commit

Permalink
feat(align-deps) allow exact version within range according to semver…
Browse files Browse the repository at this point in the history
…Satisfies
  • Loading branch information
balazsgerlei committed Apr 10, 2024
1 parent 487d867 commit 650df3d
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 4 deletions.
14 changes: 13 additions & 1 deletion packages/align-deps/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import { makeSetVersionCommand } from "./commands/setVersion";
import { defaultConfig } from "./config";
import { printError, printInfo } from "./errors";
import { isString } from "./helpers";
import type { Args, Command } from "./types";
import type { Args, Command, DiffMode } from "./types";

function isDiffMode(mode: string): mode is DiffMode {
return mode === "strict" || mode === "allow-exact-version";
}

export const cliOptions = {
"exclude-packages": {
Expand Down Expand Up @@ -76,6 +80,12 @@ export const cliOptions = {
description: "Writes changes to the specified 'package.json'.",
type: "boolean",
},
"diff-mode": {
default: "strict",
description:
"Determines the diff mode that align-deps uses to check version declarations in 'package.json'. Valid values are 'strict', meaning only strict equality with capability requirements (usually a range) are allowed and 'allow-exact-version' which allows versions that are within the required range, making it possible to declare exact versions.",
choices: ["strict", "allow-exact-version"],
},
};

async function getManifests(
Expand Down Expand Up @@ -169,6 +179,7 @@ async function makeCommand(args: Args): Promise<Command | undefined> {
"set-version": setVersion,
verbose,
write,
"diff-mode": diffMode,
} = args;

const options = {
Expand All @@ -180,6 +191,7 @@ async function makeCommand(args: Args): Promise<Command | undefined> {
write,
excludePackages: excludePackages?.toString()?.split(","),
requirements: requirements?.toString()?.split(","),
diffMode: isDiffMode(diffMode),
};

if (typeof init !== "undefined") {
Expand Down
2 changes: 1 addition & 1 deletion packages/align-deps/src/commands/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function checkPackageManifest(
kitType
);

const allChanges = diff(manifest, updatedManifest);
const allChanges = diff(manifest, updatedManifest, options.diffMode);
if (allChanges) {
if (options.write) {
// The config object may be passed to other commands, so we need to
Expand Down
11 changes: 9 additions & 2 deletions packages/align-deps/src/diff.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { PackageManifest } from "@rnx-kit/tools-node/package";
import { dependencySections } from "./helpers";
import semverSatisfies from "semver/functions/satisfies";
import type { Changes } from "./types";

export type DiffMode = "strict" | "allow-exact-version";

export function diff(
manifest: PackageManifest,
updatedManifest: PackageManifest
updatedManifest: PackageManifest,
mode: DiffMode = "strict"
): Changes | undefined {
const allChanges: Changes = {
dependencies: [],
Expand All @@ -13,6 +17,7 @@ export function diff(
capabilities: [],
};

const allowExactVersion = mode === "allow-exact-version";
const numChanges = dependencySections.reduce((count, section) => {
const changes = allChanges[section];
const currentDeps = manifest[section] ?? {};
Expand All @@ -23,7 +28,9 @@ export function diff(
if (!current) {
changes.push({ type: "added", dependency, target });
} else if (current !== target) {
changes.push({ type: "changed", dependency, target, current });
if (!allowExactVersion || !semverSatisfies(current, target)) {
changes.push({ type: "changed", dependency, target, current });
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/align-deps/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export type Changes = {
capabilities: { type: "unmanaged"; dependency: string; capability: string }[];
};

export type DiffMode = "strict" | "allow-exact-version";

export type Options = {
presets: string[];
loose: boolean;
Expand All @@ -28,6 +30,7 @@ export type Options = {
write: boolean;
excludePackages?: string[];
requirements?: string[];
diffMode?: DiffMode;
};

export type Args = Pick<Options, "loose" | "verbose" | "write"> & {
Expand All @@ -39,6 +42,7 @@ export type Args = Pick<Options, "loose" | "verbose" | "write"> & {
packages?: (string | number)[];
presets?: string | number;
requirements?: string | number;
"diff-mode"?: string;
};

export type DependencyType = "direct" | "development" | "peer";
Expand Down
1 change: 1 addition & 0 deletions packages/align-deps/test/check.app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const defaultOptions = {
noUnmanaged: false,
verbose: false,
write: true,
allowExactVersion: false,
};

function fixturePath(name: string) {
Expand Down
1 change: 1 addition & 0 deletions packages/align-deps/test/initialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const defaultOptions = {
noUnmanaged: false,
verbose: false,
write: false,
diffMode: "strict",
};

describe("initializeConfig()", () => {
Expand Down
1 change: 1 addition & 0 deletions packages/align-deps/test/setVersion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe("makeSetVersionCommand()", () => {
noUnmanaged: false,
verbose: false,
write: false,
diffMode: "strict",
};

afterEach(() => {
Expand Down
1 change: 1 addition & 0 deletions packages/align-deps/test/vigilant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ describe("checkPackageManifestUnconfigured()", () => {
noUnmanaged: false,
verbose: false,
write: false,
diffMode: "strict",
};

beforeEach(() => {
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/align-deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function rnxAlignDeps(
verbose: Boolean(args.verbose),
write: Boolean(args.write),
packages: argv,
"diff-mode": Boolean(args.diffMode),
});
}

Expand Down Expand Up @@ -74,5 +75,9 @@ export const rnxAlignDepsCommand = {
name: "--write",
description: cliOptions.write.description,
},
{
name: `--diff-mode [${cliOptions["diff-mode"].choices?.join("|")}]`,
description: cliOptions["diff-mode"].description,
},
],
};

0 comments on commit 650df3d

Please sign in to comment.