Files
openbot/scripts/mobile-codegen.ts
T
NBandClaude Opus 5.5 a808ba2891 Typecheck only the projects that a staged file affects (#1429)
* Typecheck only the projects that a staged file affects

The pre-commit hook ran `bun run typecheck`: 13 projects in parallel, about 33 CPU-seconds and
8 GB of memory for each commit of code. `scripts/staged-typecheck.ts` now selects each
`typecheck:*` project that can see a staged file, from the tsconfig includes, the workspace
dependencies and a short list of relative imports across projects, and runs them one at a time.
A root `package.json`, `tsconfig*.json` or `bun.lock` selects all projects. `--dry-run` prints
the selection.

The hook gate no longer matches data JSON. `apps/mobile` `typecheck` skips `codegen` when a hash
of its inputs did not change (`scripts/mobile-codegen.ts`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Select the projects of renderer and Storybook imports, and hash routes on disk

- `typecheck:renderer` now sees `src/preload/clipboard-files.ts`, which
  `Conversation.stories.tsx` imports.
- `typecheck:storybook` now sees `src/renderer/src`, which `.storybook/preview.tsx`
  imports. Without this, a renderer change does not select it after the Storybook
  include became `.storybook/**`.
- The mobile codegen hash leaves out a route that the index lists but the disk does not
  have, so a deleted route runs codegen again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 15:31:00 +02:00

65 lines
2.5 KiB
TypeScript

// Runs the mobile `codegen` script only when its inputs changed since the last run.
//
// bun scripts/mobile-codegen.ts
//
// `expo customize` writes `expo-env.d.ts` and the typed routes in `.expo/types`, and Uniwind writes
// `src/uniwind-types.d.ts`. Together they take about 3 seconds, and `typecheck:mobile` needs them.
// A hash of the inputs, not file times, decides: a checkout or a rebase changes the times of files
// whose content is the same, and Uniwind does not write a file whose content did not change.
import { execFileSync } from "node:child_process";
import { createHash } from "node:crypto";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
const ROOT = resolve(import.meta.dirname, "..");
const MOBILE = "apps/mobile";
/** The files that the codegen reads, relative to the repository root. */
export const MOBILE_CODEGEN_INPUTS = [
`${MOBILE}/app.json`,
`${MOBILE}/global.css`,
`${MOBILE}/package.json`,
`${MOBILE}/tsconfig.json`,
// `global.css` imports these.
"packages/brand/src/tokens.css",
"packages/brand/src/tokens-native.css",
// The Expo, Uniwind and HeroUI versions.
"bun.lock",
];
/** Expo Router makes one typed route for each file name in this folder. */
const ROUTES = `${MOBILE}/src/app`;
const OUTPUTS = [`${MOBILE}/expo-env.d.ts`, `${MOBILE}/.expo/types/router.d.ts`, `${MOBILE}/src/uniwind-types.d.ts`];
const STAMP = `${MOBILE}/.expo/codegen-inputs.sha256`;
function inputHash(): string {
const hash = createHash("sha256");
for (const path of MOBILE_CODEGEN_INPUTS) hash.update(`${path}\0`).update(readFileSync(join(ROOT, path)));
// The index still lists a route that was deleted and not staged; Expo Router reads the disk.
const routes = execFileSync("git", ["ls-files", "--cached", "--others", "--exclude-standard", "-z", "--", ROUTES], {
cwd: ROOT,
encoding: "utf8",
})
.split("\0")
.filter((path) => path && existsSync(join(ROOT, path)));
return hash.update(routes.join("\0")).digest("hex");
}
if (import.meta.main) {
const hash = inputHash();
const stamp = join(ROOT, STAMP);
const current =
OUTPUTS.every((path) => existsSync(join(ROOT, path))) &&
existsSync(stamp) &&
readFileSync(stamp, "utf8").trim() === hash;
if (current) {
process.stdout.write("Mobile codegen outputs are current.\n");
} else {
execFileSync("bun", ["run", "codegen"], { cwd: join(ROOT, MOBILE), stdio: "inherit" });
mkdirSync(dirname(stamp), { recursive: true });
writeFileSync(stamp, `${hash}\n`);
}
}