Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(align-deps) allow exact version within range according to semver… #2984

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion packages/align-deps/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,20 @@ 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 validateDiffMode(mode: string | undefined): DiffMode {
switch (mode) {
case "allow-exact-version":
case "strict":
return mode;
default:
if (mode) {
warn("Unknown diff mode:", mode);
}
return "strict";
}
}

export const cliOptions = {
"exclude-packages": {
Expand Down Expand Up @@ -76,6 +89,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 +188,7 @@ async function makeCommand(args: Args): Promise<Command | undefined> {
"set-version": setVersion,
verbose,
write,
"diff-mode": diffMode,
} = args;

const options = {
Expand All @@ -180,6 +200,7 @@ async function makeCommand(args: Args): Promise<Command | undefined> {
write,
excludePackages: excludePackages?.toString()?.split(","),
requirements: requirements?.toString()?.split(","),
diffMode: validateDiffMode(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";
balazsgerlei marked this conversation as resolved.
Show resolved Hide resolved

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": String(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,
},
],
};