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 94eb7a2
Show file tree
Hide file tree
Showing 8 changed files with 27 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",
},
"allow-exact-version": {
default: false,
description:
"Determines whether align-deps should allow exact versions in 'package.json' that are within the range and not throw an error for them.",
type: "boolean",
},
};

async function getManifests(
Expand Down Expand Up @@ -169,6 +175,7 @@ async function makeCommand(args: Args): Promise<Command | undefined> {
"set-version": setVersion,
verbose,
write,
"allow-exact-version": allowExactVersion,
} = 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(","),
allowExactVersion,
};

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

const allChanges = diff(manifest, updatedManifest);
const mode = options.allowExactVersion ? "allow-exact-version" : "exact";
const allChanges = diff(manifest, updatedManifest, mode);
if (allChanges) {
if (options.write) {
// The config object may be passed to other commands, so we need to
Expand Down
9 changes: 7 additions & 2 deletions packages/align-deps/src/diff.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
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 function diff(
manifest: PackageManifest,
updatedManifest: PackageManifest
updatedManifest: PackageManifest,
mode: "exact" | "allow-exact-version" = "exact"
): 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 +25,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[];
allowExactVersion: boolean;
};

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;
"allow-exact-version": boolean;
};

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,
allowExactVersion: false,
};

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,
allowExactVersion: false,
};

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,
allowExactVersion: false,
};

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: "--allow-exact-version",
description: cliOptions["allow-exact-version"].description,
},
],
};

0 comments on commit 94eb7a2

Please sign in to comment.