Open History to see the last commit
1#!/usr/bin/env node
2// @effect-diagnostics nodeBuiltinImport:off globalConsole:off - A one-shot CI check; synchronous Node APIs keep it small.
3// Fails when a committed GT catalog drifts from the source: `gt generate` would write a different
4// en.json, or a locale catalog's keys differ from en.json. Needs no GT credentials or network.
5import * as NodeChildProcess from "node:child_process";
6import * as NodeFS from "node:fs";
7import * as NodeOS from "node:os";
8import * as NodePath from "node:path";
9
10// Desktop only depends on gt-i18n, so the gt CLI hides `generate` there; its script adds it back.
11// The gt CLI runs through Node because the `.bin/gt` shim is a shell script Windows cannot spawn.
12const GENERATE: Record<string, readonly [string, ...string[]]> = {
13 "apps/web": [process.execPath, "node_modules/gt/bin/main.js", "generate"],
14 "apps/mobile": [process.execPath, "node_modules/gt/bin/main.js", "generate"],
15 "apps/desktop": [process.execPath, "scripts/i18n-generate.mjs"],
16};
17const APPS = Object.keys(GENERATE);
18
19// JSX entries (`<T>`) are arrays and objects, so values compare by their JSON text.
20type Catalog = Record<string, unknown>;
21const readJson = (file: string) => JSON.parse(NodeFS.readFileSync(file, "utf8"));
22const missingFrom = (from: Catalog, catalog: Catalog) =>
23 Object.keys(from).filter((key) => !Object.hasOwn(catalog, key));
24
25const root = NodePath.resolve(import.meta.dirname, "..");
26const problems: string[] = [];
27
28for (const [app, [command, ...args]] of Object.entries(GENERATE)) {
29 const appDir = NodePath.join(root, app);
30 const config = readJson(NodePath.join(appDir, "gt.config.json"));
31 const catalogDir = NodePath.join(appDir, NodePath.dirname(config.files.gt.output));
32 const source: Catalog = readJson(NodePath.join(catalogDir, `${config.defaultLocale}.json`));
33
34 // Same scan as `vp run i18n:generate`, but it writes only the source catalog, to a temp dir.
35 const tmp = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "i18n-check-"));
36 try {
37 const tmpConfig = NodePath.join(tmp, "gt.config.json");
38 NodeFS.writeFileSync(
39 tmpConfig,
40 JSON.stringify({ ...config, locales: [], files: { gt: { output: `${tmp}/[locale].json` } } }),
41 );
42 const gt = NodeChildProcess.spawnSync(command, [...args, "--config", tmpConfig], {
43 cwd: appDir,
44 encoding: "utf8",
45 });
46 if (gt.status !== 0) {
47 problems.push(`${app}: gt generate failed\n${gt.stdout}${gt.stderr}`);
48 continue;
49 }
50 const generated: Catalog = readJson(NodePath.join(tmp, `${config.defaultLocale}.json`));
51 for (const [key, text] of Object.entries(generated)) {
52 if (!Object.hasOwn(source, key)) {
53 problems.push(`${app}: ${key} ${JSON.stringify(text)} is missing from en.json`);
54 } else if (JSON.stringify(source[key]) !== JSON.stringify(text)) {
55 problems.push(
56 `${app}: en.json has ${key} ${JSON.stringify(source[key])}, source says ${JSON.stringify(text)}`,
57 );
58 }
59 }
60 for (const key of missingFrom(source, generated)) {
61 problems.push(`${app}: ${key} ${JSON.stringify(source[key])} in en.json is stale`);
62 }
63 } finally {
64 NodeFS.rmSync(tmp, { recursive: true, force: true });
65 }
66
67 for (const locale of config.locales) {
68 const catalog: Catalog = readJson(NodePath.join(catalogDir, `${locale}.json`));
69 for (const key of missingFrom(source, catalog)) {
70 problems.push(`${app}: ${locale}.json is missing ${key} ${JSON.stringify(source[key])}`);
71 }
72 for (const key of missingFrom(catalog, source)) {
73 problems.push(`${app}: ${locale}.json has ${key}, which en.json lacks`);
74 }
75 }
76}
77
78if (problems.length > 0) {
79 console.error(problems.join("\n"));
80 console.error(
81 "\nRun `vp run i18n:generate` in the app, then translate any new keys in every locale catalog.",
82 );
83 process.exitCode = 1;
84} else {
85 console.log(`i18n catalogs match the source in ${APPS.join(", ")}.`);
86}