Skip to content

Commit

Permalink
fix(dep-check): fix loading of custom profiles from relative paths (#918
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tido64 committed Dec 2, 2021
1 parent 83e5312 commit 9816461
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 114 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-chicken-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rnx-kit/dep-check": patch
---

Fix loading of custom profiles from relative paths
10 changes: 6 additions & 4 deletions packages/dep-check/src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getRequirements } from "./dependencies";
import { findBadPackages } from "./findBadPackages";
import { modifyManifest } from "./helpers";
import { updatePackageManifest } from "./manifest";
import { getProfilesFor } from "./profiles";
import { getProfilesFor, resolveCustomProfiles } from "./profiles";
import type { CheckOptions, Command } from "./types";

export function checkPackageManifest(
Expand Down Expand Up @@ -45,13 +45,15 @@ export function checkPackageManifest(
customProfiles,
} = getKitCapabilities(kitConfig);

const customProfilesPath = resolveCustomProfiles(projectRoot, customProfiles);

const { reactNativeVersion, capabilities: requiredCapabilities } =
getRequirements(
targetReactNativeVersion,
kitType,
manifest,
projectRoot,
customProfiles,
customProfilesPath,
{ loose }
);
requiredCapabilities.push(...targetCapabilities);
Expand All @@ -63,8 +65,8 @@ export function checkPackageManifest(
const updatedManifest = updatePackageManifest(
manifest,
requiredCapabilities,
getProfilesFor(reactNativeVersion, customProfiles),
getProfilesFor(reactNativeDevVersion, customProfiles),
getProfilesFor(reactNativeVersion, customProfilesPath),
getProfilesFor(reactNativeDevVersion, customProfilesPath),
kitType
);

Expand Down
9 changes: 7 additions & 2 deletions packages/dep-check/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as path from "path";
import { getAllPackageJsonFiles, getWorkspaceRoot } from "workspace-tools";
import { makeCheckCommand } from "./check";
import { initializeConfig } from "./initialize";
import { resolveCustomProfiles } from "./profiles";
import { makeSetVersionCommand } from "./setVersion";
import type { Args, Command } from "./types";
import { makeVigilantCommand } from "./vigilant";
Expand Down Expand Up @@ -95,7 +96,7 @@ async function makeCommand(args: Args): Promise<Command | undefined> {
}

const {
"custom-profiles": customProfilesPath,
"custom-profiles": customProfiles,
"exclude-packages": excludePackages,
init,
loose,
Expand All @@ -116,8 +117,12 @@ async function makeCommand(args: Args): Promise<Command | undefined> {
}

if (isString(vigilant)) {
const customProfilesPath = resolveCustomProfiles(
process.cwd(),
customProfiles?.toString()
);
return makeVigilantCommand({
customProfiles: customProfilesPath?.toString(),
customProfiles: customProfilesPath,
excludePackages: excludePackages?.toString(),
loose,
versions: vigilant.toString(),
Expand Down
16 changes: 3 additions & 13 deletions packages/dep-check/src/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ import {
getProfileVersionsFor,
profilesSatisfying,
} from "./profiles";
import type {
CheckOptions,
Profile,
ProfileVersion,
TestOverrides,
} from "./types";
import type { CheckOptions, Profile, ProfileVersion } from "./types";

type Requirements = Required<
Pick<KitConfig, "reactNativeVersion" | "capabilities">
Expand Down Expand Up @@ -85,8 +80,7 @@ export function getRequirements(
targetManifest: PackageManifest,
projectRoot: string,
customProfiles: string | undefined,
{ loose }: Pick<CheckOptions, "loose">,
testOverrides?: TestOverrides
{ loose }: Pick<CheckOptions, "loose">
): Requirements {
let profileVersions = getProfileVersionsFor(targetReactNativeVersion);
if (profileVersions.length === 0) {
Expand Down Expand Up @@ -155,11 +149,7 @@ export function getRequirements(
}
}

const profiles = getProfilesFor(
profileVersions,
customProfiles,
testOverrides
);
const profiles = getProfilesFor(profileVersions, customProfiles);
allCapabilities.forEach((capability) => {
/**
* Core capabilities are capabilities that must always be declared by the
Expand Down
1 change: 0 additions & 1 deletion packages/dep-check/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ export type {
Package,
Profile,
ProfileVersion,
TestOverrides,
VigilantOptions,
} from "./types";
55 changes: 19 additions & 36 deletions packages/dep-check/src/profiles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { error } from "@rnx-kit/console";
import { tryInvoke } from "@rnx-kit/tools-language/function";
import { hasProperty } from "@rnx-kit/tools-language/properties";
import isString from "lodash/isString";
import semver from "semver";
import { keysOf } from "./helpers";
Expand All @@ -11,7 +9,7 @@ import profile_0_64 from "./profiles/profile-0.64";
import profile_0_65 from "./profiles/profile-0.65";
import profile_0_66 from "./profiles/profile-0.66";
import profile_0_67 from "./profiles/profile-0.67";
import type { Profile, ProfileVersion, TestOverrides } from "./types";
import type { Profile, ProfileVersion } from "./types";

type ProfileMap = Record<ProfileVersion, Profile>;

Expand Down Expand Up @@ -61,36 +59,15 @@ function isValidProfileMap(map: unknown): map is Partial<ProfileMap> {
}

function loadCustomProfiles(
customProfilesPath: string | undefined,
{ moduleResolver = require.resolve }: TestOverrides = {}
customProfilesPath: string | undefined
): Partial<ProfileMap> {
if (customProfilesPath) {
const [resolvedPath, moduleNotFoundError] = tryInvoke(() =>
moduleResolver(customProfilesPath)
);
if (moduleNotFoundError || !resolvedPath) {
const helpMsg =
"Please make sure the path exists or is added to your 'package.json'.";

if (!moduleNotFoundError) {
const message = `Cannot find module '${customProfilesPath}'`;
error(`${message}. ${helpMsg}`);
throw new Error(message);
}

if (hasProperty(moduleNotFoundError, "message")) {
error(moduleNotFoundError.message);
}
error(helpMsg);
throw moduleNotFoundError;
}

const customProfiles: unknown = require(resolvedPath);
const customProfiles: unknown = require(customProfilesPath);
if (!isValidProfileMap(customProfiles)) {
const message = `'${customProfilesPath}' doesn't default export profiles`;
error(
[
"${message}. Please make sure that it exports an object with a shape similar to:",
`${message}. Please make sure that it exports an object with a shape similar to:`,
"",
" module.exports = {",
' "0.63": {',
Expand All @@ -100,6 +77,7 @@ function loadCustomProfiles(
" },",
" },",
" };",
"",
].join("\n")
);
throw new Error(message);
Expand Down Expand Up @@ -129,10 +107,9 @@ export function getProfileVersionsFor(

export function getProfilesFor(
reactVersionRange: string | ProfileVersion[],
customProfilesPath: string | undefined,
testOverrides?: TestOverrides
customProfilesPath: string | undefined
): Profile[] {
const customProfiles = loadCustomProfiles(customProfilesPath, testOverrides);
const customProfiles = loadCustomProfiles(customProfilesPath);
const profiles = getProfileVersionsFor(reactVersionRange).map((version) => ({
...defaultProfiles[version],
...customProfiles[version],
Expand All @@ -148,8 +125,7 @@ export function getProfilesFor(

export function parseProfilesString(
versions: string | number,
customProfilesPath?: string | number,
testOverrides?: TestOverrides
customProfilesPath?: string | number
): ProfilesInfo {
const profileVersions = versions
.toString()
Expand All @@ -163,14 +139,12 @@ export function parseProfilesString(
return {
supportedProfiles: getProfilesFor(
supportedVersions,
customProfilesPath?.toString(),
testOverrides
customProfilesPath?.toString()
),
supportedVersions,
targetProfile: getProfilesFor(
targetVersion,
customProfilesPath?.toString(),
testOverrides
customProfilesPath?.toString()
),
targetVersion,
};
Expand All @@ -183,3 +157,12 @@ export function profilesSatisfying(
const versions = getProfileVersionsFor(versionOrRange);
return profiles.filter((v) => versions.includes(v));
}

export function resolveCustomProfiles(
projectRoot: string,
profilesPath: string | undefined
): string | undefined {
return profilesPath
? require.resolve(profilesPath, { paths: [projectRoot] })
: undefined;
}
4 changes: 0 additions & 4 deletions packages/dep-check/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ export type ProfileVersion =
| "0.66"
| "0.67";

export type TestOverrides = {
moduleResolver?: typeof require.resolve;
};

export type ExcludedPackage = Package & {
reason: string;
};
13 changes: 3 additions & 10 deletions packages/dep-check/src/vigilant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ import { checkPackageManifest } from "./check";
import { keysOf, modifyManifest } from "./helpers";
import { updateDependencies } from "./manifest";
import { parseProfilesString } from "./profiles";
import type {
Command,
ManifestProfile,
TestOverrides,
VigilantOptions,
} from "./types";
import type { Command, ManifestProfile, VigilantOptions } from "./types";

type Change = {
name: string;
Expand All @@ -28,13 +23,11 @@ const allSections = [

export function buildManifestProfile(
versions: string,
customProfilesPath: string | undefined,
testOverrides?: TestOverrides
customProfilesPath: string | undefined
): ManifestProfile {
const { supportedProfiles, targetProfile } = parseProfilesString(
versions,
customProfilesPath,
testOverrides
customProfilesPath
);

const allCapabilities = keysOf(targetProfile[0]);
Expand Down
Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 4 additions & 9 deletions packages/dep-check/test/capabilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getProfilesFor } from "../src/profiles";
import profile_0_62 from "../src/profiles/profile-0.62";
import profile_0_63 from "../src/profiles/profile-0.63";
import profile_0_64 from "../src/profiles/profile-0.64";
import { mockResolver, pickPackage } from "./helpers";
import { pickPackage } from "./helpers";

describe("capabilitiesFor()", () => {
test("returns `undefined` when react-native is not a dependency", () => {
Expand Down Expand Up @@ -138,8 +138,7 @@ describe("resolveCapabilities()", () => {

const profiles = getProfilesFor(
"^0.62 || ^0.63 || ^0.64",
"mock-custom-profiles-module",
{ moduleResolver: mockResolver("mock-custom-profiles-module") }
"mock-custom-profiles-module"
);

const packages = resolveCapabilities(
Expand Down Expand Up @@ -199,9 +198,7 @@ describe("resolveCapabilities()", () => {

const packages = resolveCapabilities(
["core/all" as Capability],
getProfilesFor("^0.64", "mock-meta-package", {
moduleResolver: mockResolver("mock-meta-package"),
})
getProfilesFor("^0.64", "mock-meta-package")
);

expect(packages).toEqual({
Expand Down Expand Up @@ -236,9 +233,7 @@ describe("resolveCapabilities()", () => {

const packages = resolveCapabilities(
["reese" as Capability],
getProfilesFor("^0.64", "mock-meta-package-loop", {
moduleResolver: mockResolver("mock-meta-package-loop"),
})
getProfilesFor("^0.64", "mock-meta-package-loop")
);

expect(packages).toEqual({
Expand Down
4 changes: 1 addition & 3 deletions packages/dep-check/test/dependencies.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { PackageManifest, readPackage } from "@rnx-kit/tools-node/package";
import path from "path";
import { getRequirements, visitDependencies } from "../src/dependencies";
import { mockResolver } from "./helpers";

jest.unmock("@rnx-kit/config");

Expand Down Expand Up @@ -163,8 +162,7 @@ describe("getRequirements()", () => {
manifest,
fixture,
"awesome-dep-check-profiles",
defaultOptions,
{ moduleResolver: mockResolver("awesome-dep-check-profiles") }
defaultOptions
);

expect(reactNativeVersion).toBe("^0.63 || ^0.64");
Expand Down
8 changes: 0 additions & 8 deletions packages/dep-check/test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import type { Capability } from "@rnx-kit/config";
import type { Profile, Package } from "../src/types";

export function mockResolver(moduleName?: string): NodeJS.RequireResolve {
const resolve = function () {
return moduleName;
};
resolve.paths = (): string[] => [];
return resolve;
}

export function pickPackage(profile: Profile, capability: string): Package {
const pkg = profile[capability];
if (!pkg) {
Expand Down

0 comments on commit 9816461

Please sign in to comment.