diff --git a/packages/cli/src/lib/wizard/react-native/javascript.ts b/packages/cli/src/lib/wizard/react-native/javascript.ts index 2f680e105..7740a2c7f 100644 --- a/packages/cli/src/lib/wizard/react-native/javascript.ts +++ b/packages/cli/src/lib/wizard/react-native/javascript.ts @@ -220,15 +220,33 @@ export function getSentryIntegrationsPlainTextSnippet( `; } -function getMainAppFilePath(): string | undefined { - const prefixGlob = "{.,./src,./app}"; - const suffixGlob = "@(j|t|cj|mj)s?(x)"; - const universalGlob = `@(App|_layout).${suffixGlob}`; - const jsFileGlob = `${prefixGlob}/+(${universalGlob})`; - const jsPath = traceStep("find-app-js-file", () => - getFirstMatchedPath(jsFileGlob), - ); - return jsPath; +/** The root component's file: `App` (bare React Native) or `_layout` (Expo Router). */ +const APP_FILE_NAMES = "{App,_layout}.{js,jsx,ts,tsx,cjs,mjs}"; + +/** + * Where to look for {@link APP_FILE_NAMES}, in order: the first directory + * with a match wins. `src/app` is where the Expo SDK 58 template puts its + * layout. + */ +const APP_FILE_DIRS = [".", "src", "app", "src/app"]; + +/** + * The app's root component file, relative to the working directory, or + * `undefined` when there is none. One glob per directory, because + * `tinyglobby` matches nothing for a `.` inside braces (`{.,src}/App.tsx`), + * and the loop makes the order deterministic. + */ +export function getMainAppFilePath(): string | undefined { + return traceStep("find-app-js-file", () => { + for (const dir of APP_FILE_DIRS) { + const pattern = dir === "." ? APP_FILE_NAMES : `${dir}/${APP_FILE_NAMES}`; + const match = getFirstMatchedPath(pattern); + if (match) { + return match; + } + } + return undefined; + }); } /** diff --git a/packages/cli/test/lib/wizard/react-native/main-app-file.test.ts b/packages/cli/test/lib/wizard/react-native/main-app-file.test.ts new file mode 100644 index 000000000..a4ff23c97 --- /dev/null +++ b/packages/cli/test/lib/wizard/react-native/main-app-file.test.ts @@ -0,0 +1,63 @@ +/** + * `getMainAppFilePath` against the real filesystem and `tinyglobby`, for + * the layouts of the React Native and Expo templates. The glob helper is + * not mocked: the pattern is what's under test. + */ + +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { getMainAppFilePath } from "../../../../src/lib/wizard/react-native/javascript.js"; + +describe("getMainAppFilePath", () => { + const originalCwd = process.cwd(); + let dir: string; + + beforeEach(() => { + dir = mkdtempSync(join(tmpdir(), "rn-app-file-")); + process.chdir(dir); + }); + + afterEach(() => { + process.chdir(originalCwd); + rmSync(dir, { recursive: true, force: true }); + }); + + function touch(...files: string[]): void { + for (const file of files) { + mkdirSync(join(dir, dirname(file)), { recursive: true }); + writeFileSync(join(dir, file), ""); + } + } + + it.each([ + ["App.tsx", "bare React Native 0.88 template"], + ["src/app/_layout.tsx", "Expo SDK 58 template"], + ["app/_layout.tsx", "older Expo Router"], + ["src/App.js", "src directory"], + ["App.jsx", "JSX"], + ["src/app/_layout.js", "Expo Router in JavaScript"], + ])("finds %s (%s)", (file) => { + touch(file, "package.json", "index.js"); + + expect(getMainAppFilePath()).toBe(file); + }); + + it("checks the root first, then src, app, and src/app", () => { + touch("src/app/_layout.tsx", "app/_layout.tsx", "src/App.tsx", "App.tsx"); + expect(getMainAppFilePath()).toBe("App.tsx"); + + rmSync(join(dir, "App.tsx")); + expect(getMainAppFilePath()).toBe("src/App.tsx"); + + rmSync(join(dir, "src/App.tsx")); + expect(getMainAppFilePath()).toBe("app/_layout.tsx"); + }); + + it("ignores App files in node_modules and other directories", () => { + touch("node_modules/x/App.js", "lib/App.js", "App.ts.bak"); + + expect(getMainAppFilePath()).toBeUndefined(); + }); +});