Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bright-balloons-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rnx-kit/dep-check": minor
---

All dependencies of all packages, including configured ones, should be checked when `--vigilant` is specified.
44 changes: 36 additions & 8 deletions packages/dep-check/src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { findBadPackages } from "./findBadPackages";
import { modifyManifest } from "./helpers";
import { updatePackageManifest } from "./manifest";
import { getProfilesFor, resolveCustomProfiles } from "./profiles";
import type { CheckOptions, Command } from "./types";
import type { CheckConfig, CheckOptions, Command } from "./types";

export function checkPackageManifest(
export function getCheckConfig(
manifestPath: string,
{ loose, uncheckedReturnCode = 0, write }: CheckOptions
): number {
{ loose, uncheckedReturnCode = 0 }: CheckOptions
): number | CheckConfig {
const manifest = readPackage(manifestPath);
if (!isPackageManifest(manifest)) {
error(`'${manifestPath}' does not contain a valid package manifest`);
Expand Down Expand Up @@ -58,13 +58,41 @@ export function checkPackageManifest(
);
requiredCapabilities.push(...targetCapabilities);

if (requiredCapabilities.length === 0) {
return uncheckedReturnCode;
return {
kitType,
reactNativeVersion,
reactNativeDevVersion,
capabilities: requiredCapabilities,
customProfilesPath,
manifest,
};
}

export function checkPackageManifest(
manifestPath: string,
options: CheckOptions
): number {
const result = options.config || getCheckConfig(manifestPath, options);
if (typeof result === "number") {
return result;
}

const {
kitType,
reactNativeVersion,
reactNativeDevVersion,
capabilities,
customProfilesPath,
manifest,
} = result;

if (capabilities.length === 0) {
return options.uncheckedReturnCode || 0;
}

const updatedManifest = updatePackageManifest(
manifest,
requiredCapabilities,
capabilities,
getProfilesFor(reactNativeVersion, customProfilesPath),
getProfilesFor(reactNativeDevVersion, customProfilesPath),
kitType
Expand All @@ -75,7 +103,7 @@ export function checkPackageManifest(
const normalizedManifestJson = JSON.stringify(manifest, undefined, 2);

if (updatedManifestJson !== normalizedManifestJson) {
if (write) {
if (options.write) {
modifyManifest(manifestPath, updatedManifest);
} else {
const diff = diffLinesUnified(
Expand Down
10 changes: 10 additions & 0 deletions packages/dep-check/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@ export type Args = Options & {
vigilant?: string | number;
};

export type CheckConfig = {
kitType: KitType;
reactNativeVersion: string;
reactNativeDevVersion: string;
capabilities: Capability[];
customProfilesPath?: string;
manifest: PackageManifest;
};

export type CheckOptions = Options & {
uncheckedReturnCode?: number;
config?: number | CheckConfig;
};

export type VigilantOptions = Options & {
Expand Down
81 changes: 63 additions & 18 deletions packages/dep-check/src/vigilant.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Capability } from "@rnx-kit/config";
import { error } from "@rnx-kit/console";
import { PackageManifest, readPackage } from "@rnx-kit/tools-node/package";
import isString from "lodash/isString";
import { resolveCapabilities } from "./capabilities";
import { checkPackageManifest } from "./check";
import { checkPackageManifest, getCheckConfig } from "./check";
import { keysOf, modifyManifest } from "./helpers";
import { updateDependencies } from "./manifest";
import { parseProfilesString } from "./profiles";
Expand All @@ -21,39 +22,85 @@ const allSections = [
"devDependencies" as const,
];

export function removeManagedDependencies(
dependencies: Record<string, string>,
managed: string[]
): Record<string, string> {
managed.forEach((name) => delete dependencies[name]);
return dependencies;
}

/**
* Builds a profile targeting specified versions.
* @param versions Supported versions
* @param customProfilesPath Path to custom profiles
* @param managedCapabilities Capabilities that are already managed and can be skipped
* @returns A profile containing dependencies to compare against
*/
export function buildManifestProfile(
versions: string,
customProfilesPath: string | undefined
customProfilesPath: string | undefined,
managedCapabilities: Capability[] = []
): ManifestProfile {
const { supportedProfiles, targetProfile } = parseProfilesString(
versions,
customProfilesPath
);

const allCapabilities = keysOf(targetProfile[0]);
const managedDependencies = Object.keys(
resolveCapabilities(managedCapabilities, targetProfile)
);

// Use "development" type so we can check for devOnly packages under
// `dependencies` as well.
const directDependencies = updateDependencies(
{},
resolveCapabilities(allCapabilities, targetProfile),
"development"
const directDependencies = removeManagedDependencies(
updateDependencies(
{},
resolveCapabilities(allCapabilities, targetProfile),
"development"
),
managedDependencies
);

const { name, version } = require("../package.json");
return {
name,
version,
dependencies: directDependencies,
peerDependencies: updateDependencies(
{},
resolveCapabilities(allCapabilities, supportedProfiles),
"peer"
peerDependencies: removeManagedDependencies(
updateDependencies(
{},
resolveCapabilities(allCapabilities, supportedProfiles),
"peer"
),
managedDependencies
),
devDependencies: directDependencies,
};
}

export function buildProfileFromConfig(
config: ReturnType<typeof getCheckConfig>,
defaultProfile: ManifestProfile
): ManifestProfile {
if (typeof config === "number") {
return defaultProfile;
}

const {
capabilities,
customProfilesPath,
reactNativeDevVersion,
reactNativeVersion,
} = config;

const supportedVersions = reactNativeVersion.replace(/ [|]{2} /g, ",");
const versions = `${reactNativeDevVersion},${supportedVersions}`;

return buildManifestProfile(versions, customProfilesPath, capabilities);
}

export function inspect(
manifest: PackageManifest,
profile: ManifestProfile,
Expand Down Expand Up @@ -95,20 +142,17 @@ export function makeVigilantCommand({
return undefined;
}

const uncheckedReturnCode = -1;
const checkOptions = { loose, uncheckedReturnCode, write };
const checkOptions = { loose, write };

const exclusionList = isString(excludePackages)
? excludePackages.split(",")
: [];

const profile = buildManifestProfile(versions, customProfiles);
const inputProfile = buildManifestProfile(versions, customProfiles);
return (manifestPath: string) => {
const config = getCheckConfig(manifestPath, checkOptions);
try {
const checkReturnCode = checkPackageManifest(manifestPath, checkOptions);
if (checkReturnCode !== uncheckedReturnCode) {
return checkReturnCode;
}
checkPackageManifest(manifestPath, { ...checkOptions, config });
} catch (_) {
// Ignore; retry with a full inspection
}
Expand All @@ -118,7 +162,8 @@ export function makeVigilantCommand({
return 0;
}

const changes = inspect(manifest, profile, write);
const currentProfile = buildProfileFromConfig(config, inputProfile);
const changes = inspect(manifest, currentProfile, write);
if (changes.length > 0) {
if (write) {
modifyManifest(manifestPath, manifest);
Expand Down
80 changes: 40 additions & 40 deletions packages/dep-check/test/__snapshots__/vigilant.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`buildManifestProfile() builds a package manifest for a multiple profile versions 1`] = `
exports[`buildManifestProfile() builds a package manifest for a single profile version 1`] = `
Object {
"dependencies": Object {
"@react-native-async-storage/async-storage": "^1.15.8",
Expand Down Expand Up @@ -90,42 +90,40 @@ Object {
"peerDependencies": Object {
"@react-native-async-storage/async-storage": "^1.15.8",
"@react-native-clipboard/clipboard": "^1.8.3",
"@react-native-community/async-storage": "^1.12.1",
"@react-native-community/checkbox": "^0.5.7 || ^0.5.8",
"@react-native-community/clipboard": "^1.5.1",
"@react-native-community/datetimepicker": "^3.0.9 || ^3.4.6",
"@react-native-community/checkbox": "^0.5.8",
"@react-native-community/datetimepicker": "^3.4.6",
"@react-native-community/hooks": "^2.6.0",
"@react-native-community/netinfo": "^5.9.10 || ^6.0.2",
"@react-native-community/netinfo": "^6.0.2",
"@react-native-masked-view/masked-view": "^0.2.4",
"@react-navigation/native": "^5.9.4 || ^5.9.8",
"@react-navigation/stack": "^5.14.4 || ^5.14.9",
"hermes-engine": "~0.5.0 || ~0.7.0",
"react": "16.13.1 || 17.0.1",
"react-dom": "16.13.1 || 17.0.1",
"react-native": "^0.63.2 || ^0.64.2",
"@react-navigation/native": "^5.9.8",
"@react-navigation/stack": "^5.14.9",
"hermes-engine": "~0.7.0",
"react": "17.0.1",
"react-dom": "17.0.1",
"react-native": "^0.64.2",
"react-native-base64": "^0.2.1",
"react-native-floating-action": "^1.21.0",
"react-native-fs": "^2.16.6 || ^2.17.0",
"react-native-fs": "^2.17.0",
"react-native-gesture-handler": "^1.10.3",
"react-native-lazy-index": "^2.1.1",
"react-native-macos": "^0.63.0 || ^0.64.0",
"react-native-modal": "^11.5.6 || ^11.10.0",
"react-native-popover-view": "^3.1.1 || ^4.0.3",
"react-native-reanimated": "^1.13.3 || ^2.1.0",
"react-native-render-html": "^5.1.0 || ^5.1.1",
"react-native-macos": "^0.64.0",
"react-native-modal": "^11.10.0",
"react-native-popover-view": "^4.0.3",
"react-native-reanimated": "^2.1.0",
"react-native-render-html": "^5.1.1",
"react-native-safe-area-context": "^3.2.0",
"react-native-screens": "^2.18.1 || ^3.1.1",
"react-native-screens": "^3.1.1",
"react-native-shimmer": "^0.5.0",
"react-native-sqlite-storage": "^3.3.11 || ^5.0.0",
"react-native-sqlite-storage": "^5.0.0",
"react-native-svg": "^12.1.1",
"react-native-webview": "^11.4.2",
"react-native-windows": "^0.63.0 || ^0.64.0",
"react-native-windows": "^0.64.0",
},
"version": "1.0.0-test",
}
`;

exports[`buildManifestProfile() builds a package manifest for a single profile version 1`] = `
exports[`buildManifestProfile() builds a package manifest for multiple profile versions 1`] = `
Object {
"dependencies": Object {
"@react-native-async-storage/async-storage": "^1.15.8",
Expand Down Expand Up @@ -215,34 +213,36 @@ Object {
"peerDependencies": Object {
"@react-native-async-storage/async-storage": "^1.15.8",
"@react-native-clipboard/clipboard": "^1.8.3",
"@react-native-community/checkbox": "^0.5.8",
"@react-native-community/datetimepicker": "^3.4.6",
"@react-native-community/async-storage": "^1.12.1",
"@react-native-community/checkbox": "^0.5.7 || ^0.5.8",
"@react-native-community/clipboard": "^1.5.1",
"@react-native-community/datetimepicker": "^3.0.9 || ^3.4.6",
"@react-native-community/hooks": "^2.6.0",
"@react-native-community/netinfo": "^6.0.2",
"@react-native-community/netinfo": "^5.9.10 || ^6.0.2",
"@react-native-masked-view/masked-view": "^0.2.4",
"@react-navigation/native": "^5.9.8",
"@react-navigation/stack": "^5.14.9",
"hermes-engine": "~0.7.0",
"react": "17.0.1",
"react-dom": "17.0.1",
"react-native": "^0.64.2",
"@react-navigation/native": "^5.9.4 || ^5.9.8",
"@react-navigation/stack": "^5.14.4 || ^5.14.9",
"hermes-engine": "~0.5.0 || ~0.7.0",
"react": "16.13.1 || 17.0.1",
"react-dom": "16.13.1 || 17.0.1",
"react-native": "^0.63.2 || ^0.64.2",
"react-native-base64": "^0.2.1",
"react-native-floating-action": "^1.21.0",
"react-native-fs": "^2.17.0",
"react-native-fs": "^2.16.6 || ^2.17.0",
"react-native-gesture-handler": "^1.10.3",
"react-native-lazy-index": "^2.1.1",
"react-native-macos": "^0.64.0",
"react-native-modal": "^11.10.0",
"react-native-popover-view": "^4.0.3",
"react-native-reanimated": "^2.1.0",
"react-native-render-html": "^5.1.1",
"react-native-macos": "^0.63.0 || ^0.64.0",
"react-native-modal": "^11.5.6 || ^11.10.0",
"react-native-popover-view": "^3.1.1 || ^4.0.3",
"react-native-reanimated": "^1.13.3 || ^2.1.0",
"react-native-render-html": "^5.1.0 || ^5.1.1",
"react-native-safe-area-context": "^3.2.0",
"react-native-screens": "^3.1.1",
"react-native-screens": "^2.18.1 || ^3.1.1",
"react-native-shimmer": "^0.5.0",
"react-native-sqlite-storage": "^5.0.0",
"react-native-sqlite-storage": "^3.3.11 || ^5.0.0",
"react-native-svg": "^12.1.1",
"react-native-webview": "^11.4.2",
"react-native-windows": "^0.64.0",
"react-native-windows": "^0.63.0 || ^0.64.0",
},
"version": "1.0.0-test",
}
Expand Down
Loading