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

fix(cli): fix build command missing additional context #3320

Merged
merged 2 commits into from
Aug 29, 2024
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/new-cougars-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rnx-kit/cli": patch
---

Provide additional context for the `build` command
13 changes: 10 additions & 3 deletions packages/cli/src/bin/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import {
getSavedState,
saveConfigToCache,
} from "@rnx-kit/tools-react-native/cache";
import { resolveCommunityCLI } from "@rnx-kit/tools-react-native/context";
import {
loadContext,
resolveCommunityCLI,
} from "@rnx-kit/tools-react-native/context";
import { reactNativeConfig } from "../index";

type Command = BaseCommand<false> | BaseCommand<true>;
Expand Down Expand Up @@ -53,7 +56,10 @@ export function uniquify(commands: Command[]): Command[] {
return Object.values(uniqueCommands);
}

export function loadContext(userCommand: string, root = process.cwd()): Config {
export function loadContextForCommand(
userCommand: string,
root = process.cwd()
): Config {
// The fast path avoids traversing project dependencies because we know what
// information our commands depend on.
const coreCommands = getCoreCommands();
Expand Down Expand Up @@ -90,7 +96,8 @@ export function loadContext(userCommand: string, root = process.cwd()): Config {
throw new Error("Unexpected access to `platforms`");
},
get project(): Config["project"] {
throw new Error("Unexpected access to `project`");
// Used by the build command
return loadContext(root).project;
},
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/bin/rnx-cli.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Command } from "commander";
import * as path from "node:path";
import { loadContext } from "./context";
import { loadContextForCommand } from "./context";
import { findExternalCommands } from "./externalCommands";

export function main() {
const [, , userCommand] = process.argv;

const context = loadContext(userCommand);
const context = loadContextForCommand(userCommand);
const allCommands = context.commands.concat(findExternalCommands(context));
const program = new Command(path.basename(__filename, ".js"));

Expand Down
14 changes: 9 additions & 5 deletions packages/cli/test/bin/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { Command } from "@react-native-community/cli-types";
import { getCoreCommands, loadContext, uniquify } from "../../src/bin/context";
import {
getCoreCommands,
loadContextForCommand,
uniquify,
} from "../../src/bin/context";
import { reactNativeConfig } from "../../src/index";

jest.mock("@rnx-kit/tools-react-native/context", () => ({
Expand Down Expand Up @@ -48,19 +52,19 @@ describe("bin/context/uniquify()", () => {
});
});

describe("bin/context/loadContext()", () => {
describe("bin/context/loadContextForCommand()", () => {
afterAll(() => {
jest.resetAllMocks();
});

it("uses fast code path for rnx commands", () => {
for (const { name } of getCoreCommands()) {
expect(() => loadContext(name)).not.toThrow();
expect(() => loadContextForCommand(name)).not.toThrow();
}
});

it("uses full code path for other commands", () => {
expect(() => loadContext("run-android")).toThrow();
expect(() => loadContext("run-ios")).toThrow();
expect(() => loadContextForCommand("run-android")).toThrow();
expect(() => loadContextForCommand("run-ios")).toThrow();
});
});
34 changes: 13 additions & 21 deletions packages/test-app/react-native.config.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
const project = (() => {
try {
const { configureProjects } = require("react-native-test-app");
return configureProjects({
android: {
sourceDir: "android",
},
ios: {
sourceDir: "ios",
},
windows: {
sourceDir: "windows",
solutionFile: "windows/SampleCrossApp.sln",
},
});
} catch (_) {
return undefined;
}
})();

const { configureProjects } = require("react-native-test-app");
module.exports = {
...(project ? { project } : undefined),
project: configureProjects({
android: {
sourceDir: "android",
},
ios: {
sourceDir: "ios",
},
windows: {
sourceDir: "windows",
solutionFile: "windows/SampleCrossApp.sln",
},
}),
};
Loading