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.
11const GENERATE: Record<string, readonly [string, ...string[]]> = {
12 "apps/web": ["node_modules/.bin/gt", "generate"],
13 "apps/mobile": ["node_modules/.bin/gt", "generate"],
14 "apps/desktop": [process.execPath, "scripts/i18n-generate.mjs"],
15};
16const APPS = Object.keys(GENERATE);
17
18// JSX entries (`<T>`) are arrays and objects, so values compare by their JSON text.
19type Catalog = Record<string, unknown>;
20const readJson = (file: string) => JSON.parse(NodeFS.readFileSync(file, "utf8"));
21const missingFrom = (from: Catalog, catalog: Catalog) =>
22 Object.keys(from).filter((key) => !Object.hasOwn(catalog, key));
23
24const root = NodePath.resolve(import.meta.dirname, "..");
25const problems: string[] = [];
26
27for (const [app, [command, ...args]] of Object.entries(GENERATE)) {
28 const appDir = NodePath.join(root, app);
29 const config = readJson(NodePath.join(appDir, "gt.config.json"));
30 const catalogDir = NodePath.join(appDir, NodePath.dirname(config.files.gt.output));
31 const source: Catalog = readJson(NodePath.join(catalogDir, `${config.defaultLocale}.json`));
32
33 // Same scan as `vp run i18n:generate`, but it writes only the source catalog, to a temp dir.
34 const tmp = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "i18n-check-"));
35 try {
36 const tmpConfig = NodePath.join(tmp, "gt.config.json");
37 NodeFS.writeFileSync(
38 tmpConfig,
39 JSON.stringify({ ...config, locales: [], files: { gt: { output: `${tmp}/[locale].json` } } }),
40 );
41 const gt = NodeChildProcess.spawnSync(command, [...args, "--config", tmpConfig], {
42 cwd: appDir,
43 encoding: "utf8",
44 });
45 if (gt.status !== 0) {
46 problems.push(`${app}: gt generate failed\n${gt.stdout}${gt.stderr}`);
47 continue;
48 }
49 const generated: Catalog = readJson(NodePath.join(tmp, `${config.defaultLocale}.json`));
50 for (const [key, text] of Object.entries(generated)) {
51 if (!Object.hasOwn(source, key)) {
52 problems.push(`${app}: ${key} ${JSON.stringify(text)} is missing from en.json`);
53 } else if (JSON.stringify(source[key]) !== JSON.stringify(text)) {
54 problems.push(
55 `${app}: en.json has ${key} ${JSON.stringify(source[key])}, source says ${JSON.stringify(text)}`,
56 );
57 }
58 }
59 for (const key of missingFrom(source, generated)) {
60 problems.push(`${app}: ${key} ${JSON.stringify(source[key])} in en.json is stale`);
61 }
62 } finally {
63 NodeFS.rmSync(tmp, { recursive: true, force: true });
64 }
65
66 for (const locale of config.locales) {
67 const catalog: Catalog = readJson(NodePath.join(catalogDir, `${locale}.json`));
68 for (const key of missingFrom(source, catalog)) {
69 problems.push(`${app}: ${locale}.json is missing ${key} ${JSON.stringify(source[key])}`);
70 }
71 for (const key of missingFrom(catalog, source)) {
72 problems.push(`${app}: ${locale}.json has ${key}, which en.json lacks`);
73 }
74 }
75}
76
77if (problems.length > 0) {
78 console.error(problems.join("\n"));
79 console.error(
80 "\nRun `vp run i18n:generate` in the app, then translate any new keys in every locale catalog.",
81 );
82 process.exitCode = 1;
83} else {
84 console.log(`i18n catalogs match the source in ${APPS.join(", ")}.`);
85}