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 Mar 25, 2024
1 parent ebb2c54 commit b424f3e
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 3 deletions.
8 changes: 8 additions & 0 deletions packages/align-deps/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,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 +175,7 @@ async function makeCommand(args: Args): Promise<Command | undefined> {
"set-version": setVersion,
verbose,
write,
"diff-mode": diffMode,
} = args;

const options = {
Expand All @@ -180,6 +187,7 @@ async function makeCommand(args: Args): Promise<Command | undefined> {
write,
excludePackages: excludePackages?.toString()?.split(","),
requirements: requirements?.toString()?.split(","),
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,17 +1,22 @@
import { keysOf } from "@rnx-kit/tools-language";
import type { PackageManifest } from "@rnx-kit/tools-node/package";
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: [],
peerDependencies: [],
devDependencies: [],
};

const allowExactVersion = mode === "allow-exact-version";
const numChanges = keysOf(allChanges).reduce((count, section) => {
const changes = allChanges[section];
const currentDeps = manifest[section] ?? {};
Expand All @@ -22,7 +27,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
2 changes: 2 additions & 0 deletions packages/align-deps/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type Options = {
write: boolean;
excludePackages?: string[];
requirements?: string[];
diffMode?: string;
};

export type Args = Pick<Options, "loose" | "verbose" | "write"> & {
Expand All @@ -38,6 +39,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/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 @@ -301,6 +301,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,
"allow-exact-version": Boolean(args.allowExactVersion),
});
}

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 b424f3e

Please sign in to comment.