@ultimat3/cli 31.51.0 → 31.52.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.
- package/README.md +9 -9
- package/package.json +29 -29
- package/src/catalog-layout.ts +194 -0
- package/src/cmd-i18n-spec.ts +4 -4
- package/src/cmd-i18n-split.ts +133 -0
- package/src/cmd-i18n.ts +46 -66
- package/src/config-redact.ts +20 -4
- package/src/db-rollback-pass.ts +4 -1
- package/src/dev-child-watch.ts +16 -0
- package/src/dev-db-migrate.ts +27 -46
- package/src/dev-db-migrator-hold.ts +131 -0
- package/src/dev-db-rollback.ts +11 -2
- package/src/dev-rebuild-schema.ts +7 -24
- package/src/generate-catalog-locales.ts +25 -2
- package/src/generate-follow-ups.ts +8 -3
- package/src/i18n-audit.ts +17 -33
- package/src/i18n-index.ts +108 -31
- package/src/i18n-locale-parity.ts +14 -6
- package/src/i18n-new-locale.ts +75 -0
- package/src/i18n-registration.ts +52 -27
- package/src/island-capture.ts +36 -7
- package/src/merge-state.ts +3 -2
- package/src/messages-floor.ts +2 -0
- package/src/messages.ts +2 -0
- package/src/serve-boot-telemetry.ts +106 -0
- package/src/serve-boot.ts +23 -4
- package/src/shot-blank.ts +70 -0
- package/src/templates/catalog-barrel.ts +84 -0
- package/src/templates/guard-released-forms.ts +2 -1
- package/src/templates/guard-untranslated-string.ts +27 -6
- package/src/templates/locales.ts +6 -0
- package/src/templates/scaffold-gitattributes.ts +9 -2
- package/src/templates/scaffold-i18n.ts +10 -6
- package/src/templates/shapes/offline.ts +5 -2
- package/src/test-ignores.ts +3 -2
- package/src/verify-floor.ts +9 -1
- package/src/verify-local-only.ts +66 -0
- package/src/verify-run.ts +17 -2
- package/wiki/Actions.md +1 -1
- package/wiki/Auth.md +3 -3
- package/wiki/Batching-And-Preloading.md +1 -0
- package/wiki/CLI-Reference-Data.md +241 -0
- package/wiki/CLI-Reference-Gate.md +572 -0
- package/wiki/CLI-Reference-Project.md +414 -0
- package/wiki/CLI-Reference-Ship.md +348 -0
- package/wiki/CLI-Reference-Tools.md +666 -0
- package/wiki/CLI-Reference.md +14 -2225
- package/wiki/Compared.md +8 -8
- package/wiki/Configuration.md +26 -2
- package/wiki/Deployment.md +1 -1
- package/wiki/Entities-And-Migrations.md +3 -2
- package/wiki/I18n.md +39 -5
- package/wiki/Installation.md +1 -1
- package/wiki/Jobs-And-Workflows.md +29 -1
- package/wiki/Known-Gaps.md +3 -0
- package/wiki/Observability.md +15 -1
- package/wiki/Outbound-HTTP.md +62 -2
- package/wiki/PWA-And-Offline.md +1 -1
- package/wiki/Policies-And-Authz.md +10 -0
- package/wiki/Realtime.md +29 -2
- package/wiki/Static-Assets.md +1 -1
- package/wiki/Testing.md +33 -0
- package/wiki/Theming.md +31 -8
- package/wiki/Troubleshooting.md +1 -1
- package/wiki/Upgrading.md +5 -1
package/README.md
CHANGED
|
@@ -27,8 +27,8 @@ Commands and the `x verify` step count, `As of 2026-08`:
|
|
|
27
27
|
| `x g <primitive> <name>` | scaffolds a primitive **with a passing test** | never a TODO stub |
|
|
28
28
|
| `x db gen\|migrate\|rollback\|reset\|branch\|backfill` | everything DB | `rollback [--steps n]` (default 1) runs the `-- down` half of the most recently applied migration(s) and leaves the files on disk; `gen` refuses an applied migration edited since (`X_MIGRATION_CONFLICT`) before it writes — checked against the embedded ledger only (asked of a live `x dev` when one holds it): an external `DATABASE_URL` is never dialled, so there `x db migrate`'s own audit is the refusal, and an embedded ledger it cannot read is a warning line naming why, never a refusal; `branch` = copy-on-write clone + preview URL; `backfill` dry-runs unless `--write`. The schema browser is `/_x`'s db panel (`x dev`); `studio` left the registry ([Known gaps](../../wiki/Known-Gaps.md#open-by-decision)) |
|
|
29
29
|
| `x mcp serve` | `@ultimat3/mcp`'s 19 dev tools, over stdio or HTTP | one catalog, one scope set, both transports |
|
|
30
|
-
| `x upgrade` | one major per run: exact pins, the Bun floor, `bun install`, the gate; an `AGENTS.md` byte-identical to `CLAUDE.md` becomes a symlink to it, a different one is reported in `data.agentsMd` and left | the `--json` report names the wiki section, the guards that differ and what the gate still says ([fields](../../wiki/CLI-Reference.md#x-upgrade)); `bunx @ultimat3/cli@latest upgrade` in an app that predates it; pins with no `app.config.ts` still hop, and `data.proof` says what checked them |
|
|
31
|
-
| `x doctor` | environment, ports, drift, PWA prerequisites, `APP_URL` | every finding carries a fix command; probes the port `x dev` binds ([which one](../../wiki/CLI-Reference.md#x-doctor)); the framework's deploy secrets only where the machine names `staging`/`production` |
|
|
30
|
+
| `x upgrade` | one major per run: exact pins, the Bun floor, `bun install`, the gate; an `AGENTS.md` byte-identical to `CLAUDE.md` becomes a symlink to it, a different one is reported in `data.agentsMd` and left | the `--json` report names the wiki section, the guards that differ and what the gate still says ([fields](../../wiki/CLI-Reference-Tools.md#x-upgrade)); `bunx @ultimat3/cli@latest upgrade` in an app that predates it; pins with no `app.config.ts` still hop, and `data.proof` says what checked them |
|
|
31
|
+
| `x doctor` | environment, ports, drift, PWA prerequisites, `APP_URL` | every finding carries a fix command; probes the port `x dev` binds ([which one](../../wiki/CLI-Reference-Tools.md#x-doctor)); the framework's deploy secrets only where the machine names `staging`/`production` |
|
|
32
32
|
| `x deploy` | container deploy plan | compose or helm; zero platform primitives. Compose reads `.env.production` first (`X_DEPLOY_ENV_MISSING`). Helm carries `drain.*`, `drain.workerRetireMs` as `roles.worker.retireSeconds`, and `jobs.maxInFlight` as the worker's `targetAverageValue` |
|
|
33
33
|
| `x deploy verify` | production serves this build, holds it, and a worker of it claims every queue | samples `/readyz` through the load balancer, grades `x_job_workers` when `DATABASE_URL` is set; no platform API |
|
|
34
34
|
| `x manifest` / `x routes` | generated facts | `x.manifest.json`, `openapi.json`, route table |
|
|
@@ -46,16 +46,16 @@ Commands and the `x verify` step count, `As of 2026-08`:
|
|
|
46
46
|
| `x repro <id> [--write] [--verify]` / `--list` / `--from` | a dead job or a captured 5xx as a red test in `repro/` | `--verify` answers `{ reproduced, code, fingerprint }`; a mismatch is `X_REPRO_NOT_REPRODUCED`, never a crash |
|
|
47
47
|
| `x repro <id> --bundle` | the failure as one redacted `repro/bundle-<id>.json` for an issue | secrets masked, env values / hostnames / paths replaced; `bunx create-ultimate repro <file>` opens it |
|
|
48
48
|
| `x docs "<question>"` | the framework docs and the wiki, offline | answered from the installed packages and the `wiki/` this tarball ships (`prepack`: `scripts/pack-cli-wiki.ts`), never the network; `x docs Upgrading#<anchor>` prints one whole section |
|
|
49
|
-
| `x graph [--from a --to b]` / `x impact <file\|primitive\|entity>` | the app as one typed graph (`@ultimat3/manifest`'s `buildGraph` over the loaded app, its module graph and `.x/exercise.json`) · what an edit reaches | every edge `declared`, `import` or `observed`; a miss is `X_GRAPH_NODE_UNKNOWN` with the nearest three; `x affected` in an app root adds `data.tests` by import closure ([details](../../wiki/CLI-Reference.md#x-graph--x-impact)) |
|
|
50
|
-
| `x client vendor <name>` / `x client customer --out <dir>` | one OpenAPI compiler (`openapi-compile.ts`), two emitters: a vendor's document into `apps/web/clients/<name>/` (`outbound()` + `t`), or the app's published document into a client with no framework import | every generated file is stamped with its source and body hash; `drift` raises `X_CLIENT_STALE` / `X_CLIENT_EDITED` ([flags](../../wiki/CLI-Reference.md#x-client)) |
|
|
49
|
+
| `x graph [--from a --to b]` / `x impact <file\|primitive\|entity>` | the app as one typed graph (`@ultimat3/manifest`'s `buildGraph` over the loaded app, its module graph and `.x/exercise.json`) · what an edit reaches | every edge `declared`, `import` or `observed`; a miss is `X_GRAPH_NODE_UNKNOWN` with the nearest three; `x affected` in an app root adds `data.tests` by import closure ([details](../../wiki/CLI-Reference-Tools.md#x-graph--x-impact)) |
|
|
50
|
+
| `x client vendor <name>` / `x client customer --out <dir>` | one OpenAPI compiler (`openapi-compile.ts`), two emitters: a vendor's document into `apps/web/clients/<name>/` (`outbound()` + `t`), or the app's published document into a client with no framework import | every generated file is stamped with its source and body hash; `drift` raises `X_CLIENT_STALE` / `X_CLIENT_EDITED` ([flags](../../wiki/CLI-Reference-Tools.md#x-client)) |
|
|
51
51
|
| `x fix boundary <file>` | the minimal cut for a crossed surface boundary | prints the plan and the `git mv`; never rewrites a file |
|
|
52
|
-
| `x ci [runs]` / `x ci tested` | CI triage through `gh`; `tested`: may this push reuse a green PR run of the same tree | `tested` exits 0 on every answer, `data.tested`; any doubt is `false`, a full run ([conditions](../../wiki/CLI-Reference.md#x-pr--x-ci)) |
|
|
53
|
-
| `x token create\|list\|revoke` | MCP / API tokens in `x_api_keys` | `create --scopes a,b` prints the plaintext once and stores its hash; `list --owner <id>` carries no secret; no `grant`: a wider set is a new key ([details](../../wiki/CLI-Reference.md#x-token)) |
|
|
52
|
+
| `x ci [runs]` / `x ci tested` | CI triage through `gh`; `tested`: may this push reuse a green PR run of the same tree | `tested` exits 0 on every answer, `data.tested`; any doubt is `false`, a full run ([conditions](../../wiki/CLI-Reference-Tools.md#x-pr--x-ci)) |
|
|
53
|
+
| `x token create\|list\|revoke` | MCP / API tokens in `x_api_keys` | `create --scopes a,b` prints the plaintext once and stores its hash; `list --owner <id>` carries no secret; no `grant`: a wider set is a new key ([details](../../wiki/CLI-Reference-Tools.md#x-token)) |
|
|
54
54
|
| `x config show [--section <key>]` | the resolved `app.config.ts` | defaults merged by `defineConfig`; credential keys, `secret()` values and URL passwords redacted |
|
|
55
55
|
| `x logs tail [--lines n] [--role <role>]` | the end of `.x/dev.log` or a role log | the `logs.tail` MCP tool's file and reader |
|
|
56
56
|
| `x cache graph\|bust <tag>\|clear` | the tag graph off the manifest; a bust through the app's own tiers and bus | an in-process bus is said in the summary; a failed tier is `X_CACHE_DRIVER_UNAVAILABLE` |
|
|
57
57
|
| `x explain <query> [--input <json>] [--org <id>]` | one query's statements, planned on the dev database | the `exercise` step's oracle (`sweepPlans`): an unindexed read is `X_QUERY_UNINDEXED` with the `indexes:` entry as its fix |
|
|
58
|
-
| `x status [--url <origin>] [--samples n]` | the build each live worker runs, and what each origin's `/readyz` answers | a read, exit 0; `data.unrecorded` names the roles no fleet-wide record covers ([details](../../wiki/CLI-Reference.md#x-status)) |
|
|
58
|
+
| `x status [--url <origin>] [--samples n]` | the build each live worker runs, and what each origin's `/readyz` answers | a read, exit 0; `data.unrecorded` names the roles no fleet-wide record covers ([details](../../wiki/CLI-Reference-Ship.md#x-status)) |
|
|
59
59
|
|
|
60
60
|
Everything in [CLI reference](../../wiki/CLI-Reference.md)'s planned table is also in the registry
|
|
61
61
|
and exits `X_NOT_IMPLEMENTED` with a `fix:` naming the closest shipped command — "not built yet"
|
|
@@ -349,8 +349,8 @@ one, silently. `As of 2026-11`:
|
|
|
349
349
|
| Command | Beside a live `x dev` holding `.x/pgdata` |
|
|
350
350
|
|---|---|
|
|
351
351
|
| `x db migrate` | applied BY the dev server (`POST /_x/db/migrate`: loopback Host and peer, same-origin — the command sends `Sec-Fetch-Site: same-origin`, so an app's CSRF check lets it through); the report, drift and schema dump are the same. `x dev`'s own `x db gen "…" && x db migrate` fix works as printed. A holder that is still booting (its lock claimed inside the last 120 s, or a new child) is waited for, up to 120 s, on a refused connection only |
|
|
352
|
-
| `x db migrate`, app with `database.rowSecurity` |
|
|
353
|
-
| `x db rollback [--steps n]` | run BY the dev server (`POST /_x/db/rollback`, the same doors); with `database.rowSecurity`
|
|
352
|
+
| `x db migrate`, app with `database.rowSecurity` | applied by the dev server too: it holds its one session exclusively, runs the pass as `x_migrator`, and serves as `x_app` again before any other statement runs (`dev-db-migrator-hold.ts`). A session that cannot become `x_app` again stops serving: `X_DB_ROW_SECURITY_BYPASSED`, fix `x stop --json && x dev` |
|
|
353
|
+
| `x db rollback [--steps n]` | run BY the dev server (`POST /_x/db/rollback`, the same doors); with `database.rowSecurity` under the same hold as `x_migrator` |
|
|
354
354
|
| `x db gen` | reads the ledger's checksums from the dev server (`POST /_x/db/ledger`) for its edited-migration check; one that does not answer is a printed line, never a refusal |
|
|
355
355
|
| `x db seed`, `x db backfill --write`, `x jobs retry\|cancel\|delete\|promote\|resolve-step\|pause\|resume`, `x auth seal-mfa` | `X_DEV_ALREADY_RUNNING`; the fix stops the holder, runs the command and starts `x dev` again |
|
|
356
356
|
| `x db reset\|status\|diagnostics\|recount` | `X_DEV_ALREADY_RUNNING`, as before |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/cli",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.52.0",
|
|
4
4
|
"description": "The `x` binary: new, dev, build, verify, generate, db, mcp, doctor, deploy",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -53,34 +53,34 @@
|
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
55
|
"@babel/core": "^8.1.0",
|
|
56
|
-
"@ultimat3/action": "31.
|
|
57
|
-
"@ultimat3/admin": "31.
|
|
58
|
-
"@ultimat3/ai": "31.
|
|
59
|
-
"@ultimat3/auth": "31.
|
|
60
|
-
"@ultimat3/cache": "31.
|
|
61
|
-
"@ultimat3/core": "31.
|
|
62
|
-
"@ultimat3/db": "31.
|
|
63
|
-
"@ultimat3/entity": "31.
|
|
64
|
-
"@ultimat3/flags": "31.
|
|
65
|
-
"@ultimat3/http": "31.
|
|
66
|
-
"@ultimat3/i18n": "31.
|
|
67
|
-
"@ultimat3/jobs": "31.
|
|
68
|
-
"@ultimat3/mail": "31.
|
|
69
|
-
"@ultimat3/manifest": "31.
|
|
70
|
-
"@ultimat3/mcp": "31.
|
|
71
|
-
"@ultimat3/money": "31.
|
|
72
|
-
"@ultimat3/notify": "31.
|
|
73
|
-
"@ultimat3/policy": "31.
|
|
74
|
-
"@ultimat3/pwa": "31.
|
|
75
|
-
"@ultimat3/query": "31.
|
|
76
|
-
"@ultimat3/realtime": "31.
|
|
77
|
-
"@ultimat3/render": "31.
|
|
78
|
-
"@ultimat3/schema": "31.
|
|
79
|
-
"@ultimat3/scraping": "31.
|
|
80
|
-
"@ultimat3/seo": "31.
|
|
81
|
-
"@ultimat3/storage": "31.
|
|
82
|
-
"@ultimat3/testing": "31.
|
|
83
|
-
"@ultimat3/time": "31.
|
|
56
|
+
"@ultimat3/action": "31.52.0",
|
|
57
|
+
"@ultimat3/admin": "31.52.0",
|
|
58
|
+
"@ultimat3/ai": "31.52.0",
|
|
59
|
+
"@ultimat3/auth": "31.52.0",
|
|
60
|
+
"@ultimat3/cache": "31.52.0",
|
|
61
|
+
"@ultimat3/core": "31.52.0",
|
|
62
|
+
"@ultimat3/db": "31.52.0",
|
|
63
|
+
"@ultimat3/entity": "31.52.0",
|
|
64
|
+
"@ultimat3/flags": "31.52.0",
|
|
65
|
+
"@ultimat3/http": "31.52.0",
|
|
66
|
+
"@ultimat3/i18n": "31.52.0",
|
|
67
|
+
"@ultimat3/jobs": "31.52.0",
|
|
68
|
+
"@ultimat3/mail": "31.52.0",
|
|
69
|
+
"@ultimat3/manifest": "31.52.0",
|
|
70
|
+
"@ultimat3/mcp": "31.52.0",
|
|
71
|
+
"@ultimat3/money": "31.52.0",
|
|
72
|
+
"@ultimat3/notify": "31.52.0",
|
|
73
|
+
"@ultimat3/policy": "31.52.0",
|
|
74
|
+
"@ultimat3/pwa": "31.52.0",
|
|
75
|
+
"@ultimat3/query": "31.52.0",
|
|
76
|
+
"@ultimat3/realtime": "31.52.0",
|
|
77
|
+
"@ultimat3/render": "31.52.0",
|
|
78
|
+
"@ultimat3/schema": "31.52.0",
|
|
79
|
+
"@ultimat3/scraping": "31.52.0",
|
|
80
|
+
"@ultimat3/seo": "31.52.0",
|
|
81
|
+
"@ultimat3/storage": "31.52.0",
|
|
82
|
+
"@ultimat3/testing": "31.52.0",
|
|
83
|
+
"@ultimat3/time": "31.52.0",
|
|
84
84
|
"babel-preset-solid": "^1.9.16"
|
|
85
85
|
}
|
|
86
86
|
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
// Where an app's catalogs live, per locale: one flat `catalogs/<locale>.json`, or a directory
|
|
2
|
+
// `catalogs/<locale>/` of namespace files (`billing.json`, `site/home.json`) joined by a generated
|
|
3
|
+
// `index.ts`. Every CLI reader and writer of an app's catalogs asks this module, so the two forms
|
|
4
|
+
// are one answer: read, loaded, named in a finding, and written back in the form they came in.
|
|
5
|
+
|
|
6
|
+
// why: `node:` and not Bun: `Bun.file().exists()` answers false for a directory and Bun has no
|
|
7
|
+
// directory listing, and both are how a split locale is told from a flat one; Bun has no path API
|
|
8
|
+
// at all, so `join` builds what `Bun.file` reads.
|
|
9
|
+
import { existsSync, readdirSync } from 'node:fs';
|
|
10
|
+
import { join } from 'node:path';
|
|
11
|
+
import { renderThrowable } from '@ultimat3/core';
|
|
12
|
+
import type { Catalog, Locale } from '@ultimat3/i18n';
|
|
13
|
+
import {
|
|
14
|
+
catalogFilePrefix,
|
|
15
|
+
catalogInvalid,
|
|
16
|
+
catalogLayoutMixed,
|
|
17
|
+
joinCatalogFiles,
|
|
18
|
+
loadCatalog,
|
|
19
|
+
partitionCatalog,
|
|
20
|
+
} from '@ultimat3/i18n';
|
|
21
|
+
import { catalogBarrel } from './templates/catalog-barrel';
|
|
22
|
+
import { catalogJson } from './templates/catalog-json';
|
|
23
|
+
import { CATALOG_BARREL, CATALOG_ROOT, catalogDir, catalogPath } from './templates/locales';
|
|
24
|
+
|
|
25
|
+
/** `mixed` is a locale with both — refused on every read, repaired by `x i18n split`. */
|
|
26
|
+
export type CatalogForm = 'flat' | 'split' | 'mixed';
|
|
27
|
+
|
|
28
|
+
export interface LocaleLayout {
|
|
29
|
+
readonly form: CatalogForm;
|
|
30
|
+
/** Split and mixed: every namespace file, relative to the directory, sorted. Flat: none. */
|
|
31
|
+
readonly files: readonly string[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Locale → its layout, for every locale with a catalog on disk. */
|
|
35
|
+
export type CatalogLayout = Readonly<Record<Locale, LocaleLayout>>;
|
|
36
|
+
|
|
37
|
+
/** The namespace files of a locale directory, `/`-separated whatever the host's separator. */
|
|
38
|
+
async function namespaceFiles(root: string, locale: string): Promise<readonly string[]> {
|
|
39
|
+
const files: string[] = [];
|
|
40
|
+
const cwd = join(root, catalogDir(locale));
|
|
41
|
+
for await (const entry of new Bun.Glob('**/*.json').scan({ cwd, absolute: false })) {
|
|
42
|
+
files.push(entry.replaceAll('\\', '/'));
|
|
43
|
+
}
|
|
44
|
+
return files.sort();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Every locale on disk and its form. No `catalogs/` directory yet is no locale at all. */
|
|
48
|
+
export async function readCatalogLayout(root: string): Promise<CatalogLayout> {
|
|
49
|
+
const dir = join(root, CATALOG_ROOT);
|
|
50
|
+
if (!existsSync(dir)) return {};
|
|
51
|
+
const layout: Record<string, LocaleLayout> = {};
|
|
52
|
+
const entries = readdirSync(dir, { withFileTypes: true });
|
|
53
|
+
const flat = new Set(
|
|
54
|
+
entries
|
|
55
|
+
.filter((entry) => entry.isFile() && entry.name.endsWith('.json'))
|
|
56
|
+
.map((entry) => entry.name.slice(0, -'.json'.length)),
|
|
57
|
+
);
|
|
58
|
+
for (const locale of flat) layout[locale] = { form: 'flat', files: [] };
|
|
59
|
+
for (const entry of entries.filter((one) => one.isDirectory())) {
|
|
60
|
+
const files = await namespaceFiles(root, entry.name);
|
|
61
|
+
layout[entry.name] = { form: flat.has(entry.name) ? 'mixed' : 'split', files };
|
|
62
|
+
}
|
|
63
|
+
return Object.fromEntries(Object.entries(layout).sort(([a], [b]) => (a < b ? -1 : 1)));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Where a finding points for `locale`: the flat file, or the directory a split locale is. */
|
|
67
|
+
export const catalogLocation = (layout: CatalogLayout | undefined, locale: Locale): string =>
|
|
68
|
+
layout !== undefined && Object.hasOwn(layout, locale) && layout[locale]?.form === 'split'
|
|
69
|
+
? `${catalogDir(locale)}/`
|
|
70
|
+
: catalogPath(locale);
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The one file that holds `key` in `locale`: a split locale's namespace file of the key's longest
|
|
74
|
+
* prefix, so a finding about `billing.plans.pro` sends its reader to `en/billing.json` rather than
|
|
75
|
+
* to a directory of forty files. A flat locale, or a key no file prefixes, is `catalogLocation`.
|
|
76
|
+
*/
|
|
77
|
+
export function catalogFileOf(
|
|
78
|
+
layout: CatalogLayout | undefined,
|
|
79
|
+
locale: Locale,
|
|
80
|
+
key: string,
|
|
81
|
+
): string {
|
|
82
|
+
const entry = layout !== undefined && Object.hasOwn(layout, locale) ? layout[locale] : undefined;
|
|
83
|
+
if (entry?.form !== 'split') return catalogLocation(layout, locale);
|
|
84
|
+
const owner = entry.files
|
|
85
|
+
.filter((name) => key.startsWith(`${catalogFilePrefix(name)}.`))
|
|
86
|
+
.sort((a, b) => b.length - a.length)[0];
|
|
87
|
+
return owner === undefined ? `${catalogDir(locale)}/` : `${catalogDir(locale)}/${owner}`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* A `JSON.parse` failure is still a catalog problem, not a bare-Error crash through the command —
|
|
92
|
+
* reported through the same factory a structural violation uses.
|
|
93
|
+
*/
|
|
94
|
+
function parseCatalogJson(path: string, raw: string): unknown {
|
|
95
|
+
try {
|
|
96
|
+
return JSON.parse(raw);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
throw catalogInvalid(path, renderThrowable(error));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const readJson = async (root: string, path: string): Promise<unknown> =>
|
|
103
|
+
parseCatalogJson(path, await Bun.file(join(root, path)).text());
|
|
104
|
+
|
|
105
|
+
/** The flat file's catalog: the one `x i18n split` folds into a directory. */
|
|
106
|
+
export async function loadFlatCatalog(root: string, locale: Locale): Promise<Catalog> {
|
|
107
|
+
return loadCatalog(await readJson(root, catalogPath(locale)));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** A split locale's directory as one catalog — `X_CATALOG_KEY_DUPLICATE` for a key two files hold. */
|
|
111
|
+
export async function loadSplitCatalog(
|
|
112
|
+
root: string,
|
|
113
|
+
locale: Locale,
|
|
114
|
+
files: readonly string[],
|
|
115
|
+
): Promise<Catalog> {
|
|
116
|
+
const parts = [];
|
|
117
|
+
for (const name of files) {
|
|
118
|
+
const path = `${catalogDir(locale)}/${name}`;
|
|
119
|
+
parts.push({ path, name, value: await readJson(root, path) });
|
|
120
|
+
}
|
|
121
|
+
return joinCatalogFiles(parts);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Every locale's catalog, parsed and flattened — never a bare `JSON.parse`: `loadCatalog` is the
|
|
126
|
+
* seam that fails a malformed entry loud (`X_CATALOG_INVALID`) instead of auditing it as empty.
|
|
127
|
+
* A mixed locale is `X_CATALOG_LAYOUT_MIXED`: which half ships is the question it cannot answer.
|
|
128
|
+
*/
|
|
129
|
+
export async function loadLayoutCatalogs(
|
|
130
|
+
root: string,
|
|
131
|
+
layout: CatalogLayout,
|
|
132
|
+
): Promise<Readonly<Record<Locale, Catalog>>> {
|
|
133
|
+
const catalogs: Record<string, Catalog> = {};
|
|
134
|
+
for (const [locale, { form, files }] of Object.entries(layout)) {
|
|
135
|
+
if (form === 'mixed') throw catalogLayoutMixed(locale);
|
|
136
|
+
catalogs[locale] =
|
|
137
|
+
form === 'flat'
|
|
138
|
+
? await loadFlatCatalog(root, locale)
|
|
139
|
+
: await loadSplitCatalog(root, locale, files);
|
|
140
|
+
}
|
|
141
|
+
return catalogs;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** One file a catalog write lands, root-relative. */
|
|
145
|
+
export interface CatalogWrite {
|
|
146
|
+
readonly path: string;
|
|
147
|
+
readonly contents: string;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* The files `catalog` is written as for `locale`, in `form` — each nested and sorted, the bytes
|
|
152
|
+
* `serializeCatalog` writes (`nestCatalog` walks the keys sorted): the one flat file, or one file per
|
|
153
|
+
* namespace — a key going to the existing file of its longest prefix (`files`), else to its
|
|
154
|
+
* namespace's own, created — plus the barrel over every file the directory then holds.
|
|
155
|
+
*/
|
|
156
|
+
export function catalogWrites(
|
|
157
|
+
locale: Locale,
|
|
158
|
+
catalog: Catalog,
|
|
159
|
+
form: CatalogForm,
|
|
160
|
+
files: readonly string[] = [],
|
|
161
|
+
): readonly CatalogWrite[] {
|
|
162
|
+
if (form === 'flat') return [{ path: catalogPath(locale), contents: catalogJson(catalog) }];
|
|
163
|
+
const parts = partitionCatalog(catalog, files);
|
|
164
|
+
const dir = catalogDir(locale);
|
|
165
|
+
// A file `catalog` leaves no key in is written empty, never left behind: the barrel imports it.
|
|
166
|
+
const names = [...new Set([...files, ...parts.keys()])].sort();
|
|
167
|
+
const writes = names.map((name) => ({
|
|
168
|
+
path: `${dir}/${name}`,
|
|
169
|
+
contents: catalogJson(parts.get(name) ?? {}),
|
|
170
|
+
}));
|
|
171
|
+
const barrel = catalogBarrel(names);
|
|
172
|
+
return [...writes, { path: `${dir}/${CATALOG_BARREL}`, contents: barrel }];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* `catalogWrites`, landed: only the files whose bytes change, so a sync that adds one key to
|
|
177
|
+
* `billing` leaves every other namespace file untouched. Answers the paths it wrote.
|
|
178
|
+
*/
|
|
179
|
+
export async function writeLocaleCatalog(
|
|
180
|
+
root: string,
|
|
181
|
+
locale: Locale,
|
|
182
|
+
catalog: Catalog,
|
|
183
|
+
layout: LocaleLayout | undefined,
|
|
184
|
+
): Promise<readonly string[]> {
|
|
185
|
+
const form = layout?.form === 'split' ? 'split' : 'flat';
|
|
186
|
+
const written: string[] = [];
|
|
187
|
+
for (const write of catalogWrites(locale, catalog, form, layout?.files)) {
|
|
188
|
+
const file = Bun.file(join(root, write.path));
|
|
189
|
+
if ((await file.exists()) && (await file.text()) === write.contents) continue;
|
|
190
|
+
await Bun.write(file, write.contents);
|
|
191
|
+
written.push(write.path);
|
|
192
|
+
}
|
|
193
|
+
return written;
|
|
194
|
+
}
|
package/src/cmd-i18n-spec.ts
CHANGED
|
@@ -3,14 +3,14 @@
|
|
|
3
3
|
|
|
4
4
|
import type { CommandSpec } from './parse';
|
|
5
5
|
|
|
6
|
-
export const I18N_SUBCOMMANDS = ['check', 'add', 'sync'] as const;
|
|
6
|
+
export const I18N_SUBCOMMANDS = ['check', 'add', 'sync', 'split'] as const;
|
|
7
7
|
|
|
8
8
|
export const i18nSpec: CommandSpec = {
|
|
9
9
|
name: 'i18n',
|
|
10
|
-
summary: 'catalogs: add a locale, sync keys, check for gaps',
|
|
11
|
-
usage: 'x i18n [check|add <locale>|sync <locale>] [--json]',
|
|
10
|
+
summary: 'catalogs: add a locale, sync keys, check for gaps, split into namespace files',
|
|
11
|
+
usage: 'x i18n [check|add <locale>|sync <locale>|split [<locale>]] [--json]',
|
|
12
12
|
requiresApp: true,
|
|
13
13
|
subcommands: I18N_SUBCOMMANDS,
|
|
14
|
-
// The bare `x i18n` audits; `add` and `
|
|
14
|
+
// The bare `x i18n` audits; `add`, `sync` and `split` write catalogs and must be asked for.
|
|
15
15
|
defaultSubcommand: 'check',
|
|
16
16
|
};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// `x i18n split [<locale>]`: every flat catalog, or the one named, moved into a directory of
|
|
2
|
+
// namespace files — `catalogs/<locale>/<namespace>.json` — with the barrel that joins them, the
|
|
3
|
+
// app's index importing it and `.gitattributes` merging it. Lossless: the files join back to the
|
|
4
|
+
// exact catalog that was split. Idempotent: a split locale is re-derived in place, nothing moves.
|
|
5
|
+
|
|
6
|
+
// why: Bun has no path API, so `join` builds the absolute path `Bun.file` reads and deletes.
|
|
7
|
+
import { join } from 'node:path';
|
|
8
|
+
import type { Catalog } from '@ultimat3/i18n';
|
|
9
|
+
import { catalogKeyDuplicate } from '@ultimat3/i18n';
|
|
10
|
+
import type { CatalogLayout, LocaleLayout } from './catalog-layout';
|
|
11
|
+
import {
|
|
12
|
+
catalogFileOf,
|
|
13
|
+
loadFlatCatalog,
|
|
14
|
+
loadSplitCatalog,
|
|
15
|
+
readCatalogLayout,
|
|
16
|
+
writeLocaleCatalog,
|
|
17
|
+
} from './catalog-layout';
|
|
18
|
+
import { BadFlagError } from './errors';
|
|
19
|
+
import { syncI18nIndex } from './i18n-index';
|
|
20
|
+
import { msg } from './messages';
|
|
21
|
+
import type { CommandResult } from './output';
|
|
22
|
+
import { quoteArg } from './shell-quote';
|
|
23
|
+
import { catalogPath } from './templates/locales';
|
|
24
|
+
import {
|
|
25
|
+
FLAT_CATALOG_ATTRIBUTE,
|
|
26
|
+
MERGE_ATTRIBUTES,
|
|
27
|
+
ROOT_GITATTRIBUTES_PATH,
|
|
28
|
+
} from './templates/scaffold-gitattributes';
|
|
29
|
+
|
|
30
|
+
/** What one locale's split did. */
|
|
31
|
+
interface LocaleSplit {
|
|
32
|
+
readonly locale: string;
|
|
33
|
+
/** The form it was in before this run. */
|
|
34
|
+
readonly from: LocaleLayout['form'];
|
|
35
|
+
readonly keys: number;
|
|
36
|
+
readonly files: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A mixed locale's two halves as one catalog. A key both define is refused naming the flat file
|
|
41
|
+
* and the namespace file — never resolved by picking one: which wording ships is a decision.
|
|
42
|
+
*/
|
|
43
|
+
async function mixedCatalog(root: string, locale: string, layout: CatalogLayout): Promise<Catalog> {
|
|
44
|
+
const flat = await loadFlatCatalog(root, locale);
|
|
45
|
+
const split = await loadSplitCatalog(root, locale, layout[locale]?.files ?? []);
|
|
46
|
+
for (const key of Object.keys(flat)) {
|
|
47
|
+
if (Object.hasOwn(split, key)) {
|
|
48
|
+
throw catalogKeyDuplicate(key, [catalogPath(locale), catalogFileOf(layout, locale, key)]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return { ...split, ...flat };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function splitLocale(
|
|
55
|
+
root: string,
|
|
56
|
+
locale: string,
|
|
57
|
+
layout: CatalogLayout,
|
|
58
|
+
): Promise<{
|
|
59
|
+
readonly result: LocaleSplit;
|
|
60
|
+
readonly written: readonly string[];
|
|
61
|
+
readonly removed?: string;
|
|
62
|
+
}> {
|
|
63
|
+
const entry = layout[locale];
|
|
64
|
+
if (entry === undefined) {
|
|
65
|
+
throw new BadFlagError({
|
|
66
|
+
flag: 'locale',
|
|
67
|
+
command: 'i18n',
|
|
68
|
+
reason: `no catalog for "${locale}" at ${catalogPath(locale)}`,
|
|
69
|
+
fix: `x i18n add ${quoteArg(locale)}`,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
let catalog: Catalog;
|
|
73
|
+
if (entry.form === 'flat') catalog = await loadFlatCatalog(root, locale);
|
|
74
|
+
else if (entry.form === 'mixed') catalog = await mixedCatalog(root, locale, layout);
|
|
75
|
+
else catalog = await loadSplitCatalog(root, locale, entry.files);
|
|
76
|
+
// Every namespace file first, the flat file deleted only after: a run that dies half-way leaves
|
|
77
|
+
// a mixed locale, which every read refuses and this command repairs — never a lost key.
|
|
78
|
+
const target = { form: 'split' as const, files: entry.files };
|
|
79
|
+
const written = await writeLocaleCatalog(root, locale, catalog, target);
|
|
80
|
+
if (entry.form !== 'split') await Bun.file(join(root, catalogPath(locale))).delete();
|
|
81
|
+
const files = (await readCatalogLayout(root))[locale]?.files.length ?? 0;
|
|
82
|
+
const result = { locale, from: entry.form, keys: Object.keys(catalog).length, files };
|
|
83
|
+
return entry.form === 'split'
|
|
84
|
+
? { result, written }
|
|
85
|
+
: { result, written, removed: catalogPath(locale) };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* `.gitattributes`' flat-only catalog rule, widened to the split layout — the line `x new` wrote
|
|
90
|
+
* before split catalogs existed. A file without that line (no merge rules, or rules written by
|
|
91
|
+
* hand) is left alone: `bunx x upgrade`'s `merge-drivers` codemod is what writes them.
|
|
92
|
+
*/
|
|
93
|
+
async function widenAttributes(root: string): Promise<readonly string[]> {
|
|
94
|
+
const file = Bun.file(join(root, ROOT_GITATTRIBUTES_PATH));
|
|
95
|
+
if (!(await file.exists())) return [];
|
|
96
|
+
const held = await file.text();
|
|
97
|
+
if (!held.split('\n').includes(FLAT_CATALOG_ATTRIBUTE)) return [];
|
|
98
|
+
const rules = MERGE_ATTRIBUTES.split('\n').filter((line) => line.startsWith('packages/i18n/'));
|
|
99
|
+
await Bun.write(file, held.replace(FLAT_CATALOG_ATTRIBUTE, rules.join('\n')));
|
|
100
|
+
return [ROOT_GITATTRIBUTES_PATH];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** `locale` is the positional, already validated by the caller; absent means every locale. */
|
|
104
|
+
export async function runSplit(root: string, locale: string | undefined): Promise<CommandResult> {
|
|
105
|
+
const layout = await readCatalogLayout(root);
|
|
106
|
+
const locales = locale === undefined ? Object.keys(layout) : [locale];
|
|
107
|
+
const results: LocaleSplit[] = [];
|
|
108
|
+
const written: string[] = [];
|
|
109
|
+
const removed: string[] = [];
|
|
110
|
+
for (const one of locales) {
|
|
111
|
+
const split = await splitLocale(root, one, layout);
|
|
112
|
+
results.push(split.result);
|
|
113
|
+
written.push(...split.written);
|
|
114
|
+
if (split.removed !== undefined) removed.push(split.removed);
|
|
115
|
+
}
|
|
116
|
+
// The index imports each locale in the form it is now in, and each barrel is current.
|
|
117
|
+
const index = await syncI18nIndex(root);
|
|
118
|
+
written.push(...(index.edited ?? []), ...(await widenAttributes(root)));
|
|
119
|
+
const touched = [...new Set(written)].sort();
|
|
120
|
+
const moved = results.filter((one) => one.from !== 'split').length;
|
|
121
|
+
return {
|
|
122
|
+
ok: index.findings.length === 0,
|
|
123
|
+
command: 'i18n',
|
|
124
|
+
summary: msg('cli.i18n.split', {
|
|
125
|
+
locales: results.length,
|
|
126
|
+
files: results.reduce((sum, one) => sum + one.files, 0),
|
|
127
|
+
moved,
|
|
128
|
+
}),
|
|
129
|
+
lines: [...touched.map((path) => ` ${path}`), ...removed.map((path) => ` ${path} (removed)`)],
|
|
130
|
+
findings: index.findings,
|
|
131
|
+
data: { locales: results.map((one) => ({ ...one })), written: touched, removed },
|
|
132
|
+
};
|
|
133
|
+
}
|