@verbaly/compiler 0.64.0 → 0.66.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 +12 -6
- package/dist/{claude-ByRATJ03.js → claude-BmD8W9uW.js} +29 -7
- package/dist/claude-BmD8W9uW.js.map +1 -0
- package/dist/{claude-CbKynSMc.js → claude-D-j-ZiYH.js} +29 -7
- package/dist/claude-D-j-ZiYH.js.map +1 -0
- package/dist/cli.js +336 -102
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +25 -4
- package/dist/index.js +251 -81
- package/dist/index.js.map +1 -1
- package/dist/{config-DYlbHrPc.js → text-M4XbWU6N.js} +17 -3
- package/dist/text-M4XbWU6N.js.map +1 -0
- package/package.json +4 -4
- package/dist/claude-ByRATJ03.js.map +0 -1
- package/dist/claude-CbKynSMc.js.map +0 -1
- package/dist/config-DYlbHrPc.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
1
|
+
import { a as loadConfigFile, i as loadConfig, n as findConfigFile, o as resolveConfig, s as targetLocales, t as counted } from "./text-M4XbWU6N.js";
|
|
2
|
+
import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, rmSync, watch, writeFileSync } from "node:fs";
|
|
3
3
|
import { dirname, join, relative } from "node:path";
|
|
4
4
|
import { CATALOG_SCRIPT, LOCALE_STORAGE_KEY, RICH_TAGS, VOID_TAGS, alternateLinks, createVerbaly, flatten, localeDirection, normalizeLink, parse, parseIcu, parseTags, safeAttribute } from "verbaly";
|
|
5
5
|
import { glob } from "tinyglobby";
|
|
@@ -8,6 +8,12 @@ import { createHash } from "node:crypto";
|
|
|
8
8
|
import picomatch from "picomatch";
|
|
9
9
|
import MagicString from "magic-string";
|
|
10
10
|
//#region src/catalog.ts
|
|
11
|
+
function emptyCatalog() {
|
|
12
|
+
return Object.create(null);
|
|
13
|
+
}
|
|
14
|
+
function own(catalog, key) {
|
|
15
|
+
return Object.hasOwn(catalog, key) ? catalog[key] : void 0;
|
|
16
|
+
}
|
|
11
17
|
function catalogPath(cfg, locale) {
|
|
12
18
|
return join(cfg.dir, `${locale}.json`);
|
|
13
19
|
}
|
|
@@ -34,8 +40,9 @@ function readTree(cfg, locale) {
|
|
|
34
40
|
return {};
|
|
35
41
|
}
|
|
36
42
|
const parsed = parseTree(content);
|
|
37
|
-
if (parsed)
|
|
38
|
-
throw new Error(`[verbaly] ${catalogPath(cfg, locale)} is not
|
|
43
|
+
if (parsed === void 0) throw new Error(`[verbaly] ${catalogPath(cfg, locale)} is not valid JSON, fix or delete the file`);
|
|
44
|
+
if (!isTree(parsed)) throw new Error(`[verbaly] ${catalogPath(cfg, locale)} is not a JSON object of messages, fix or delete the file`);
|
|
45
|
+
return parsed;
|
|
39
46
|
}
|
|
40
47
|
function parseCatalog(content) {
|
|
41
48
|
const tree = parseTree(content);
|
|
@@ -49,6 +56,9 @@ function parseTree(content) {
|
|
|
49
56
|
return;
|
|
50
57
|
}
|
|
51
58
|
}
|
|
59
|
+
function isTree(value) {
|
|
60
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
61
|
+
}
|
|
52
62
|
function loadCatalogs(cfg) {
|
|
53
63
|
const catalogs = {};
|
|
54
64
|
for (const locale of cfg.locales) catalogs[locale] = readCatalog(cfg, locale);
|
|
@@ -57,14 +67,17 @@ function loadCatalogs(cfg) {
|
|
|
57
67
|
function isNested(tree) {
|
|
58
68
|
return Object.values(tree).some((value) => typeof value === "object" && value !== null);
|
|
59
69
|
}
|
|
70
|
+
function group() {
|
|
71
|
+
return Object.create(null);
|
|
72
|
+
}
|
|
60
73
|
function nest(catalog) {
|
|
61
|
-
const tree =
|
|
74
|
+
const tree = group();
|
|
62
75
|
for (const [key, message] of Object.entries(catalog).sort(([a], [b]) => a < b ? -1 : 1)) {
|
|
63
76
|
const parts = key.split(".");
|
|
64
77
|
const leaf = parts[parts.length - 1];
|
|
65
78
|
let node = parts.length > 1 ? tree : void 0;
|
|
66
79
|
for (let i = 0; node && i < parts.length - 1; i++) {
|
|
67
|
-
const next = node[parts[i]] ??=
|
|
80
|
+
const next = node[parts[i]] ??= group();
|
|
68
81
|
node = typeof next === "object" && next !== null ? next : void 0;
|
|
69
82
|
}
|
|
70
83
|
if (node && typeof node[leaf] !== "object") node[leaf] = message;
|
|
@@ -73,7 +86,7 @@ function nest(catalog) {
|
|
|
73
86
|
return tree;
|
|
74
87
|
}
|
|
75
88
|
function sortTree(tree) {
|
|
76
|
-
const sorted =
|
|
89
|
+
const sorted = group();
|
|
77
90
|
for (const key of Object.keys(tree).sort()) {
|
|
78
91
|
const value = tree[key];
|
|
79
92
|
if (typeof value === "string") sorted[key] = value;
|
|
@@ -88,7 +101,7 @@ function isSorted(keys) {
|
|
|
88
101
|
function orderLike(tree, previous) {
|
|
89
102
|
const had = Object.keys(previous);
|
|
90
103
|
if (isSorted(had)) return sortTree(tree);
|
|
91
|
-
const out =
|
|
104
|
+
const out = group();
|
|
92
105
|
const take = (key) => {
|
|
93
106
|
const value = tree[key];
|
|
94
107
|
if (typeof value === "string") {
|
|
@@ -99,8 +112,8 @@ function orderLike(tree, previous) {
|
|
|
99
112
|
const before = previous[key];
|
|
100
113
|
out[key] = typeof before === "object" && before !== null ? orderLike(value, before) : sortTree(value);
|
|
101
114
|
};
|
|
102
|
-
for (const key of had) if (key
|
|
103
|
-
for (const key of Object.keys(tree).sort()) if (!(key
|
|
115
|
+
for (const key of had) if (Object.hasOwn(tree, key)) take(key);
|
|
116
|
+
for (const key of Object.keys(tree).sort()) if (!Object.hasOwn(previous, key)) take(key);
|
|
104
117
|
return out;
|
|
105
118
|
}
|
|
106
119
|
function serializeCatalog(catalog, nested = false, previous) {
|
|
@@ -115,7 +128,8 @@ function writeCatalog(cfg, locale, catalog) {
|
|
|
115
128
|
} catch {
|
|
116
129
|
mkdirSync(cfg.dir, { recursive: true });
|
|
117
130
|
}
|
|
118
|
-
const
|
|
131
|
+
const parsed = existing === void 0 ? void 0 : parseTree(existing);
|
|
132
|
+
const previous = isTree(parsed) ? parsed : void 0;
|
|
119
133
|
const serialized = serializeCatalog(catalog, wantsNesting(cfg, locale, existing, previous), previous);
|
|
120
134
|
if (existing === serialized) return serialized;
|
|
121
135
|
writeFileSync(path, serialized);
|
|
@@ -137,11 +151,6 @@ function needsRelative(catalogs) {
|
|
|
137
151
|
return false;
|
|
138
152
|
}
|
|
139
153
|
//#endregion
|
|
140
|
-
//#region src/text.ts
|
|
141
|
-
function counted(count, noun, many = `${noun}s`) {
|
|
142
|
-
return `${count} ${count === 1 ? noun : many}`;
|
|
143
|
-
}
|
|
144
|
-
//#endregion
|
|
145
154
|
//#region src/bundle.ts
|
|
146
155
|
function isExcluded(key, prefixes) {
|
|
147
156
|
return prefixes.some((prefix) => key === prefix || key.startsWith(`${prefix}.`));
|
|
@@ -151,7 +160,7 @@ function clientCatalogs(cfg, catalogs) {
|
|
|
151
160
|
if (prefixes.length === 0) return catalogs;
|
|
152
161
|
const out = {};
|
|
153
162
|
for (const [locale, catalog] of Object.entries(catalogs)) {
|
|
154
|
-
const kept =
|
|
163
|
+
const kept = emptyCatalog();
|
|
155
164
|
for (const [key, message] of Object.entries(catalog)) if (!isExcluded(key, prefixes)) kept[key] = message;
|
|
156
165
|
out[locale] = kept;
|
|
157
166
|
}
|
|
@@ -218,9 +227,10 @@ var MessageRegistry = class {
|
|
|
218
227
|
}
|
|
219
228
|
return out;
|
|
220
229
|
}
|
|
221
|
-
usedKeys() {
|
|
230
|
+
usedKeys(strict = false) {
|
|
222
231
|
const out = /* @__PURE__ */ new Map();
|
|
223
232
|
for (const analysis of this.files.values()) for (const used of analysis.usedKeys) {
|
|
233
|
+
if (strict && used.loose) continue;
|
|
224
234
|
const files = out.get(used.key) ?? [];
|
|
225
235
|
if (!files.includes(used.file)) files.push(used.file);
|
|
226
236
|
out.set(used.key, files);
|
|
@@ -305,11 +315,8 @@ function analyze(code, file, options = {}) {
|
|
|
305
315
|
return;
|
|
306
316
|
}
|
|
307
317
|
if (!isTReference(callee, names)) return;
|
|
308
|
-
const
|
|
309
|
-
if (
|
|
310
|
-
key: first.value,
|
|
311
|
-
file
|
|
312
|
-
});
|
|
318
|
+
const key = staticString(args[0]);
|
|
319
|
+
if (key !== void 0) usedKeys.push(usedKey(key, file, args[0]));
|
|
313
320
|
} else if (node.type === "JSXElement") handleTrans(code, node, file, tagged, usedKeys, names);
|
|
314
321
|
});
|
|
315
322
|
return {
|
|
@@ -323,14 +330,28 @@ function collectDeclaredKeys(node, file, out) {
|
|
|
323
330
|
for (const property of node.properties) {
|
|
324
331
|
if (property.type !== "ObjectProperty") continue;
|
|
325
332
|
const value = property.value;
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
file
|
|
330
|
-
});
|
|
333
|
+
const key = staticString(value);
|
|
334
|
+
if (key !== void 0) {
|
|
335
|
+
if (key !== "") out.push(usedKey(key, file, value));
|
|
331
336
|
} else if (value.type === "ObjectExpression") collectDeclaredKeys(value, file, out);
|
|
332
337
|
}
|
|
333
338
|
}
|
|
339
|
+
function usedKey(key, file, node) {
|
|
340
|
+
return node?.type === "StringLiteral" ? {
|
|
341
|
+
key,
|
|
342
|
+
file
|
|
343
|
+
} : {
|
|
344
|
+
key,
|
|
345
|
+
file,
|
|
346
|
+
loose: true
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
function staticString(node) {
|
|
350
|
+
if (!node) return void 0;
|
|
351
|
+
if (node.type === "StringLiteral") return node.value;
|
|
352
|
+
if (node.type === "TemplateLiteral" && node.expressions.length === 0) return cookedValue(node.quasis[0]);
|
|
353
|
+
if (node.type === "JSXExpressionContainer") return staticString(node.expression);
|
|
354
|
+
}
|
|
334
355
|
const VERBALY_PACKAGE = /^(?:verbaly|@verbaly\/[\w-]+)$/;
|
|
335
356
|
function collectStrayImports(node, file, out) {
|
|
336
357
|
const source = node.source;
|
|
@@ -370,19 +391,16 @@ function handleTrans(code, node, file, tagged, usedKeys, names) {
|
|
|
370
391
|
});
|
|
371
392
|
if (idAttr) {
|
|
372
393
|
const value = idAttr.value;
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
if (attrs.length === 1 && children?.length) {
|
|
394
|
+
const id = staticString(value);
|
|
395
|
+
if (id === void 0) return;
|
|
396
|
+
if (value?.type === "StringLiteral" && attrs.length === 1 && children?.length) {
|
|
376
397
|
const built = buildTransMessage(code, children, names);
|
|
377
398
|
if (built?.text.trim()) {
|
|
378
399
|
push(id, built);
|
|
379
400
|
return;
|
|
380
401
|
}
|
|
381
402
|
}
|
|
382
|
-
usedKeys.push(
|
|
383
|
-
key: id,
|
|
384
|
-
file
|
|
385
|
-
});
|
|
403
|
+
usedKeys.push(usedKey(id, file, value));
|
|
386
404
|
return;
|
|
387
405
|
}
|
|
388
406
|
if (attrs.length > 0) return;
|
|
@@ -728,22 +746,22 @@ async function extractProject(cfg) {
|
|
|
728
746
|
}
|
|
729
747
|
async function collectOrigins(cfg, reuse) {
|
|
730
748
|
const registry = reuse ?? await extractProject(cfg);
|
|
731
|
-
const origins =
|
|
749
|
+
const origins = Object.create(null);
|
|
732
750
|
for (const [key, files] of registry.origins()) origins[key] = files.map((file) => relative(cfg.root, file).replaceAll("\\", "/"));
|
|
733
751
|
return origins;
|
|
734
752
|
}
|
|
735
753
|
function syncCatalogs(cfg, catalogs, registry) {
|
|
736
754
|
const added = {};
|
|
737
|
-
const source = catalogs[cfg.sourceLocale] ??=
|
|
738
|
-
for (const [key, msg] of registry.messages()) if (source
|
|
755
|
+
const source = catalogs[cfg.sourceLocale] ??= emptyCatalog();
|
|
756
|
+
for (const [key, msg] of registry.messages()) if (own(source, key) !== msg.message) {
|
|
739
757
|
source[key] = msg.message;
|
|
740
758
|
(added[cfg.sourceLocale] ??= []).push(key);
|
|
741
759
|
}
|
|
742
760
|
const needed = Object.keys(source);
|
|
743
761
|
for (const locale of cfg.locales) {
|
|
744
762
|
if (locale === cfg.sourceLocale) continue;
|
|
745
|
-
const catalog = catalogs[locale] ??=
|
|
746
|
-
for (const key of needed) if (catalog
|
|
763
|
+
const catalog = catalogs[locale] ??= emptyCatalog();
|
|
764
|
+
for (const key of needed) if (own(catalog, key) === void 0) {
|
|
747
765
|
catalog[key] = "";
|
|
748
766
|
(added[locale] ??= []).push(key);
|
|
749
767
|
}
|
|
@@ -751,8 +769,9 @@ function syncCatalogs(cfg, catalogs, registry) {
|
|
|
751
769
|
return { added };
|
|
752
770
|
}
|
|
753
771
|
function pruneCatalogs(cfg, catalogs, registry) {
|
|
754
|
-
const keep = /* @__PURE__ */ new Set([...registry.messages().keys(), ...registry.usedKeys().keys()]);
|
|
755
772
|
const removed = {};
|
|
773
|
+
if (registry.parseErrors().length > 0) return removed;
|
|
774
|
+
const keep = /* @__PURE__ */ new Set([...registry.messages().keys(), ...registry.usedKeys().keys()]);
|
|
756
775
|
for (const locale of cfg.locales) {
|
|
757
776
|
const catalog = catalogs[locale];
|
|
758
777
|
if (!catalog) continue;
|
|
@@ -916,7 +935,7 @@ function reachableCategories(locale, ordinal) {
|
|
|
916
935
|
if (hit) return hit;
|
|
917
936
|
let out;
|
|
918
937
|
try {
|
|
919
|
-
const rules = new Intl.PluralRules(locale, { type });
|
|
938
|
+
const rules = new Intl.PluralRules(locale.replace(/_/g, "-"), { type });
|
|
920
939
|
const seen = /* @__PURE__ */ new Set();
|
|
921
940
|
for (let n = 0; n <= COUNT_PROBE; n++) seen.add(rules.select(n));
|
|
922
941
|
out = [...seen];
|
|
@@ -937,7 +956,7 @@ const NUMBER_FORMATS = /* @__PURE__ */ new Set([
|
|
|
937
956
|
const DATE_FORMATS = /* @__PURE__ */ new Set(["date", "time"]);
|
|
938
957
|
function collectParams(message) {
|
|
939
958
|
const out = /* @__PURE__ */ new Map();
|
|
940
|
-
visit(parse(message), out);
|
|
959
|
+
visit(parse(message, parseIcu), out);
|
|
941
960
|
return out;
|
|
942
961
|
}
|
|
943
962
|
function visit(nodes, out) {
|
|
@@ -1030,7 +1049,10 @@ ${loaders}
|
|
|
1030
1049
|
// raw catalog access: SSR integrations serialize it across the client boundary
|
|
1031
1050
|
export async function loadMessages(locale) {
|
|
1032
1051
|
if (locale === ${src}) return source;
|
|
1033
|
-
|
|
1052
|
+
// own entries only: a locale named constructor must be a miss, not the Object function
|
|
1053
|
+
const loader = Object.prototype.hasOwnProperty.call(localeLoaders, locale)
|
|
1054
|
+
? localeLoaders[locale]
|
|
1055
|
+
: undefined;
|
|
1034
1056
|
return loader ? (await loader()).default : {};
|
|
1035
1057
|
}
|
|
1036
1058
|
|
|
@@ -1072,7 +1094,7 @@ export async function setLocale(locale) {
|
|
|
1072
1094
|
${options.extraExports ?? ""}`;
|
|
1073
1095
|
}
|
|
1074
1096
|
function generateLocaleModule(catalog) {
|
|
1075
|
-
return `export default ${JSON.stringify(catalog)};\n`;
|
|
1097
|
+
return `export default ${JSON.stringify(catalog).replace(/([{,])"__proto__":/, "$1[\"__proto__\"]:")};\n`;
|
|
1076
1098
|
}
|
|
1077
1099
|
function generateDts(catalog, options = {}) {
|
|
1078
1100
|
const lines = [];
|
|
@@ -1145,7 +1167,9 @@ declare module 'virtual:verbaly/locale/*' {
|
|
|
1145
1167
|
`;
|
|
1146
1168
|
}
|
|
1147
1169
|
function writeDts(cfg, catalog, file) {
|
|
1148
|
-
file
|
|
1170
|
+
const target = file === void 0 ? cfg.dts : file;
|
|
1171
|
+
if (target === false) return;
|
|
1172
|
+
file = target ?? join(cfg.root, "verbaly.d.ts");
|
|
1149
1173
|
const content = generateDts(catalog, { inlineCatalog: cfg.render.inlineCatalog === true });
|
|
1150
1174
|
try {
|
|
1151
1175
|
if (readFileSync(file, "utf8") === content) return;
|
|
@@ -1154,6 +1178,20 @@ function writeDts(cfg, catalog, file) {
|
|
|
1154
1178
|
writeFileSync(file, content);
|
|
1155
1179
|
}
|
|
1156
1180
|
//#endregion
|
|
1181
|
+
//#region src/tag.ts
|
|
1182
|
+
function isLocaleTag(tag) {
|
|
1183
|
+
try {
|
|
1184
|
+
Intl.getCanonicalLocales(tag);
|
|
1185
|
+
return true;
|
|
1186
|
+
} catch {
|
|
1187
|
+
return false;
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
function suggestTag(tag) {
|
|
1191
|
+
const near = tag.replace(/_/g, "-");
|
|
1192
|
+
return near !== tag && isLocaleTag(near) ? near : void 0;
|
|
1193
|
+
}
|
|
1194
|
+
//#endregion
|
|
1157
1195
|
//#region src/init.ts
|
|
1158
1196
|
const HOSTS = [
|
|
1159
1197
|
{
|
|
@@ -1242,10 +1280,38 @@ function configSource(options, typescript) {
|
|
|
1242
1280
|
if (typescript) return `import type { VerbalyConfig } from '@verbaly/compiler';\n\n${body} satisfies VerbalyConfig;\n`;
|
|
1243
1281
|
return `/** @type {import('@verbaly/compiler').VerbalyConfig} */\n${body};\n`;
|
|
1244
1282
|
}
|
|
1245
|
-
async function init(
|
|
1246
|
-
const root =
|
|
1283
|
+
async function init(input = {}) {
|
|
1284
|
+
const root = input.root ?? process.cwd();
|
|
1247
1285
|
const created = [];
|
|
1248
1286
|
const skipped = [];
|
|
1287
|
+
const renamed = [];
|
|
1288
|
+
const refused = [];
|
|
1289
|
+
const typed = input.locales?.flatMap((locale) => {
|
|
1290
|
+
if (isLocaleTag(locale)) return [locale];
|
|
1291
|
+
const near = suggestTag(locale);
|
|
1292
|
+
if (near) renamed.push({
|
|
1293
|
+
from: locale,
|
|
1294
|
+
to: near
|
|
1295
|
+
});
|
|
1296
|
+
else refused.push(locale);
|
|
1297
|
+
return near ? [near] : [];
|
|
1298
|
+
});
|
|
1299
|
+
const locales = typed && [...new Set(typed)];
|
|
1300
|
+
let sourceLocale = input.sourceLocale;
|
|
1301
|
+
if (sourceLocale !== void 0 && !isLocaleTag(sourceLocale)) {
|
|
1302
|
+
const near = suggestTag(sourceLocale);
|
|
1303
|
+
if (!near) throw new Error(`[verbaly] "${sourceLocale}" is not a locale tag: pass --source with one like en or pt-BR`);
|
|
1304
|
+
renamed.push({
|
|
1305
|
+
from: sourceLocale,
|
|
1306
|
+
to: near
|
|
1307
|
+
});
|
|
1308
|
+
sourceLocale = near;
|
|
1309
|
+
}
|
|
1310
|
+
const options = {
|
|
1311
|
+
...input,
|
|
1312
|
+
locales,
|
|
1313
|
+
sourceLocale
|
|
1314
|
+
};
|
|
1249
1315
|
const existing = findConfigFile(root);
|
|
1250
1316
|
const typescript = existsSync(join(root, "tsconfig.json"));
|
|
1251
1317
|
const configFile = existing ?? (typescript ? "verbaly.config.ts" : "verbaly.config.mjs");
|
|
@@ -1278,7 +1344,9 @@ async function init(options = {}) {
|
|
|
1278
1344
|
skipped,
|
|
1279
1345
|
host: host?.name,
|
|
1280
1346
|
configFile,
|
|
1281
|
-
next
|
|
1347
|
+
next,
|
|
1348
|
+
renamed,
|
|
1349
|
+
refused
|
|
1282
1350
|
};
|
|
1283
1351
|
}
|
|
1284
1352
|
//#endregion
|
|
@@ -1291,8 +1359,9 @@ function check(cfg, catalogs, registry) {
|
|
|
1291
1359
|
const flat = {};
|
|
1292
1360
|
for (const locale of cfg.locales) flat[locale] = flatten(catalogs[locale] ?? {});
|
|
1293
1361
|
const source = flat[cfg.sourceLocale] ?? {};
|
|
1362
|
+
const used = registry.usedKeys();
|
|
1294
1363
|
const unknown = [];
|
|
1295
|
-
for (const [key, files] of registry.usedKeys()) if (!(extracted.has(key) || cfg.locales.some((locale) => flat[locale]?.[key] !== void 0))) unknown.push({
|
|
1364
|
+
for (const [key, files] of registry.usedKeys(true)) if (!(extracted.has(key) || cfg.locales.some((locale) => flat[locale]?.[key] !== void 0))) unknown.push({
|
|
1296
1365
|
key,
|
|
1297
1366
|
files
|
|
1298
1367
|
});
|
|
@@ -1320,7 +1389,7 @@ function check(cfg, catalogs, registry) {
|
|
|
1320
1389
|
issue: issue.message
|
|
1321
1390
|
});
|
|
1322
1391
|
};
|
|
1323
|
-
const sourceText =
|
|
1392
|
+
const sourceText = Object.assign(emptyCatalog(), source);
|
|
1324
1393
|
for (const [key, entry] of extracted) if (!sourceText[key]) sourceText[key] = entry.message;
|
|
1325
1394
|
for (const [key, text] of Object.entries(sourceText)) add(cfg.sourceLocale, key, validateMessage(text, cfg.sourceLocale));
|
|
1326
1395
|
for (const locale of cfg.locales) {
|
|
@@ -1332,6 +1401,18 @@ function check(cfg, catalogs, registry) {
|
|
|
1332
1401
|
if (text) add(locale, key, validatePair(text, translated));
|
|
1333
1402
|
}
|
|
1334
1403
|
}
|
|
1404
|
+
const extra = [];
|
|
1405
|
+
for (const locale of cfg.locales) {
|
|
1406
|
+
if (locale === cfg.sourceLocale) continue;
|
|
1407
|
+
for (const key of Object.keys(flat[locale] ?? {})) {
|
|
1408
|
+
if (source[key] !== void 0 || extracted.has(key)) continue;
|
|
1409
|
+
extra.push({
|
|
1410
|
+
locale,
|
|
1411
|
+
key,
|
|
1412
|
+
files: used.get(key) ?? []
|
|
1413
|
+
});
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1335
1416
|
return {
|
|
1336
1417
|
ok: gatePasses({
|
|
1337
1418
|
missing,
|
|
@@ -1340,7 +1421,8 @@ function check(cfg, catalogs, registry) {
|
|
|
1340
1421
|
}),
|
|
1341
1422
|
missing,
|
|
1342
1423
|
unknown,
|
|
1343
|
-
broken
|
|
1424
|
+
broken,
|
|
1425
|
+
extra
|
|
1344
1426
|
};
|
|
1345
1427
|
}
|
|
1346
1428
|
function formatCheckResult(result, root) {
|
|
@@ -1373,7 +1455,9 @@ function checkNextSteps(result, cliReachable = true) {
|
|
|
1373
1455
|
return steps.join("\n");
|
|
1374
1456
|
}
|
|
1375
1457
|
function formatCheckWarnings(result) {
|
|
1376
|
-
|
|
1458
|
+
const lines = result.broken.filter((entry) => entry.severity === "warning").map((entry) => ` [${entry.locale}] ${entry.key}: ${entry.issue}`);
|
|
1459
|
+
for (const entry of result.extra) lines.push(entry.files.length > 0 ? ` [${entry.locale}] ${entry.key}: only this translation has it, so the source language shows the key itself` : ` [${entry.locale}] ${entry.key}: only this translation has it, and no code reads it`);
|
|
1460
|
+
return lines.join("\n");
|
|
1377
1461
|
}
|
|
1378
1462
|
function truncate(text, max) {
|
|
1379
1463
|
return text.length > max ? text.slice(0, max - 1) + "…" : text;
|
|
@@ -1441,7 +1525,7 @@ function transformSource(code, id, registry) {
|
|
|
1441
1525
|
const analysis = analyzeFile(code, id);
|
|
1442
1526
|
registry.update(id, analysis);
|
|
1443
1527
|
if (analysis.parseError) warnParseError(id, analysis.parseError);
|
|
1444
|
-
const messages =
|
|
1528
|
+
const messages = emptyCatalog();
|
|
1445
1529
|
for (const msg of analysis.tagged) messages[msg.key] ??= msg.message;
|
|
1446
1530
|
return {
|
|
1447
1531
|
messages,
|
|
@@ -1452,6 +1536,8 @@ function runBuildGate(cfg, registry, failOnMissing) {
|
|
|
1452
1536
|
const catalogs = loadCatalogs(cfg);
|
|
1453
1537
|
for (const issue of auditBundle(cfg, catalogs, registry)) warnOnce(`${formatBundleIssue(issue)}\n fix: ${issue.fix}`, `bundle:${issue.prefix}`);
|
|
1454
1538
|
const found = check(cfg, catalogs, registry);
|
|
1539
|
+
const warnings = found.broken.filter((entry) => entry.severity === "warning").length + found.extra.length;
|
|
1540
|
+
if (warnings > 0) warnOnce(`check has ${counted(warnings, "warning")}, and the build still passes: run \`npx verbaly check\` to read them`, "gate:warnings");
|
|
1455
1541
|
const result = failOnMissing === false ? {
|
|
1456
1542
|
...found,
|
|
1457
1543
|
missing: []
|
|
@@ -1514,7 +1600,7 @@ function effectiveDrafts(drafts, catalogs) {
|
|
|
1514
1600
|
const out = {};
|
|
1515
1601
|
for (const [locale, keys] of Object.entries(drafts)) {
|
|
1516
1602
|
const catalog = catalogs[locale] ?? {};
|
|
1517
|
-
const live = keys.filter((key) => catalog
|
|
1603
|
+
const live = keys.filter((key) => own(catalog, key));
|
|
1518
1604
|
if (live.length) out[locale] = live;
|
|
1519
1605
|
}
|
|
1520
1606
|
return out;
|
|
@@ -1632,27 +1718,39 @@ async function doctor(cfg) {
|
|
|
1632
1718
|
error(`locale ${locale}`, `${rel(file)} is missing`, "run `npx verbaly extract` to create it");
|
|
1633
1719
|
continue;
|
|
1634
1720
|
}
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
const bad = badLeaf(parsed);
|
|
1638
|
-
if (bad) {
|
|
1639
|
-
catalogsHealthy = false;
|
|
1640
|
-
error(`locale ${locale}`, `${rel(file)} has a non-string value at "${bad}"`, "catalog values are text (groups of text are fine); fix the value");
|
|
1641
|
-
} else catalogs[locale] = flatten(parsed);
|
|
1642
|
-
} catch {
|
|
1721
|
+
const parsed = parseTree(readFileSync(file, "utf8"));
|
|
1722
|
+
if (parsed === void 0) {
|
|
1643
1723
|
catalogsHealthy = false;
|
|
1644
1724
|
error(`locale ${locale}`, `${rel(file)} is not valid JSON`, "repair the file (or delete it and run `npx verbaly extract`)");
|
|
1725
|
+
continue;
|
|
1645
1726
|
}
|
|
1727
|
+
if (!isTree(parsed)) {
|
|
1728
|
+
catalogsHealthy = false;
|
|
1729
|
+
error(`locale ${locale}`, `${rel(file)} is not a JSON object of messages`, "a catalog is an object of texts (groups of text are fine): fix the file");
|
|
1730
|
+
continue;
|
|
1731
|
+
}
|
|
1732
|
+
const bad = badLeaf(parsed);
|
|
1733
|
+
if (bad) {
|
|
1734
|
+
catalogsHealthy = false;
|
|
1735
|
+
error(`locale ${locale}`, `${rel(file)} has a non-string value at "${bad}"`, "catalog values are text (groups of text are fine); fix the value");
|
|
1736
|
+
} else catalogs[locale] = flatten(parsed);
|
|
1646
1737
|
}
|
|
1647
1738
|
if (catalogsHealthy) {
|
|
1648
1739
|
const how = cfg.localesDeclared ? "" : ", no locales set, so the catalog files decide";
|
|
1649
1740
|
ok("catalogs", `${counted(cfg.locales.length, "locale")} (${cfg.locales.join(", ")}) in ${rel(cfg.dir)}/${how}`);
|
|
1650
1741
|
}
|
|
1651
1742
|
}
|
|
1743
|
+
for (const locale of cfg.locales) {
|
|
1744
|
+
if (isLocaleTag(locale)) continue;
|
|
1745
|
+
const near = suggestTag(locale);
|
|
1746
|
+
const file = rel(join(cfg.dir, `${locale}.json`));
|
|
1747
|
+
warn("locales", `"${locale}" is not a locale tag, so numbers, dates and plurals in it format in a fallback`, near ? `rename it to ${near}: the file ${file} and your config` : `name it with a language tag like es or pt-BR, in ${file} and in your config`);
|
|
1748
|
+
}
|
|
1652
1749
|
const source = catalogs[cfg.sourceLocale];
|
|
1653
1750
|
if (source && Object.keys(source).length === 0) warn("source", `source catalog ${cfg.sourceLocale}.json is empty`, "write your first t`…` message and run `npx verbaly extract`");
|
|
1654
1751
|
const mirrors = cfg.render.site !== void 0 || Object.keys(cfg.render).length > 0;
|
|
1655
1752
|
if (cfg.routing === "no-prefix" && mirrors) warn("routing", "routing is \"no-prefix\" but a render section is configured, and render writes one url tree per locale", "drop the render section, or set routing to \"prefix-except-source\" so the helpers agree with the urls");
|
|
1753
|
+
else if (cfg.routing === "prefix-all" && mirrors) warn("routing", `routing is "prefix-all" but render keeps ${cfg.sourceLocale} at the root, so its links point at a /${cfg.sourceLocale}/ tree nobody wrote`, "set routing to \"prefix-except-source\", the url tree render writes");
|
|
1656
1754
|
else {
|
|
1657
1755
|
const how = (configFile ? readRoutingChoice(cfg.root, configFile) : false) ? "" : " (from your setup, no routing set)";
|
|
1658
1756
|
ok("routing", `${cfg.routing}${how}: ${ROUTING_SAYS[cfg.routing]}`);
|
|
@@ -1693,7 +1791,8 @@ async function doctor(cfg) {
|
|
|
1693
1791
|
const extracted = registry.messages();
|
|
1694
1792
|
const used = registry.usedKeys();
|
|
1695
1793
|
const orphans = Object.keys(source).filter((key) => !extracted.has(key) && !used.has(key));
|
|
1696
|
-
|
|
1794
|
+
const unread = registry.parseErrors().length > 0;
|
|
1795
|
+
if (orphans.length > 0) warn("orphans", `${counted(orphans.length, "catalog key")} no longer referenced in code (${preview(orphans)})`, unread ? "fix the files that do not parse first, they may use these, and `npx verbaly extract --prune` leaves every key alone until then" : "run `npx verbaly extract --prune` to drop them");
|
|
1697
1796
|
else ok("orphans", "no orphan keys");
|
|
1698
1797
|
}
|
|
1699
1798
|
const excluded = cfg.bundle.exclude ?? [];
|
|
@@ -1714,6 +1813,10 @@ async function doctor(cfg) {
|
|
|
1714
1813
|
const locales = [...new Set(broken.map((b) => b.locale))];
|
|
1715
1814
|
error("translations", `${counted(broken.length, "broken translation")} (${locales.join(", ")}): present but not rendering what the source renders`, "run `npx verbaly check` to read what each one lost");
|
|
1716
1815
|
}
|
|
1816
|
+
if (result.extra.length > 0) {
|
|
1817
|
+
const shown = result.extra.map((entry) => `${entry.locale}: ${entry.key}`);
|
|
1818
|
+
warn("extras", `a translation has ${counted(result.extra.length, "key")} the source catalog does not (${preview(shown)})`, scanning ? "add them to the source catalog if your code uses them, or run `npx verbaly extract --prune` to drop the rest" : "add them to the source catalog if your code uses them, or delete them from the translations");
|
|
1819
|
+
}
|
|
1717
1820
|
const warnings = result.broken.filter((entry) => entry.severity === "warning");
|
|
1718
1821
|
if (warnings.length > 0) warn("translations", `${counted(warnings.length, "structural warning")} (plural forms a language asks for)`, "run `npx verbaly check` to read them, they never fail the build");
|
|
1719
1822
|
if (result.ok) ok("translations", "all translations complete");
|
|
@@ -1780,8 +1883,8 @@ async function translateCatalogs(cfg, catalogs, provider, options = {}) {
|
|
|
1780
1883
|
};
|
|
1781
1884
|
const jobs = [];
|
|
1782
1885
|
for (const locale of targets) {
|
|
1783
|
-
const catalog = catalogs[locale] ??=
|
|
1784
|
-
const missing = Object.keys(source).filter((key) => source[key] && !catalog
|
|
1886
|
+
const catalog = catalogs[locale] ??= emptyCatalog();
|
|
1887
|
+
const missing = Object.keys(source).filter((key) => source[key] && !own(catalog, key));
|
|
1785
1888
|
if (missing.length === 0) continue;
|
|
1786
1889
|
if (options.dryRun) {
|
|
1787
1890
|
result.pending[locale] = missing;
|
|
@@ -1802,7 +1905,7 @@ async function translateCatalogs(cfg, catalogs, provider, options = {}) {
|
|
|
1802
1905
|
const { locale, keys } = job;
|
|
1803
1906
|
const catalog = catalogs[locale];
|
|
1804
1907
|
const messages = Object.fromEntries(keys.map((key) => [key, source[key]]));
|
|
1805
|
-
const origins = options.origins ? Object.fromEntries(keys.filter((key) => options.origins
|
|
1908
|
+
const origins = options.origins ? Object.fromEntries(keys.filter((key) => Object.hasOwn(options.origins, key)).map((key) => [key, options.origins[key]])) : void 0;
|
|
1806
1909
|
let out;
|
|
1807
1910
|
try {
|
|
1808
1911
|
out = await attempt(() => provider({
|
|
@@ -1843,6 +1946,19 @@ async function translateCatalogs(cfg, catalogs, provider, options = {}) {
|
|
|
1843
1946
|
function compare(a, b) {
|
|
1844
1947
|
return (a[0] ?? "").localeCompare(b[0] ?? "");
|
|
1845
1948
|
}
|
|
1949
|
+
function mergeTranslations(cfg, locale, translated, keys) {
|
|
1950
|
+
const source = readCatalog(cfg, cfg.sourceLocale);
|
|
1951
|
+
const current = readCatalog(cfg, locale);
|
|
1952
|
+
const written = [];
|
|
1953
|
+
for (const key of keys) {
|
|
1954
|
+
const text = translated[key];
|
|
1955
|
+
if (!source[key] || current[key] || !text) continue;
|
|
1956
|
+
current[key] = text;
|
|
1957
|
+
written.push(key);
|
|
1958
|
+
}
|
|
1959
|
+
if (written.length > 0) writeCatalog(cfg, locale, current);
|
|
1960
|
+
return written;
|
|
1961
|
+
}
|
|
1846
1962
|
function formatTranslateFailures(failed) {
|
|
1847
1963
|
const grouped = [];
|
|
1848
1964
|
for (const entry of failed) {
|
|
@@ -1858,7 +1974,7 @@ function formatTranslateFailures(failed) {
|
|
|
1858
1974
|
async function resolveProvider(cfg, model) {
|
|
1859
1975
|
const configured = cfg.translate.provider;
|
|
1860
1976
|
if (typeof configured === "function") return configured;
|
|
1861
|
-
const { claudeProvider } = await import("./claude-
|
|
1977
|
+
const { claudeProvider } = await import("./claude-D-j-ZiYH.js");
|
|
1862
1978
|
return claudeProvider({ model: model ?? cfg.translate.model });
|
|
1863
1979
|
}
|
|
1864
1980
|
function structureMatches(source, translated) {
|
|
@@ -2017,7 +2133,7 @@ function renderHtml(html, options) {
|
|
|
2017
2133
|
};
|
|
2018
2134
|
}
|
|
2019
2135
|
function pageSlice(v, locale, used) {
|
|
2020
|
-
const slice =
|
|
2136
|
+
const slice = Object.create(null);
|
|
2021
2137
|
for (const key of used) {
|
|
2022
2138
|
const found = v.inspect(key);
|
|
2023
2139
|
slice[key] = found?.from === locale ? found.source : "";
|
|
@@ -2028,12 +2144,16 @@ function publicPath(rel) {
|
|
|
2028
2144
|
const path = `/${rel.replace(/(^|\/)index\.html$/, "$1")}`;
|
|
2029
2145
|
return path.length > 1 ? path.replace(/\/+$/, "") : path;
|
|
2030
2146
|
}
|
|
2147
|
+
function linkFor(links, name) {
|
|
2148
|
+
return links && Object.hasOwn(links, name) ? links[name] : void 0;
|
|
2149
|
+
}
|
|
2031
2150
|
function usedLinks(nodes, links) {
|
|
2032
2151
|
const out = {};
|
|
2033
2152
|
const walk = (list) => {
|
|
2034
2153
|
for (const node of list) {
|
|
2035
2154
|
if (typeof node === "string") continue;
|
|
2036
|
-
|
|
2155
|
+
const link = linkFor(links, node.name);
|
|
2156
|
+
if (link !== void 0) out[node.name] = link;
|
|
2037
2157
|
walk(node.children);
|
|
2038
2158
|
}
|
|
2039
2159
|
};
|
|
@@ -2117,7 +2237,7 @@ function injectAlternates(ms, html, alternates) {
|
|
|
2117
2237
|
if (head) ms.appendLeft(head.index, block);
|
|
2118
2238
|
}
|
|
2119
2239
|
function injectCatalog(ms, html, messages) {
|
|
2120
|
-
const json = JSON.stringify(messages).replace(/<\//g, "<\\/");
|
|
2240
|
+
const json = JSON.stringify(messages).replace(/<\//g, "<\\/").replace(/<!--/g, "\\u003c!--");
|
|
2121
2241
|
const tag = `<script ${CATALOG_SCRIPT} type="application/json">${json}<\/script>`;
|
|
2122
2242
|
const block = `${CATALOG_OPEN}${tag}${CATALOG_CLOSE}`;
|
|
2123
2243
|
const from = html.indexOf(CATALOG_OPEN);
|
|
@@ -2303,15 +2423,18 @@ function setAttribute(ms, html, chunkStart, openEnd, attrChunk, name, value) {
|
|
|
2303
2423
|
}
|
|
2304
2424
|
function richToHtml(nodes, allowed, links) {
|
|
2305
2425
|
let out = "";
|
|
2306
|
-
for (const node of nodes)
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2426
|
+
for (const node of nodes) {
|
|
2427
|
+
const link = typeof node === "string" ? void 0 : linkFor(links, node.name);
|
|
2428
|
+
if (typeof node === "string") out += escapeHtml(node);
|
|
2429
|
+
else if (link !== void 0) {
|
|
2430
|
+
const { href, target, rel } = normalizeLink(link);
|
|
2431
|
+
let attrs = href !== void 0 ? ` href="${escapeAttr(href)}"` : "";
|
|
2432
|
+
if (target) attrs += ` target="${escapeAttr(target)}"`;
|
|
2433
|
+
if (rel) attrs += ` rel="${escapeAttr(rel)}"`;
|
|
2434
|
+
out += `<a${attrs}>${richToHtml(node.children, allowed, links)}</a>`;
|
|
2435
|
+
} else if (allowed.has(node.name)) out += VOID.has(node.name) ? `<${node.name}>` : `<${node.name}>${richToHtml(node.children, allowed, links)}</${node.name}>`;
|
|
2436
|
+
else out += richToHtml(node.children, allowed, links);
|
|
2437
|
+
}
|
|
2315
2438
|
return out;
|
|
2316
2439
|
}
|
|
2317
2440
|
function escapeHtml(text) {
|
|
@@ -2324,6 +2447,53 @@ function decodeEntities(text) {
|
|
|
2324
2447
|
return text.replace(/"/g, "\"").replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
|
|
2325
2448
|
}
|
|
2326
2449
|
//#endregion
|
|
2450
|
+
//#region src/watch.ts
|
|
2451
|
+
const NATIVE_TREE = process.platform === "darwin" || process.platform === "win32";
|
|
2452
|
+
function watchTree(root, options, onFile) {
|
|
2453
|
+
const watchers = /* @__PURE__ */ new Map();
|
|
2454
|
+
const close = (name) => {
|
|
2455
|
+
watchers.get(name)?.close();
|
|
2456
|
+
watchers.delete(name);
|
|
2457
|
+
};
|
|
2458
|
+
const open = (name, recursive, listener) => {
|
|
2459
|
+
const watcher = watch(join(root, name), {
|
|
2460
|
+
recursive,
|
|
2461
|
+
persistent: options.persistent ?? true
|
|
2462
|
+
}, listener);
|
|
2463
|
+
watcher.on("error", (error) => {
|
|
2464
|
+
close(name);
|
|
2465
|
+
console.warn(`[verbaly] stopped watching ${name || "the project"}:`, error);
|
|
2466
|
+
});
|
|
2467
|
+
watchers.set(name, watcher);
|
|
2468
|
+
};
|
|
2469
|
+
if (options.native ?? NATIVE_TREE) open("", true, (_event, file) => {
|
|
2470
|
+
if (file) onFile(file);
|
|
2471
|
+
});
|
|
2472
|
+
else {
|
|
2473
|
+
const sync = (name) => {
|
|
2474
|
+
close(name);
|
|
2475
|
+
if (name !== options.keep && (name === "node_modules" || name.startsWith("."))) return;
|
|
2476
|
+
try {
|
|
2477
|
+
if (!lstatSync(join(root, name)).isDirectory()) return;
|
|
2478
|
+
open(name, true, (_event, file) => {
|
|
2479
|
+
if (file) onFile(`${name}/${file}`);
|
|
2480
|
+
});
|
|
2481
|
+
} catch (error) {
|
|
2482
|
+
if (error.code !== "ENOENT") console.warn(`[verbaly] could not watch ${name}:`, error);
|
|
2483
|
+
}
|
|
2484
|
+
};
|
|
2485
|
+
open("", false, (event, file) => {
|
|
2486
|
+
if (!file) return;
|
|
2487
|
+
if (event === "rename") sync(file);
|
|
2488
|
+
onFile(file);
|
|
2489
|
+
});
|
|
2490
|
+
for (const entry of readdirSync(root, { withFileTypes: true })) if (entry.isDirectory()) sync(entry.name);
|
|
2491
|
+
}
|
|
2492
|
+
return () => {
|
|
2493
|
+
for (const name of [...watchers.keys()]) close(name);
|
|
2494
|
+
};
|
|
2495
|
+
}
|
|
2496
|
+
//#endregion
|
|
2327
2497
|
//#region src/wrap.ts
|
|
2328
2498
|
const WRAP_ATTRS = /* @__PURE__ */ new Set([
|
|
2329
2499
|
"title",
|
|
@@ -2606,6 +2776,6 @@ function formatCliError(error) {
|
|
|
2606
2776
|
return message.startsWith("[verbaly]") ? message : `[verbaly] ${message}`;
|
|
2607
2777
|
}
|
|
2608
2778
|
//#endregion
|
|
2609
|
-
export { DRAFTS_FILE, LOCALE_MODULE_PREFIX, MessageRegistry, RESOLVED_VIRTUAL_ID, SOURCE_FILE_RE, check, clearDrafts, clientCatalogs, collectOrigins, counted, createSourceFilter, doctor, effectiveDrafts, extractProject, formatCheckResult, formatCheckWarnings, formatCliError, formatDoctorEntry, formatRenderWarnings, formatStatusResult, formatTranslateFailures, generateDts, generateLocaleModule, generateRuntimeModule, init, isTransformTarget, loadCatalogs, loadConfig, loadDrafts, loadVirtualModule, markDrafts, needsIcu, needsRelative, parseCatalog, pruneCatalogs, readCatalog, renderSite, resolveConfig, resolveProvider, resolveVirtualId, runBuildGate, saveDrafts, stableKey, status, syncCatalogs, targetLocales, transformCode, transformSource, translateCatalogs, validateMessage, validatePair, wrapProject, writeCatalog, writeDts };
|
|
2779
|
+
export { DRAFTS_FILE, LOCALE_MODULE_PREFIX, MessageRegistry, RESOLVED_VIRTUAL_ID, SOURCE_FILE_RE, check, clearDrafts, clientCatalogs, collectOrigins, counted, createSourceFilter, doctor, effectiveDrafts, extractProject, formatCheckResult, formatCheckWarnings, formatCliError, formatDoctorEntry, formatRenderWarnings, formatStatusResult, formatTranslateFailures, generateDts, generateLocaleModule, generateRuntimeModule, init, isTransformTarget, loadCatalogs, loadConfig, loadDrafts, loadVirtualModule, markDrafts, mergeTranslations, needsIcu, needsRelative, parseCatalog, pruneCatalogs, readCatalog, renderSite, resolveConfig, resolveProvider, resolveVirtualId, runBuildGate, saveDrafts, stableKey, status, syncCatalogs, targetLocales, transformCode, transformSource, translateCatalogs, validateMessage, validatePair, watchTree, wrapProject, writeCatalog, writeDts };
|
|
2610
2780
|
|
|
2611
2781
|
//# sourceMappingURL=index.js.map
|