@nifrajs/cli 5.0.0 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (296) hide show
  1. package/CHANGELOG.md +3211 -0
  2. package/README.md +10 -2
  3. package/dist/agent-files.d.ts +19 -11
  4. package/dist/agent-files.d.ts.map +1 -1
  5. package/dist/agent-files.js +26 -18
  6. package/dist/agent-files.js.map +1 -1
  7. package/dist/assure-hydration.d.ts.map +1 -1
  8. package/dist/assure-hydration.js +5 -2
  9. package/dist/assure-hydration.js.map +1 -1
  10. package/dist/assure.d.ts +12 -2
  11. package/dist/assure.d.ts.map +1 -1
  12. package/dist/assure.js +73 -9
  13. package/dist/assure.js.map +1 -1
  14. package/dist/capabilities-tool.d.ts.map +1 -1
  15. package/dist/capabilities-tool.js +152 -49
  16. package/dist/capabilities-tool.js.map +1 -1
  17. package/dist/check-diagnostics.d.ts +7 -0
  18. package/dist/check-diagnostics.d.ts.map +1 -1
  19. package/dist/check-diagnostics.js +3 -0
  20. package/dist/check-diagnostics.js.map +1 -1
  21. package/dist/check-scan.d.ts +8 -2
  22. package/dist/check-scan.d.ts.map +1 -1
  23. package/dist/check-scan.js +49 -13
  24. package/dist/check-scan.js.map +1 -1
  25. package/dist/check.d.ts.map +1 -1
  26. package/dist/check.js +91 -45
  27. package/dist/check.js.map +1 -1
  28. package/dist/cli.d.ts +2 -0
  29. package/dist/cli.d.ts.map +1 -1
  30. package/dist/cli.js +128 -59
  31. package/dist/cli.js.map +1 -1
  32. package/dist/client-module.d.ts +5 -3
  33. package/dist/client-module.d.ts.map +1 -1
  34. package/dist/client-module.js +34 -10
  35. package/dist/client-module.js.map +1 -1
  36. package/dist/command-catalog.d.ts +9 -1
  37. package/dist/command-catalog.d.ts.map +1 -1
  38. package/dist/command-catalog.js +75 -26
  39. package/dist/command-catalog.js.map +1 -1
  40. package/dist/dev-bun-config.d.ts +6 -3
  41. package/dist/dev-bun-config.d.ts.map +1 -1
  42. package/dist/dev-bun-config.js +13 -10
  43. package/dist/dev-bun-config.js.map +1 -1
  44. package/dist/dev-feed-tool.d.ts +19 -1
  45. package/dist/dev-feed-tool.d.ts.map +1 -1
  46. package/dist/dev-feed-tool.js +114 -0
  47. package/dist/dev-feed-tool.js.map +1 -1
  48. package/dist/doctor.d.ts +15 -1
  49. package/dist/doctor.d.ts.map +1 -1
  50. package/dist/doctor.js +79 -61
  51. package/dist/doctor.js.map +1 -1
  52. package/dist/fix-recipes.d.ts +2 -0
  53. package/dist/fix-recipes.d.ts.map +1 -1
  54. package/dist/fix-recipes.js +93 -12
  55. package/dist/fix-recipes.js.map +1 -1
  56. package/dist/i18n-check.d.ts +11 -1
  57. package/dist/i18n-check.d.ts.map +1 -1
  58. package/dist/i18n-check.js +170 -17
  59. package/dist/i18n-check.js.map +1 -1
  60. package/dist/init-agents.d.ts +60 -4
  61. package/dist/init-agents.d.ts.map +1 -1
  62. package/dist/init-agents.js +196 -33
  63. package/dist/init-agents.js.map +1 -1
  64. package/dist/internal/config-paths.d.ts +16 -0
  65. package/dist/internal/config-paths.d.ts.map +1 -0
  66. package/dist/internal/config-paths.js +124 -0
  67. package/dist/internal/config-paths.js.map +1 -0
  68. package/dist/internal/installed-package.d.ts +4 -0
  69. package/dist/internal/installed-package.d.ts.map +1 -0
  70. package/dist/internal/installed-package.js +17 -0
  71. package/dist/internal/installed-package.js.map +1 -0
  72. package/dist/internal/project-specifiers.d.ts +14 -1
  73. package/dist/internal/project-specifiers.d.ts.map +1 -1
  74. package/dist/internal/project-specifiers.js +63 -16
  75. package/dist/internal/project-specifiers.js.map +1 -1
  76. package/dist/internal/source-facts.d.ts +12 -1
  77. package/dist/internal/source-facts.d.ts.map +1 -1
  78. package/dist/internal/source-facts.js +117 -5
  79. package/dist/internal/source-facts.js.map +1 -1
  80. package/dist/internal/typescript-import.d.ts.map +1 -1
  81. package/dist/internal/typescript-import.js +5 -3
  82. package/dist/internal/typescript-import.js.map +1 -1
  83. package/dist/internal/workspace-packages.d.ts +22 -0
  84. package/dist/internal/workspace-packages.d.ts.map +1 -0
  85. package/dist/internal/workspace-packages.js +215 -0
  86. package/dist/internal/workspace-packages.js.map +1 -0
  87. package/dist/introspect.d.ts +9 -0
  88. package/dist/introspect.d.ts.map +1 -1
  89. package/dist/introspect.js +32 -1
  90. package/dist/introspect.js.map +1 -1
  91. package/dist/levels-tool.d.ts.map +1 -1
  92. package/dist/levels-tool.js +5 -8
  93. package/dist/levels-tool.js.map +1 -1
  94. package/dist/load.d.ts +9 -4
  95. package/dist/load.d.ts.map +1 -1
  96. package/dist/load.js +32 -3
  97. package/dist/load.js.map +1 -1
  98. package/dist/lock-links.d.ts +42 -0
  99. package/dist/lock-links.d.ts.map +1 -0
  100. package/dist/lock-links.js +144 -0
  101. package/dist/lock-links.js.map +1 -0
  102. package/dist/mcp-context.d.ts.map +1 -1
  103. package/dist/mcp-context.js +6 -21
  104. package/dist/mcp-context.js.map +1 -1
  105. package/dist/mcp-delegate.d.ts +2 -2
  106. package/dist/mcp-delegate.d.ts.map +1 -1
  107. package/dist/mcp-delegate.js +19 -7
  108. package/dist/mcp-delegate.js.map +1 -1
  109. package/dist/mcp-exec.d.ts +1 -0
  110. package/dist/mcp-exec.d.ts.map +1 -1
  111. package/dist/mcp-exec.js +13 -5
  112. package/dist/mcp-exec.js.map +1 -1
  113. package/dist/mcp-http.js +1 -1
  114. package/dist/mcp-io.d.ts +13 -0
  115. package/dist/mcp-io.d.ts.map +1 -1
  116. package/dist/mcp-io.js +23 -0
  117. package/dist/mcp-io.js.map +1 -1
  118. package/dist/mcp-path.d.ts +2 -1
  119. package/dist/mcp-path.d.ts.map +1 -1
  120. package/dist/mcp-path.js +9 -24
  121. package/dist/mcp-path.js.map +1 -1
  122. package/dist/mcp-root.d.ts +6 -4
  123. package/dist/mcp-root.d.ts.map +1 -1
  124. package/dist/mcp-root.js +12 -8
  125. package/dist/mcp-root.js.map +1 -1
  126. package/dist/mcp.d.ts.map +1 -1
  127. package/dist/mcp.js +40 -17
  128. package/dist/mcp.js.map +1 -1
  129. package/dist/migrate-layout.d.ts +41 -1
  130. package/dist/migrate-layout.d.ts.map +1 -1
  131. package/dist/migrate-layout.js +1729 -236
  132. package/dist/migrate-layout.js.map +1 -1
  133. package/dist/output-schemas.d.ts +34 -0
  134. package/dist/output-schemas.d.ts.map +1 -1
  135. package/dist/output-schemas.js +132 -21
  136. package/dist/output-schemas.js.map +1 -1
  137. package/dist/project-path.d.ts +6 -0
  138. package/dist/project-path.d.ts.map +1 -1
  139. package/dist/project-path.js +48 -14
  140. package/dist/project-path.js.map +1 -1
  141. package/dist/project-site.d.ts +7 -0
  142. package/dist/project-site.d.ts.map +1 -0
  143. package/dist/project-site.js +19 -0
  144. package/dist/project-site.js.map +1 -0
  145. package/dist/recipes/4.0.0.d.ts.map +1 -1
  146. package/dist/recipes/4.0.0.js +2 -0
  147. package/dist/recipes/4.0.0.js.map +1 -1
  148. package/dist/recipes/5.1.0.d.ts +9 -0
  149. package/dist/recipes/5.1.0.d.ts.map +1 -0
  150. package/dist/recipes/5.1.0.js +22 -0
  151. package/dist/recipes/5.1.0.js.map +1 -0
  152. package/dist/recipes/5.2.0.d.ts +10 -0
  153. package/dist/recipes/5.2.0.d.ts.map +1 -0
  154. package/dist/recipes/5.2.0.js +32 -0
  155. package/dist/recipes/5.2.0.js.map +1 -0
  156. package/dist/recipes/index.d.ts +3 -0
  157. package/dist/recipes/index.d.ts.map +1 -1
  158. package/dist/recipes/index.js +7 -0
  159. package/dist/recipes/index.js.map +1 -1
  160. package/dist/release-verification.d.ts +5 -0
  161. package/dist/release-verification.d.ts.map +1 -1
  162. package/dist/release-verification.js +14 -0
  163. package/dist/release-verification.js.map +1 -1
  164. package/dist/review.d.ts.map +1 -1
  165. package/dist/review.js +7 -0
  166. package/dist/review.js.map +1 -1
  167. package/dist/rule-explain.d.ts +26 -0
  168. package/dist/rule-explain.d.ts.map +1 -0
  169. package/dist/rule-explain.js +49 -0
  170. package/dist/rule-explain.js.map +1 -0
  171. package/dist/rules/codes.d.ts +5 -1
  172. package/dist/rules/codes.d.ts.map +1 -1
  173. package/dist/rules/codes.js +5 -1
  174. package/dist/rules/codes.js.map +1 -1
  175. package/dist/rules/contract-visibility.d.ts.map +1 -1
  176. package/dist/rules/contract-visibility.js +93 -21
  177. package/dist/rules/contract-visibility.js.map +1 -1
  178. package/dist/rules/data-guard.d.ts +6 -0
  179. package/dist/rules/data-guard.d.ts.map +1 -1
  180. package/dist/rules/data-guard.js +37 -0
  181. package/dist/rules/data-guard.js.map +1 -1
  182. package/dist/rules/explanations.d.ts +14 -0
  183. package/dist/rules/explanations.d.ts.map +1 -0
  184. package/dist/rules/explanations.js +316 -0
  185. package/dist/rules/explanations.js.map +1 -0
  186. package/dist/rules/legacy.d.ts.map +1 -1
  187. package/dist/rules/legacy.js +44 -2
  188. package/dist/rules/legacy.js.map +1 -1
  189. package/dist/rules/merge-auth.d.ts +12 -0
  190. package/dist/rules/merge-auth.d.ts.map +1 -0
  191. package/dist/rules/merge-auth.js +52 -0
  192. package/dist/rules/merge-auth.js.map +1 -0
  193. package/dist/rules/pages.d.ts +56 -3
  194. package/dist/rules/pages.d.ts.map +1 -1
  195. package/dist/rules/pages.js +412 -23
  196. package/dist/rules/pages.js.map +1 -1
  197. package/dist/rules/routes.d.ts +3 -2
  198. package/dist/rules/routes.d.ts.map +1 -1
  199. package/dist/rules/routes.js +4 -3
  200. package/dist/rules/routes.js.map +1 -1
  201. package/dist/rules/security.d.ts +3 -2
  202. package/dist/rules/security.d.ts.map +1 -1
  203. package/dist/rules/security.js +42 -11
  204. package/dist/rules/security.js.map +1 -1
  205. package/dist/rules/zones.d.ts.map +1 -1
  206. package/dist/rules/zones.js +34 -11
  207. package/dist/rules/zones.js.map +1 -1
  208. package/dist/stylex-migrate.d.ts.map +1 -1
  209. package/dist/stylex-migrate.js +127 -17
  210. package/dist/stylex-migrate.js.map +1 -1
  211. package/dist/test-tool.d.ts.map +1 -1
  212. package/dist/test-tool.js +3 -9
  213. package/dist/test-tool.js.map +1 -1
  214. package/dist/typecheck-coverage.d.ts +11 -0
  215. package/dist/typecheck-coverage.d.ts.map +1 -1
  216. package/dist/typecheck-coverage.js +50 -7
  217. package/dist/typecheck-coverage.js.map +1 -1
  218. package/dist/upgrade.d.ts +57 -6
  219. package/dist/upgrade.d.ts.map +1 -1
  220. package/dist/upgrade.js +247 -19
  221. package/dist/upgrade.js.map +1 -1
  222. package/dist/verification-plan.d.ts.map +1 -1
  223. package/dist/verification-plan.js +7 -2
  224. package/dist/verification-plan.js.map +1 -1
  225. package/dist/version-drift.d.ts.map +1 -1
  226. package/dist/version-drift.js +66 -3
  227. package/dist/version-drift.js.map +1 -1
  228. package/dist/web-app-options.d.ts +1 -1
  229. package/dist/web-app-options.d.ts.map +1 -1
  230. package/dist/web-app-options.js.map +1 -1
  231. package/docs/examples.json +21 -3
  232. package/docs/llms-full.txt +1083 -168
  233. package/docs/types.json +535 -112
  234. package/package.json +13 -12
  235. package/src/agent-files.ts +27 -17
  236. package/src/assure-hydration.ts +5 -1
  237. package/src/assure.ts +99 -11
  238. package/src/capabilities-tool.ts +188 -53
  239. package/src/check-diagnostics.ts +10 -0
  240. package/src/check-scan.ts +65 -12
  241. package/src/check.ts +122 -51
  242. package/src/cli.ts +127 -45
  243. package/src/client-module.ts +45 -9
  244. package/src/command-catalog.ts +92 -28
  245. package/src/dev-bun-config.ts +13 -9
  246. package/src/dev-feed-tool.ts +146 -1
  247. package/src/doctor.ts +102 -66
  248. package/src/fix-recipes.ts +101 -10
  249. package/src/i18n-check.ts +254 -18
  250. package/src/init-agents.ts +225 -44
  251. package/src/internal/config-paths.ts +154 -0
  252. package/src/internal/installed-package.ts +15 -0
  253. package/src/internal/project-specifiers.ts +78 -17
  254. package/src/internal/source-facts.ts +147 -5
  255. package/src/internal/typescript-import.ts +5 -3
  256. package/src/internal/workspace-packages.ts +218 -0
  257. package/src/introspect.ts +39 -1
  258. package/src/levels-tool.ts +7 -9
  259. package/src/load.ts +46 -4
  260. package/src/lock-links.ts +173 -0
  261. package/src/mcp-context.ts +5 -18
  262. package/src/mcp-delegate.ts +22 -10
  263. package/src/mcp-exec.ts +14 -4
  264. package/src/mcp-http.ts +1 -1
  265. package/src/mcp-io.ts +33 -0
  266. package/src/mcp-path.ts +8 -22
  267. package/src/mcp-root.ts +14 -8
  268. package/src/mcp.ts +48 -19
  269. package/src/migrate-layout.ts +1968 -245
  270. package/src/output-schemas.ts +179 -23
  271. package/src/project-path.ts +44 -13
  272. package/src/project-site.ts +18 -0
  273. package/src/recipes/4.0.0.ts +2 -0
  274. package/src/recipes/5.1.0.ts +23 -0
  275. package/src/recipes/5.2.0.ts +33 -0
  276. package/src/recipes/index.ts +7 -0
  277. package/src/release-verification.ts +20 -0
  278. package/src/review.ts +7 -0
  279. package/src/rule-explain.ts +75 -0
  280. package/src/rules/codes.ts +5 -1
  281. package/src/rules/contract-visibility.ts +118 -20
  282. package/src/rules/data-guard.ts +40 -0
  283. package/src/rules/explanations.ts +329 -0
  284. package/src/rules/legacy.ts +46 -2
  285. package/src/rules/merge-auth.ts +55 -0
  286. package/src/rules/pages.ts +486 -25
  287. package/src/rules/routes.ts +4 -3
  288. package/src/rules/security.ts +49 -11
  289. package/src/rules/zones.ts +33 -9
  290. package/src/stylex-migrate.ts +117 -17
  291. package/src/test-tool.ts +3 -9
  292. package/src/typecheck-coverage.ts +51 -6
  293. package/src/upgrade.ts +312 -19
  294. package/src/verification-plan.ts +15 -2
  295. package/src/version-drift.ts +68 -2
  296. package/src/web-app-options.ts +9 -1
package/src/i18n-check.ts CHANGED
@@ -2,14 +2,25 @@
2
2
  * `nifra i18n check [entry]` - runs `checkCatalogs()` from `@nifrajs/i18n/check` over an app's locale
3
3
  * registry and catalogs. Unlike `nifra check`, this IMPORTS app code: the entry module, which exports
4
4
  * `locales` (from `defineLocales`) and `catalogs` (by locale key: a catalog, or a function returning
5
- * one or a promise of one, such as `() => import("./messages/fr.json")`), plus an optional `ignore`.
5
+ * one or a promise of one, such as `() => import("./messages/fr.json")`), plus an optional `ignore`
6
+ * and `check`, the rest of `checkCatalogs()`'s options (`latinWords`, thresholds, `rules`).
6
7
  * The default entry is the first of {@link DEFAULT_ENTRIES} that exists.
8
+ *
9
+ * An entry may export a copy function (`defineCopy` from `@nifrajs/i18n/copy`) as well as, or instead
10
+ * of, `catalogs`. Its tables are found in the project's source with the TypeScript AST, read without
11
+ * running them, and checked as catalogs too: each under its id, its `file:line` or `copy.id()`'s.
7
12
  */
8
- import { existsSync } from "node:fs"
13
+ import { existsSync, readFileSync } from "node:fs"
9
14
  import { relative, resolve } from "node:path"
10
15
  import { pathToFileURL } from "node:url"
11
16
  import type { MessageTree } from "@nifrajs/i18n"
12
- import type { CatalogCheckOptions, CatalogCheckResult, CatalogFinding } from "@nifrajs/i18n/check"
17
+ import type {
18
+ CatalogCheckOptions,
19
+ CatalogCheckResult,
20
+ CatalogFinding,
21
+ CopyTableEntry,
22
+ } from "@nifrajs/i18n/check"
23
+ import type { CopyTableSite } from "@nifrajs/i18n/extract"
13
24
 
14
25
  export const DEFAULT_ENTRIES = [
15
26
  "shared/i18n.ts",
@@ -20,10 +31,21 @@ export const DEFAULT_ENTRIES = [
20
31
  "app/i18n.ts",
21
32
  ] as const
22
33
 
34
+ /** The copy tables a check read, when the entry exports a copy function. */
35
+ export interface CopyTablesChecked {
36
+ /** Where the copy function is defined (`shared/i18n.ts`). */
37
+ readonly definitions: readonly string[]
38
+ readonly tables: number
39
+ readonly files: number
40
+ /** Each table's id -> its `file:line`. */
41
+ readonly locations: Readonly<Record<string, string>>
42
+ }
43
+
23
44
  export interface I18nCheckOutput {
24
45
  /** The entry module, relative to the project directory. */
25
46
  readonly entry: string
26
47
  readonly result: CatalogCheckResult
48
+ readonly copy?: CopyTablesChecked
27
49
  }
28
50
 
29
51
  const isObject = (value: unknown): value is Record<string, unknown> =>
@@ -35,6 +57,15 @@ const unwrapModule = (value: unknown): unknown =>
35
57
  ? value.default
36
58
  : value
37
59
 
60
+ /** A copy function `defineCopy` returned: callable, with `pick`, `format` and its registry. */
61
+ const isCopy = (value: unknown): value is { readonly locales: unknown } =>
62
+ typeof value === "function" &&
63
+ "pick" in value &&
64
+ typeof value.pick === "function" &&
65
+ "format" in value &&
66
+ typeof value.format === "function" &&
67
+ "locales" in value
68
+
38
69
  const isRegistry = (value: unknown): value is CatalogCheckOptions["locales"] =>
39
70
  isObject(value) &&
40
71
  typeof value.default === "string" &&
@@ -57,7 +88,7 @@ function resolveEntry(cwd: string, entry: string | undefined): string {
57
88
  )
58
89
  }
59
90
 
60
- /** Import the entry, load every catalog it names, and check them. */
91
+ /** Import the entry, load every catalog it names, read the copy tables, and check them. */
61
92
  export async function runI18nCheck(
62
93
  cwd: string,
63
94
  options: { readonly entry?: string | undefined } = {},
@@ -65,36 +96,221 @@ export async function runI18nCheck(
65
96
  const path = resolveEntry(cwd, options.entry)
66
97
  const mod = (await import(pathToFileURL(path).href)) as Record<string, unknown>
67
98
  const source = isObject(mod.default) && mod.locales === undefined ? mod.default : mod
68
- const { locales, catalogs, ignore } = source
99
+ const { catalogs, ignore, check } = source
69
100
  const entry = relative(cwd, path).replaceAll("\\", "/") || path
101
+ const copies = Object.entries(source).filter(
102
+ (entry): entry is [string, { readonly locales: unknown }] => isCopy(entry[1]),
103
+ )
104
+ const locales = source.locales ?? copies[0]?.[1].locales
70
105
  if (!isRegistry(locales)) {
71
106
  throw new Error(
72
107
  `i18n check: ${entry} must export \`locales\`, the registry defineLocales() returns`,
73
108
  )
74
109
  }
75
- if (!isObject(catalogs)) {
110
+ if (!isObject(catalogs) && copies.length === 0) {
76
111
  throw new Error(
77
- `i18n check: ${entry} must export \`catalogs\`, an object of catalogs by locale key`,
112
+ `i18n check: ${entry} must export \`catalogs\`, an object of catalogs by locale key, or a copy function from defineCopy()`,
78
113
  )
79
114
  }
80
115
  if (ignore !== undefined && !isObject(ignore)) {
81
116
  throw new Error(`i18n check: \`ignore\` in ${entry} must be an object of key patterns by check`)
82
117
  }
118
+ if (check !== undefined && !isObject(check)) {
119
+ throw new Error(
120
+ `i18n check: \`check\` in ${entry} must be an object of checkCatalogs() options`,
121
+ )
122
+ }
83
123
  const loaded: Record<string, MessageTree | undefined> = {}
84
- for (const key of Object.keys(catalogs)) {
85
- let value = catalogs[key]
124
+ for (const [key, entryValue] of isObject(catalogs) ? Object.entries(catalogs) : []) {
125
+ let value = entryValue
86
126
  if (typeof value === "function") value = (value as () => unknown)()
87
127
  loaded[key] = unwrapModule(await value) as MessageTree | undefined
88
128
  }
89
129
  // Loaded on use: the command catalog imports this module on every CLI start.
90
130
  const { checkCatalogs } = await import("@nifrajs/i18n/check")
131
+ const copy =
132
+ copies.length === 0
133
+ ? undefined
134
+ : await readCopyTables(
135
+ cwd,
136
+ path,
137
+ locales,
138
+ copies.map(([name]) => name),
139
+ loaded,
140
+ )
141
+ const result = checkCatalogs({
142
+ // biome-ignore lint/plugin/requireSafetyCommentForTypeAssertion: the app's own entry module supplies these options; checkCatalogs reads each one by name.
143
+ ...(check as Omit<CatalogCheckOptions, "locales" | "catalogs"> | undefined),
144
+ locales,
145
+ catalogs: copy?.catalogs ?? loaded,
146
+ // biome-ignore lint/plugin/requireSafetyCommentForTypeAssertion: the app's own entry module supplies the patterns; checkCatalogs only compares them with keys.
147
+ ignore: (ignore ??
148
+ (isObject(check) ? check.ignore : undefined)) as CatalogCheckOptions["ignore"],
149
+ })
150
+ if (copy === undefined) return { entry, result }
151
+ const findings = [...copy.findings, ...result.findings]
91
152
  return {
92
153
  entry,
93
- result: checkCatalogs({
94
- locales,
95
- catalogs: loaded,
96
- ignore: ignore as CatalogCheckOptions["ignore"],
97
- }),
154
+ result: { ...result, findings, ok: findings.every((finding) => finding.severity !== "error") },
155
+ copy: copy.checked,
156
+ }
157
+ }
158
+
159
+ /**
160
+ * Find every table of the entry's copy functions in the project, and merge them into `loaded` as
161
+ * catalogs. Problems with the tables themselves come back as findings.
162
+ */
163
+ async function readCopyTables(
164
+ cwd: string,
165
+ entryPath: string,
166
+ locales: CatalogCheckOptions["locales"],
167
+ exportNames: readonly string[],
168
+ loaded: Readonly<Record<string, MessageTree | undefined>>,
169
+ ): Promise<{
170
+ readonly catalogs: Record<string, MessageTree | undefined>
171
+ readonly findings: readonly CatalogFinding[]
172
+ readonly checked: CopyTablesChecked
173
+ }> {
174
+ const [{ walkSource }, { loadProjectTypeScript }, { createCopyScanner }, { tablesToCatalogs }] =
175
+ await Promise.all([
176
+ import("./check-scan.ts"),
177
+ import("./internal/typescript-import.ts"),
178
+ import("@nifrajs/i18n/extract"),
179
+ import("@nifrajs/i18n/check"),
180
+ ])
181
+ const root = resolve(cwd).replaceAll("\\", "/")
182
+ const files = new Map<string, string>()
183
+ await walkSource(cwd, (file, content) => files.set(`${root}/${file}`, content), { scripts: true })
184
+ const ts = await loadProjectTypeScript(cwd)
185
+ const findings: CatalogFinding[] = []
186
+ const finding = (
187
+ severity: CatalogFinding["severity"],
188
+ code: "copy-table" | "copy-id",
189
+ key: string | undefined,
190
+ message: string,
191
+ ): void => {
192
+ const found: CatalogFinding = { severity, code, locale: locales.default, message }
193
+ findings.push(key === undefined ? found : { ...found, key })
194
+ }
195
+ const checked = (
196
+ definitions: readonly string[],
197
+ sites: readonly CopyTableSite[],
198
+ ): CopyTablesChecked => ({
199
+ definitions,
200
+ tables: sites.length,
201
+ files: new Set(sites.map((site) => site.file)).size,
202
+ // The first table under an id is the one checked; a later one is reported as reusing it.
203
+ locations: Object.fromEntries(
204
+ sites.toReversed().map((site) => [site.id, `${site.file}:${site.line}`]),
205
+ ),
206
+ })
207
+ try {
208
+ if (ts.compiler === undefined) {
209
+ finding(
210
+ "error",
211
+ "copy-table",
212
+ undefined,
213
+ "copy tables need the project's TypeScript to be read; install typescript",
214
+ )
215
+ return { catalogs: { ...loaded }, findings, checked: checked([], []) }
216
+ }
217
+ const scanner = createCopyScanner(ts.compiler, {
218
+ root,
219
+ read: (file) => {
220
+ const known = files.get(file)
221
+ if (known !== undefined) return known
222
+ try {
223
+ return readFileSync(file, "utf8")
224
+ } catch {
225
+ return undefined
226
+ }
227
+ },
228
+ })
229
+ const entryFile = entryPath.replaceAll("\\", "/")
230
+ const definitions = new Set<string>()
231
+ for (const name of exportNames) {
232
+ const ref = scanner.exportedDefinition(entryFile, name)
233
+ if (ref === undefined) {
234
+ finding(
235
+ "error",
236
+ "copy-table",
237
+ undefined,
238
+ `\`${name}\` in ${relative(cwd, entryPath)} does not lead to a \`defineCopy(...)\` constant, so its tables cannot be found`,
239
+ )
240
+ continue
241
+ }
242
+ definitions.add(`${ref.file}\u0000${ref.name}`)
243
+ const unreadable = scanner.definition(ref)?.unreadable
244
+ if (unreadable !== undefined) {
245
+ finding(
246
+ "warning",
247
+ "copy-table",
248
+ undefined,
249
+ `${ref.file}: ${unreadable}, so a production client build keeps every locale of its tables in every page`,
250
+ )
251
+ }
252
+ }
253
+ const sites = [...files.keys()]
254
+ .flatMap((file) => scanner.tables(file))
255
+ .filter((site) => definitions.has(`${site.definition.file}\u0000${site.definition.name}`))
256
+ const tables: CopyTableEntry[] = []
257
+ const seen = new Map<string, CopyTableSite>()
258
+ for (const site of sites) {
259
+ const where = `${site.file}:${site.line}`
260
+ if (site.values === undefined) {
261
+ finding(
262
+ "warning",
263
+ "copy-table",
264
+ site.id,
265
+ `${where} is not read statically (${site.unreadable}), so it is not checked and a production client build keeps every locale of it`,
266
+ )
267
+ continue
268
+ }
269
+ const first = seen.get(site.id)
270
+ if (first !== undefined) {
271
+ finding(
272
+ "error",
273
+ "copy-id",
274
+ site.id,
275
+ `${where} reuses the id of ${first.file}:${first.line}`,
276
+ )
277
+ continue
278
+ }
279
+ seen.set(site.id, site)
280
+ tables.push({ id: site.id, values: site.values })
281
+ }
282
+ const transposed = tablesToCatalogs(tables, locales)
283
+ for (const id of transposed.conflicts) {
284
+ finding("error", "copy-id", id, `${id} nests under, or over, another table's id`)
285
+ }
286
+ // Catalog keys and table ids share one namespace per locale.
287
+ const catalogs: Record<string, MessageTree | undefined> = { ...loaded }
288
+ for (const [locale, tree] of Object.entries(transposed.catalogs)) {
289
+ const own = catalogs[locale]
290
+ if (own === undefined) {
291
+ catalogs[locale] = tree
292
+ continue
293
+ }
294
+ const merged: MessageTree = { ...own }
295
+ for (const [key, value] of Object.entries(tree)) {
296
+ if (Object.hasOwn(merged, key)) {
297
+ finding("error", "copy-id", key, `the ${locale} catalog and a copy table both use ${key}`)
298
+ continue
299
+ }
300
+ merged[key] = value
301
+ }
302
+ catalogs[locale] = merged
303
+ }
304
+ return {
305
+ catalogs,
306
+ findings,
307
+ checked: checked(
308
+ [...definitions].map((key) => key.split("\u0000", 1)[0] ?? key),
309
+ sites,
310
+ ),
311
+ }
312
+ } finally {
313
+ await ts.session?.close()
98
314
  }
99
315
  }
100
316
 
@@ -125,22 +341,42 @@ const percent = (share: number): string => `${(share * 100).toFixed(share === 1
125
341
 
126
342
  /** The human report: a coverage table, then findings, grouped where they are key lists. */
127
343
  export function renderI18nCheck(out: I18nCheckOutput): readonly string[] {
128
- const { result } = out
344
+ const { result, copy } = out
129
345
  const lines = [`nifra i18n check - ${out.entry}`, ""]
346
+ if (copy !== undefined) {
347
+ lines.splice(
348
+ 1,
349
+ 0,
350
+ `${copy.tables} copy table${copy.tables === 1 ? "" : "s"} in ${copy.files} file${copy.files === 1 ? "" : "s"} (${copy.definitions.join(", ") || "no definition found"})`,
351
+ )
352
+ }
353
+ // An explicit copy.id() reads as a key; say where it is.
354
+ const located = (key: string): string => {
355
+ const at = copy?.locations[key]
356
+ return at === undefined || key.startsWith(at) ? key : `${key} (${at})`
357
+ }
130
358
  const width = Math.max(6, ...result.coverage.map((row) => row.locale.length))
131
359
  for (const row of result.coverage) {
132
360
  const label = row.default ? "default" : row.draft ? "draft" : ""
133
361
  const counts = row.default
134
362
  ? `${row.total} messages`
135
363
  : `${row.translated}/${row.total} (${percent(row.coverage)})${row.own === row.translated ? "" : `, ${row.own} own`}`
136
- lines.push(` ${row.locale.padEnd(width)} ${label.padEnd(7)} ${counts}`)
364
+ const latin =
365
+ row.latinShare === undefined || row.latinShare === 0
366
+ ? ""
367
+ : `, ${percent(row.latinShare)} Latin words`
368
+ lines.push(` ${row.locale.padEnd(width)} ${label.padEnd(7)} ${counts}${latin}`)
137
369
  }
138
370
  if (result.coverage.length > 0) lines.push("")
139
371
 
140
372
  const groups = new Map<string, CatalogFinding[]>()
141
373
  for (const finding of result.findings) {
142
374
  if (!GROUPED.has(finding.code) || finding.key === undefined) {
143
- lines.push(`${MARK[finding.severity]} ${finding.locale} ${finding.code}: ${finding.message}`)
375
+ const at = finding.key === undefined ? undefined : copy?.locations[finding.key]
376
+ const where = at === undefined || finding.message.includes(at) ? "" : ` (${at})`
377
+ lines.push(
378
+ `${MARK[finding.severity]} ${finding.locale} ${finding.code}: ${finding.message}${where}`,
379
+ )
144
380
  continue
145
381
  }
146
382
  const id = `${finding.severity}\u0000${finding.locale}\u0000${finding.code}`
@@ -150,7 +386,7 @@ export function renderI18nCheck(out: I18nCheckOutput): readonly string[] {
150
386
  }
151
387
  for (const group of groups.values()) {
152
388
  const first = group[0] as CatalogFinding
153
- const keys = group.map((finding) => finding.key as string)
389
+ const keys = group.map((finding) => located(finding.key as string))
154
390
  const shown = keys.slice(0, SHOWN_KEYS).join(", ")
155
391
  const more = keys.length > SHOWN_KEYS ? `, +${keys.length - SHOWN_KEYS} more` : ""
156
392
  lines.push(