Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions apps/mobile/src/copy-keys.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/// <reference types="node" />
import { test } from "node:test";
import assert from "node:assert/strict";
import { readdirSync, readFileSync } from "node:fs";
import { join, relative } from "node:path";
import { copyKeys } from "./copy-keys.ts";

const ROOT = new URL("../../../", import.meta.url).pathname;
const MOBILE_SRC = new URL(".", import.meta.url).pathname;
const PACKAGES = join(ROOT, "packages");
const LOCALE_DIR = join(PACKAGES, "copy", "locales");

function loadLocale(name: "en" | "es-419"): Record<string, string> {
return JSON.parse(readFileSync(join(LOCALE_DIR, `${name}.json`), "utf8")) as Record<string, string>;
}

function walkSourceFiles(dir: string): string[] {
return readdirSync(dir, { recursive: true })
.map(String)
.filter((f) => (f.endsWith(".ts") || f.endsWith(".tsx")) && !f.endsWith(".test.ts") && !f.endsWith(".test.tsx"))
.map((f) => join(dir, f));
}

test("extracts literal copy keys and skips template expressions and non-keys", () => {
const src = [
'const a = t("HM-BOT-02.clearing");',
"const b = t('common.cancel');",
"const c = `HM-APPR-01.approve`;",
"const d = `HM-SET-10.failed.${reason}`;",
'const e = "https://example.com";',
'const f = "HM-SET-10.failed.";',
'const g = "not.a.key";',
].join("\n");
assert.deepEqual(copyKeys(src), ["HM-BOT-02.clearing", "common.cancel", "HM-APPR-01.approve"]);
});

test("every literal copy key in apps/mobile/src and packages/*/src exists in both locales", () => {
const en = loadLocale("en");
const es = loadLocale("es-419");

const dirs = [
MOBILE_SRC,
...readdirSync(PACKAGES).map((pkg) => join(PACKAGES, pkg, "src")),
];

const files = dirs.flatMap((dir) => {
try {
return walkSourceFiles(dir);
} catch {
return [];
}
});

const missing: string[] = [];
for (const file of files) {
const src = readFileSync(file, "utf8");
const rel = relative(ROOT, file);
for (const key of copyKeys(src)) {
if (en[key] === undefined) missing.push(`${rel} (en): ${key}`);
if (es[key] === undefined) missing.push(`${rel} (es-419): ${key}`);
}
}
assert.deepEqual(missing, []);
});
16 changes: 16 additions & 0 deletions apps/mobile/src/copy-keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { KEY_PATTERN } from "@hermit/copy";

const LITERAL = /"([^"\\\n]*)"|'([^'\\\n]*)'|`([^`\\$]*(?:\$(?!\{)[^`\\$]*)*)`/g;

/**
* Extracts copy keys written as whole string literals (double quotes, single quotes, or
* backticks without `${` interpolations) from a TypeScript/TSX source file.
*/
export function copyKeys(source: string): string[] {
const seen = new Set<string>();
for (const match of source.matchAll(LITERAL)) {
const value = match[1] ?? match[2] ?? match[3];
if (value !== undefined && KEY_PATTERN.test(value)) seen.add(value);
}
return [...seen];
}