From 1fda8dccdcb1d05b9d50a032d85a930d8b9d07e2 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Thu, 29 Aug 2024 16:33:14 +0200 Subject: [PATCH 1/2] fix(cli): fix `build` command missing additional context --- .changeset/new-cougars-collect.md | 5 ++++ packages/cli/src/bin/context.ts | 13 ++++++--- packages/cli/src/bin/rnx-cli.ts | 4 +-- packages/test-app/react-native.config.js | 34 +++++++++--------------- 4 files changed, 30 insertions(+), 26 deletions(-) create mode 100644 .changeset/new-cougars-collect.md diff --git a/.changeset/new-cougars-collect.md b/.changeset/new-cougars-collect.md new file mode 100644 index 000000000..65d655dc3 --- /dev/null +++ b/.changeset/new-cougars-collect.md @@ -0,0 +1,5 @@ +--- +"@rnx-kit/cli": patch +--- + +Provide additional context for the `build` command diff --git a/packages/cli/src/bin/context.ts b/packages/cli/src/bin/context.ts index d98ed6210..a5e49c1bb 100644 --- a/packages/cli/src/bin/context.ts +++ b/packages/cli/src/bin/context.ts @@ -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 | BaseCommand; @@ -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(); @@ -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; }, }; } diff --git a/packages/cli/src/bin/rnx-cli.ts b/packages/cli/src/bin/rnx-cli.ts index 7bcb54c10..ba318451b 100644 --- a/packages/cli/src/bin/rnx-cli.ts +++ b/packages/cli/src/bin/rnx-cli.ts @@ -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")); diff --git a/packages/test-app/react-native.config.js b/packages/test-app/react-native.config.js index b13fe486a..227b9550f 100644 --- a/packages/test-app/react-native.config.js +++ b/packages/test-app/react-native.config.js @@ -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", + }, + }), }; From e6176118ffecefa253b7dac9877b40dab0d79dcc Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Thu, 29 Aug 2024 17:08:29 +0200 Subject: [PATCH 2/2] update tests --- packages/cli/test/bin/context.test.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/cli/test/bin/context.test.ts b/packages/cli/test/bin/context.test.ts index 4bee62ab0..38de2d8fc 100644 --- a/packages/cli/test/bin/context.test.ts +++ b/packages/cli/test/bin/context.test.ts @@ -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", () => ({ @@ -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(); }); });