@ultimat3/cli 31.50.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/CLAUDE.md +1 -1
- package/README.md +9 -9
- package/package.json +29 -29
- package/src/app-boundaries.ts +2 -0
- package/src/app-dev-imports.ts +77 -0
- package/src/app-load-import-chain.ts +112 -0
- package/src/app-load-unresolved.ts +173 -16
- package/src/app-load.ts +22 -5
- 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-only-imports.ts +59 -0
- package/src/dev-rebuild-schema.ts +7 -24
- package/src/error-codes-boundary.ts +2 -0
- package/src/evaluated-walk.ts +103 -0
- 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/index.ts +2 -0
- package/src/island-capture.ts +36 -7
- package/src/mcp-errors.ts +3 -1
- package/src/merge-state.ts +3 -2
- package/src/messages-floor.ts +2 -0
- package/src/messages.ts +2 -0
- package/src/module-imports.ts +26 -15
- package/src/page-route.ts +75 -0
- package/src/scan-ways.ts +145 -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-checks.ts +7 -3
- 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 +4 -4
- 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 +58 -0
- package/wiki/Theming.md +31 -8
- package/wiki/Troubleshooting.md +2 -2
- package/wiki/Upgrading.md +6 -1
|
@@ -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
|
+
}
|
package/src/cmd-i18n.ts
CHANGED
|
@@ -1,34 +1,28 @@
|
|
|
1
|
-
// `x i18n check|add <locale>|sync <locale
|
|
1
|
+
// `x i18n check|add <locale>|sync <locale>|split [<locale>]` — the command every X_LOCALE_UNSUPPORTED,
|
|
2
2
|
// X_CATALOG_MISSING_KEYS and X_CATALOG_INVALID fix line already names. CLI wiring only: the
|
|
3
3
|
// facts come from `i18n-audit.ts`, the audit itself from `@ultimat3/i18n`'s own `auditCatalogs`.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
// one there is — `Bun.write` overwrites, so an existence check before it is a race a second
|
|
7
|
-
// `x i18n add` wins. `mkdir` comes with it because `open` does not create the parent directory
|
|
8
|
-
// `Bun.write` would, and `node:path` because Bun exposes no path API to build what either of them
|
|
9
|
-
// takes.
|
|
10
|
-
import { type FileHandle, mkdir, open } from 'node:fs/promises';
|
|
11
|
-
import { dirname, join } from 'node:path';
|
|
12
|
-
import { DEFAULT_LOCALE, stringField } from '@ultimat3/core';
|
|
5
|
+
import { DEFAULT_LOCALE } from '@ultimat3/core';
|
|
13
6
|
import type { Catalog } from '@ultimat3/i18n';
|
|
14
7
|
import { auditCatalogs, catalogFor, catalogKeys } from '@ultimat3/i18n';
|
|
15
8
|
import { adminKeyLabel, mountedAdminKeys } from './admin-catalog-keys';
|
|
16
9
|
import { loadApp } from './app-load';
|
|
17
10
|
import { requireAppRoot } from './app-root';
|
|
11
|
+
import type { CatalogLayout } from './catalog-layout';
|
|
12
|
+
import {
|
|
13
|
+
catalogLocation,
|
|
14
|
+
loadLayoutCatalogs,
|
|
15
|
+
readCatalogLayout,
|
|
16
|
+
writeLocaleCatalog,
|
|
17
|
+
} from './catalog-layout';
|
|
18
18
|
import { i18nSpec } from './cmd-i18n-spec';
|
|
19
|
+
import { runSplit } from './cmd-i18n-split';
|
|
19
20
|
import type { CliCommand, CommandContext } from './command';
|
|
20
|
-
import { BadFlagError,
|
|
21
|
-
import {
|
|
22
|
-
auditApp,
|
|
23
|
-
loadCatalogs,
|
|
24
|
-
resolveDefaultLocale,
|
|
25
|
-
scanSource,
|
|
26
|
-
seedCatalog,
|
|
27
|
-
serializeCatalog,
|
|
28
|
-
syncCatalog,
|
|
29
|
-
} from './i18n-audit';
|
|
21
|
+
import { BadFlagError, MissingPositionalError } from './errors';
|
|
22
|
+
import { auditApp, resolveDefaultLocale, scanSource, seedCatalog, syncCatalog } from './i18n-audit';
|
|
30
23
|
import { syncI18nIndex } from './i18n-index';
|
|
31
24
|
import { localeParityFindings } from './i18n-locale-parity';
|
|
25
|
+
import { writeNewLocale } from './i18n-new-locale';
|
|
32
26
|
import {
|
|
33
27
|
checkRegistration,
|
|
34
28
|
isLoudMiss,
|
|
@@ -48,41 +42,6 @@ export { I18N_SUBCOMMANDS } from './cmd-i18n-spec';
|
|
|
48
42
|
/** `ExtractReport` is plain JSON by construction — same idiom as `cmd-registries.ts`'s `asJson`. */
|
|
49
43
|
const asJson = (value: object): Record<string, JsonValue> => value as Record<string, JsonValue>;
|
|
50
44
|
|
|
51
|
-
/**
|
|
52
|
-
* `open`'s failure when the file is already there — the one errno this command translates.
|
|
53
|
-
*
|
|
54
|
-
* `stringField`, never `'code' in error && error.code`: `in` narrows for the COMPILER and promises
|
|
55
|
-
* the runtime nothing, so the read still happens on a value this process did not build and a
|
|
56
|
-
* throwing getter takes the command down one line after the guard written to make it safe. The
|
|
57
|
-
* same repair `dev-lock.ts` took for the cast spelling of the identical read.
|
|
58
|
-
*/
|
|
59
|
-
const isAlreadyExists = (error: unknown): boolean => stringField(error, 'code') === 'EEXIST';
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* The exclusive half of the create: `wx` fails rather than truncates, so an existing catalog is
|
|
63
|
-
* refused by the write itself. An `existsSync` before a `Bun.write` is the same answer with a
|
|
64
|
-
* window in it, and what falls through that window is a translator's work.
|
|
65
|
-
*/
|
|
66
|
-
async function openExclusive(absolute: string, locale: string): Promise<FileHandle> {
|
|
67
|
-
try {
|
|
68
|
-
return await open(absolute, 'wx');
|
|
69
|
-
} catch (error) {
|
|
70
|
-
if (isAlreadyExists(error)) throw new CatalogExistsError({ locale, path: catalogPath(locale) });
|
|
71
|
-
throw error;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/** `x i18n add`'s only write. `catalogs/` may not exist yet, and `open` never creates it. */
|
|
76
|
-
async function writeNewCatalog(absolute: string, locale: string, contents: string): Promise<void> {
|
|
77
|
-
await mkdir(dirname(absolute), { recursive: true });
|
|
78
|
-
const handle = await openExclusive(absolute, locale);
|
|
79
|
-
try {
|
|
80
|
-
await handle.writeFile(contents);
|
|
81
|
-
} finally {
|
|
82
|
-
await handle.close();
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
45
|
function requireLocalePositional(ctx: CommandContext, sub: string): string {
|
|
87
46
|
const raw = ctx.args.positionals[0];
|
|
88
47
|
if (raw === undefined) {
|
|
@@ -132,21 +91,31 @@ const unansweredAdminKeys = (locale: string, keys: readonly string[]): readonly
|
|
|
132
91
|
unanswered(catalogFor(locale), keys);
|
|
133
92
|
|
|
134
93
|
async function runCheck(root: string): Promise<CommandResult> {
|
|
135
|
-
const { report: audited, catalogs, extraction, ignoreUnused } = await auditApp(root);
|
|
94
|
+
const { report: audited, catalogs, extraction, ignoreUnused, layout } = await auditApp(root);
|
|
136
95
|
// Corrected once, before anything reads it: the table's `missing` column, the summary count, the
|
|
137
96
|
// findings and `--json`'s `data` are four projections of one report, and a placeholder counted in
|
|
138
97
|
// only some of them is the command disagreeing with itself about the same app.
|
|
139
98
|
const report = withPlaceholdersMissing(audited, catalogs);
|
|
140
99
|
// The runtime question, asked after the file question and never instead of it: a catalog can be
|
|
141
100
|
// complete on disk, used everywhere in source, and reach no registry at all (issue #249).
|
|
142
|
-
const registration = await checkRegistration({
|
|
101
|
+
const registration = await checkRegistration({
|
|
102
|
+
root,
|
|
103
|
+
catalogs,
|
|
104
|
+
extraction,
|
|
105
|
+
ignoreUnused,
|
|
106
|
+
layout,
|
|
107
|
+
});
|
|
143
108
|
// The two rules only a comparison with the reference locale can see: a slot one side does not
|
|
144
109
|
// have, and a string nobody translated. The same call `x verify`'s `i18n` step makes, so the
|
|
145
110
|
// command and the gate answer about an app with one voice.
|
|
146
|
-
const parity = localeParityFindings(catalogs);
|
|
111
|
+
const parity = localeParityFindings(catalogs, layout);
|
|
147
112
|
const parityLocales = new Set(parity.map((finding) => finding.meta?.['locale'])).size;
|
|
148
113
|
|
|
149
|
-
const findings: Finding[] = [
|
|
114
|
+
const findings: Finding[] = [
|
|
115
|
+
...missingKeyFindings(report, layout),
|
|
116
|
+
...registration.findings,
|
|
117
|
+
...parity,
|
|
118
|
+
];
|
|
150
119
|
|
|
151
120
|
// Whether the LOCALE has a catalog is the wrong question — the framework's own `en` is always
|
|
152
121
|
// registered, so a locale-presence column reads `yes` for an app whose every key is a loud miss.
|
|
@@ -228,12 +197,14 @@ async function runCheck(root: string): Promise<CommandResult> {
|
|
|
228
197
|
|
|
229
198
|
async function runAdd(root: string, ctx: CommandContext): Promise<CommandResult> {
|
|
230
199
|
const locale = resolveOneLocale(ctx, 'add');
|
|
231
|
-
const path = catalogPath(locale);
|
|
232
200
|
const app = await loadApp(root);
|
|
233
|
-
const
|
|
201
|
+
const layout = await readCatalogLayout(root);
|
|
202
|
+
const catalogs = await loadLayoutCatalogs(root, layout);
|
|
234
203
|
const from = resolveDefaultLocale(app.locales.defaultLocale, catalogs);
|
|
235
204
|
const seeded = seedCatalog(from === undefined ? {} : (catalogs[from] ?? {}));
|
|
236
|
-
|
|
205
|
+
// In the default locale's form: a split app's new locale is a directory of the same files.
|
|
206
|
+
const source = from === undefined ? undefined : layout[from];
|
|
207
|
+
const path = await writeNewLocale(root, locale, seeded, source);
|
|
237
208
|
// The second half of adding a locale, and without it this command SHIPPED A RED GATE: the file
|
|
238
209
|
// landed, `packages/i18n/src/index.ts` still said `locales: { en }`, and `x verify --only i18n`
|
|
239
210
|
// answered `X_CATALOG_UNREGISTERED` with a fix naming an edit that had already been made. Same
|
|
@@ -319,6 +290,7 @@ async function adminCatalog(locale: string, seeded: boolean): Promise<Catalog> {
|
|
|
319
290
|
async function backfillDefault(
|
|
320
291
|
root: string,
|
|
321
292
|
catalogs: Readonly<Record<string, Catalog>>,
|
|
293
|
+
layout: CatalogLayout,
|
|
322
294
|
from: string,
|
|
323
295
|
adminKeys: readonly string[],
|
|
324
296
|
): Promise<readonly string[]> {
|
|
@@ -330,13 +302,14 @@ async function backfillDefault(
|
|
|
330
302
|
};
|
|
331
303
|
const source = Object.fromEntries(adminKeys.map((key) => [key, real(key)]));
|
|
332
304
|
const { merged, added } = syncCatalog(target, source);
|
|
333
|
-
if (added.length > 0) await
|
|
305
|
+
if (added.length > 0) await writeLocaleCatalog(root, from, merged, layout[from]);
|
|
334
306
|
return added;
|
|
335
307
|
}
|
|
336
308
|
|
|
337
309
|
async function runSync(root: string, ctx: CommandContext): Promise<CommandResult> {
|
|
338
310
|
const locale = resolveOneLocale(ctx, 'sync');
|
|
339
|
-
const
|
|
311
|
+
const layout = await readCatalogLayout(root);
|
|
312
|
+
const catalogs = await loadLayoutCatalogs(root, layout);
|
|
340
313
|
const target = catalogs[locale];
|
|
341
314
|
if (target === undefined) {
|
|
342
315
|
throw new BadFlagError({
|
|
@@ -364,9 +337,12 @@ async function runSync(root: string, ctx: CommandContext): Promise<CommandResult
|
|
|
364
337
|
: seedCatalog(catalogs[from] ?? {})),
|
|
365
338
|
};
|
|
366
339
|
const { merged, added } = syncCatalog(target, source);
|
|
367
|
-
|
|
340
|
+
// Into the locale's own form: a split locale's key lands in its namespace file, created if new.
|
|
341
|
+
if (added.length > 0) await writeLocaleCatalog(root, locale, merged, layout[locale]);
|
|
368
342
|
const adminAdded = added.filter((key) => Object.hasOwn(admin, key));
|
|
369
|
-
const defaultAdded = seeded
|
|
343
|
+
const defaultAdded = seeded
|
|
344
|
+
? []
|
|
345
|
+
: await backfillDefault(root, catalogs, layout, from, adminAdded);
|
|
370
346
|
// Unconditional, and not only when keys moved: a catalog that reached disk some other way — a
|
|
371
347
|
// hand-created file, a `git merge` — is exactly the unregistered locale the gate refuses, and
|
|
372
348
|
// this is the command its `fix:` names. Re-deriving an index that is already correct writes the
|
|
@@ -389,7 +365,7 @@ async function runSync(root: string, ctx: CommandContext): Promise<CommandResult
|
|
|
389
365
|
from: from ?? locale,
|
|
390
366
|
added,
|
|
391
367
|
total,
|
|
392
|
-
path:
|
|
368
|
+
path: catalogLocation(layout, locale),
|
|
393
369
|
registered,
|
|
394
370
|
// Which of `added` still need a human: every one but an admin label in the default locale.
|
|
395
371
|
// `⟦key⟧` where there was no catalog to copy from, `⟦<default string>⟧` on a real merge —
|
|
@@ -410,6 +386,10 @@ export const i18nCommand: CliCommand = {
|
|
|
410
386
|
const sub = ctx.args.subcommand ?? 'check';
|
|
411
387
|
if (sub === 'add') return runAdd(root, ctx);
|
|
412
388
|
if (sub === 'sync') return runSync(root, ctx);
|
|
389
|
+
if (sub === 'split') {
|
|
390
|
+
const named = ctx.args.positionals[0] === undefined ? undefined : resolveOneLocale(ctx, sub);
|
|
391
|
+
return runSplit(root, named);
|
|
392
|
+
}
|
|
413
393
|
return runCheck(root);
|
|
414
394
|
},
|
|
415
395
|
};
|
package/src/config-redact.ts
CHANGED
|
@@ -1,13 +1,29 @@
|
|
|
1
1
|
// The resolved `AppConfig` as JSON an operator may paste into an issue: every credential-named key
|
|
2
|
-
// and every `secret()` value replaced, a URL's password cut out of its userinfo
|
|
3
|
-
//
|
|
2
|
+
// and every `secret()` value replaced, a URL's password cut out of its userinfo — a `proxy` URL's
|
|
3
|
+
// whole userinfo. The rules are core's (`isRedactedKey`, `redactUrlPassword`, `replaceUrlUserinfo`).
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
isRedactedKey,
|
|
7
|
+
isSecret,
|
|
8
|
+
REDACTED,
|
|
9
|
+
redactUrlPassword,
|
|
10
|
+
replaceUrlUserinfo,
|
|
11
|
+
} from '@ultimat3/core';
|
|
6
12
|
import type { JsonValue } from './output';
|
|
7
13
|
|
|
8
14
|
/** Past this depth a config is not data an operator wrote; the walk stops rather than recurse. */
|
|
9
15
|
const MAX_DEPTH = 16;
|
|
10
16
|
|
|
17
|
+
/**
|
|
18
|
+
* A proxy URL carries its credential in the USER as often as the password (`http://sk-token@egress`),
|
|
19
|
+
* so `outbound.proxy` — any `proxy` key — loses the whole userinfo, as core's boot line shows it.
|
|
20
|
+
*/
|
|
21
|
+
const redactedEntry = (key: string, item: unknown, depth: number): JsonValue => {
|
|
22
|
+
if (isRedactedKey(key)) return REDACTED;
|
|
23
|
+
if (key === 'proxy' && typeof item === 'string') return replaceUrlUserinfo(item, () => REDACTED);
|
|
24
|
+
return redactConfigValue(item, depth + 1);
|
|
25
|
+
};
|
|
26
|
+
|
|
11
27
|
export function redactConfigValue(value: unknown, depth = 0): JsonValue {
|
|
12
28
|
if (isSecret(value)) return REDACTED;
|
|
13
29
|
if (value === null || value === undefined) return null;
|
|
@@ -26,7 +42,7 @@ export function redactConfigValue(value: unknown, depth = 0): JsonValue {
|
|
|
26
42
|
// `defineProperty`, never `out[key] =`: a config key is the app's string, and `__proto__`
|
|
27
43
|
// assigned would re-prototype the record instead of adding a field.
|
|
28
44
|
Object.defineProperty(out, key, {
|
|
29
|
-
value:
|
|
45
|
+
value: redactedEntry(key, item, depth),
|
|
30
46
|
enumerable: true,
|
|
31
47
|
writable: true,
|
|
32
48
|
configurable: true,
|
package/src/db-rollback-pass.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// beside a live `x dev` holding the embedded database — that process, asked over `/_x`
|
|
3
3
|
// (`dev-db-rollback.ts`). The rule `db-migrate-pass.ts` states: PGlite takes one opener.
|
|
4
4
|
|
|
5
|
+
import type { DbClient } from '@ultimat3/db';
|
|
5
6
|
import { rollback } from '@ultimat3/db';
|
|
6
7
|
import { t } from '@ultimat3/schema';
|
|
7
8
|
import { DEV_ROLLBACK_PATH } from './db-dev-wire';
|
|
@@ -19,17 +20,19 @@ const REVERTED = t.object({ reverted: t.array(t.string) });
|
|
|
19
20
|
/**
|
|
20
21
|
* `rollback()` over the ambient client, with this command's two questions asked under the lock.
|
|
21
22
|
* Shared by the command and the dev route, so the holder cannot reverse what the command refuses.
|
|
23
|
+
* `on` is the dev route's held session (`dev-db-migrator-hold.ts`): its client, and no second lock.
|
|
22
24
|
*/
|
|
23
25
|
export async function reverseApplied(
|
|
24
26
|
root: string,
|
|
25
27
|
env: Env,
|
|
26
28
|
steps: number,
|
|
29
|
+
on: { readonly client?: DbClient; readonly lock?: boolean } = {},
|
|
27
30
|
): Promise<readonly string[]> {
|
|
28
31
|
const migrations = await readMigrations(root);
|
|
29
32
|
// The binding, so a database on another machine needs the names whatever the environment says.
|
|
30
33
|
const database = resolveServices(root, env).db;
|
|
31
34
|
const confirm = rollbackConfirm(await withoutDown(root, migrations), env, steps, database);
|
|
32
|
-
return rollback({ migrations, steps, confirm });
|
|
35
|
+
return rollback({ migrations, steps, confirm, client: on.client, lock: on.lock });
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
/** The ids reversed, most recently applied first. */
|
package/src/dev-child-watch.ts
CHANGED
|
@@ -153,6 +153,22 @@ export function hardExit(
|
|
|
153
153
|
};
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
+
/**
|
|
157
|
+
* A stop this process begins on its own because serving on is unsafe — the embedded session that
|
|
158
|
+
* could not be made `x_app` again after a migrate pass (`dev-db-migrator-hold.ts`). Its own hard
|
|
159
|
+
* exit, since an unsupervised `x dev` starts no watch: that drain meets a database whose turn is
|
|
160
|
+
* never given back, and a hook waiting on it must not keep the process. Exit 1 if it comes to that.
|
|
161
|
+
*/
|
|
162
|
+
export function stopDevServing(
|
|
163
|
+
signal: string,
|
|
164
|
+
exit: (code: number) => void = (code) => process.exit(code),
|
|
165
|
+
begin: (signal: string) => Promise<void> = drain,
|
|
166
|
+
afterMs: () => number = devHardExitMs,
|
|
167
|
+
): void {
|
|
168
|
+
hardExit(exit, afterMs)(1);
|
|
169
|
+
void begin(signal);
|
|
170
|
+
}
|
|
171
|
+
|
|
156
172
|
/** What the supervisor sends for a stop: the signal it would have forwarded on POSIX. */
|
|
157
173
|
export interface DevDrainMessage {
|
|
158
174
|
readonly type: 'x-dev-drain';
|