@menapo/vi18n 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -0
- package/dist/{apply-FAHl1R6T.js → apply-Bg-3rB2m.js} +4 -4
- package/dist/{captured-CTjjDF5o.js → captured-DfLk50Ji.js} +1 -1
- package/dist/{check-DwFwOB1d.js → check-CunekuYs.js} +16 -7
- package/dist/cli.js +1 -1
- package/dist/{collisions-uMTuq36M.js → collisions-CDSdtLQm.js} +2 -2
- package/dist/{compile-CEvH-6sY.js → compile-hva0qjhZ.js} +6 -6
- package/dist/config.d.ts +1 -1
- package/dist/config.js +2 -2
- package/dist/{diagnostics-1onDF6Ua.js → diagnostics-LgIWyeSp.js} +2 -1
- package/dist/{duplicates-CsUXRX2J.js → duplicates-8cudcHNj.js} +1 -1
- package/dist/{engine-Bf7nVigL.js → engine-ecRD8-IE.js} +11 -11
- package/dist/engine.d.ts +1 -1
- package/dist/{exchange-DYG9RZoT.js → exchange-DjwlrTQ9.js} +3 -3
- package/dist/{exclude-vMnm3lqU.js → exclude-BFETmvde.js} +1 -1
- package/dist/{export-BYq0gfLd.js → export-D2tsqWIr.js} +6 -6
- package/dist/{extract-CYtuEo8u.js → extract-BxYchVmY.js} +7 -7
- package/dist/{formats-ChV1LVH_.js → formats-BaryZ2N7.js} +3 -3
- package/dist/{i18next-D66Icz3L.js → i18next-COO6o0tX.js} +6 -6
- package/dist/i18next-Dr7DCQlQ.js +1 -0
- package/dist/{import-BApbQlmm.js → import-vSC29Cn4.js} +6 -6
- package/dist/index.d.ts +1 -1
- package/dist/{init-J7AwwA5e.js → init-Cc7A-8VX.js} +9 -9
- package/dist/{library-BfSOBeBd.js → library-tp_xqzSY.js} +2 -2
- package/dist/{lingui-Ee2farVo.js → lingui-BnnlqGXT.js} +8 -8
- package/dist/lint.js +7 -6
- package/dist/{load-BFLLqY8x.js → load-BSIeGJ5v.js} +8 -6
- package/dist/{load-CMwhukZ4.js → load-SSrAeJVB.js} +5 -3
- package/dist/{locales-DxLDrx-h.js → locales-D0U-yVRm.js} +4 -4
- package/dist/{lsp-BC7xDGON.js → lsp-DvC11pBZ.js} +2 -2
- package/dist/{mcp-Cka8ywDF.js → mcp-D2zzOTBy.js} +2 -2
- package/dist/{merge-driver-B04Apm29.js → merge-driver-BLI8OtAc.js} +6 -6
- package/dist/migrate-1r43OBfe.js +1 -0
- package/dist/{migrate-D7TNl6SD.js → migrate-ChDGhlC-.js} +11 -11
- package/dist/{openai-RWw0YlAx.js → openai-CLn4puY8.js} +1 -1
- package/dist/{output-CF9uvk_-.js → output-D5tAt8q4.js} +3 -3
- package/dist/parse.d.ts +1 -1
- package/dist/{pipeline-sgl_q_op.js → pipeline-BJSkkRYC.js} +7 -7
- package/dist/{pipeline-B30irGMD.js → pipeline-BeCc441z.js} +1 -1
- package/dist/{project-DSzWKU3C.js → project-B5KIO0q-.js} +9 -9
- package/dist/{pseudo-DNbNNVGP.js → pseudo-B2DUpoLu.js} +2 -1
- package/dist/{react-intl-tzGIk6G3.js → react-intl-C8JehBBL.js} +6 -6
- package/dist/rename-ZVGq9ZG1.js +577 -0
- package/dist/repeated-uuI1tUGg.js +85 -0
- package/dist/{restore-BG_zMpSj.js → restore-xlBIrtRt.js} +8 -8
- package/dist/{rewrite-hAcRJwiZ.js → rewrite-BwzXoBiA.js} +1 -1
- package/dist/{run-CFzR3V93.js → run-CQ9pDLSa.js} +50 -23
- package/dist/scan-worker.d.ts +1 -1
- package/dist/scan-worker.js +1 -1
- package/dist/{server-ceuzzkal.js → server-Br8tAuG2.js} +28 -8
- package/dist/{server-WlxZH0oX.js → server-DJRpUQVZ.js} +793 -12
- package/dist/server.d.ts +1 -1
- package/dist/server.js +7 -1
- package/dist/{sitemap-796jJKUT.js → sitemap-CSAkm6oi.js} +4 -4
- package/dist/{sources-DuOdM02f.js → sources-DNVbYsgs.js} +4 -3
- package/dist/{status-CTH3qI-0.js → status-q6M3rBrh.js} +3 -3
- package/dist/{sync-CpL0Qsgw.js → sync-BNIAwMhR.js} +6 -6
- package/dist/test.d.ts +1 -1
- package/dist/test.js +53 -12
- package/dist/{translate-BrfVRkjl.js → translate-BP4iVhOR.js} +6 -6
- package/dist/{types-BaG1LboH.d.ts → types-B4wNX6PA.d.ts} +4 -2
- package/dist/{types-Cy2bPdSg.d.ts → types-_4UdFhR9.d.ts} +10 -1
- package/dist/{types-Ds_vebja.js → types-xR2exI3L.js} +1 -0
- package/dist/{units-VVLgQ7x7.js → units-DPPfCN7j.js} +3 -3
- package/dist/{validate-zv4-IZyZ.js → validate-CgzaDdOY.js} +2 -2
- package/dist/{version-BFs370Ce.js → version-DC-J5WvV.js} +2 -2
- package/dist/{vite-scan-DFKSGVE9.js → vite-scan-kM8Ta3rO.js} +6 -6
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +20 -16
- package/dist/{vue-i18n-CBmZqHbG.js → vue-i18n-_uIXlFVh.js} +6 -6
- package/dist/{which-BpyJyBkX.js → which-DFWk_KwN.js} +8 -8
- package/dist/{worker-BjzVqJVO.js → worker-zlhNj4y3.js} +4 -4
- package/package.json +1 -1
- package/dist/i18next-1waHziMy.js +0 -1
- package/dist/migrate-khCY8TE0.js +0 -1
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { i as DEFAULT_CONFIG, n as isDiagnosticError, t as diagnosticError } from "./diagnostics-
|
|
2
|
-
import { r as resolveDuplicates } from "./duplicates-
|
|
3
|
-
import { A as usableTranslation, C as emptyCatalog, E as entryKey, L as compareText, M as sourceMatches, h as updateCatalogFile, o as catalogLayout, s as catalogPath, x as createEntry } from "./library-
|
|
4
|
-
import { b as
|
|
5
|
-
import { t as VERSION } from "./version-
|
|
1
|
+
import { i as DEFAULT_CONFIG, n as isDiagnosticError, t as diagnosticError } from "./diagnostics-LgIWyeSp.js";
|
|
2
|
+
import { r as resolveDuplicates } from "./duplicates-8cudcHNj.js";
|
|
3
|
+
import { A as usableTranslation, C as emptyCatalog, E as entryKey, L as compareText, M as sourceMatches, h as updateCatalogFile, o as catalogLayout, s as catalogPath, x as createEntry } from "./library-tp_xqzSY.js";
|
|
4
|
+
import { b as printICU, g as isManualId, p as createCodec, r as validateTranslation, t as pluralCategories, x as parseICU, y as normalizeSource } from "./validate-CgzaDdOY.js";
|
|
5
|
+
import { t as VERSION } from "./version-DC-J5WvV.js";
|
|
6
6
|
import { t as adaptiveConcurrency } from "./concurrency-Dy2ny50V.js";
|
|
7
|
-
import { t as excludeKind } from "./exclude-
|
|
8
|
-
import { _ as tokenCount, a as answerItems, c as exchange, d as providerError, f as readKey, g as sleep, h as retryAfterDelay, i as MAX_DELAY_MS, l as field, m as requireKey, o as costAt, p as requestText, r as openaiPreset, s as detail, u as parseBaseURL } from "./openai-
|
|
7
|
+
import { t as excludeKind } from "./exclude-BFETmvde.js";
|
|
8
|
+
import { _ as tokenCount, a as answerItems, c as exchange, d as providerError, f as readKey, g as sleep, h as retryAfterDelay, i as MAX_DELAY_MS, l as field, m as requireKey, o as costAt, p as requestText, r as openaiPreset, s as detail, u as parseBaseURL } from "./openai-CLn4puY8.js";
|
|
9
9
|
// src/ai/mask.ts
|
|
10
10
|
/**
|
|
11
11
|
* Tokens already present in the text come first, so a literal `@@PH1@@` is
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { t as MAX_RETRIES, n as catalogPersister, r as createProvider, i as estimateUsage, a as firstRequests, o as planTranslations, s as replayProvider, c as runTranslations } from "./pipeline-
|
|
1
|
+
export { t as MAX_RETRIES, n as catalogPersister, r as createProvider, i as estimateUsage, a as firstRequests, o as planTranslations, s as replayProvider, c as runTranslations } from "./pipeline-BJSkkRYC.js";
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { A as usableTranslation, E as entryKey, i as libraryTranslation, l as loadCatalogSet, o as catalogLayout, s as catalogPath } from "./library-
|
|
2
|
-
import {
|
|
3
|
-
import { r as loadConfig } from "./load-
|
|
4
|
-
import { n as isDiagnosticError } from "./diagnostics-
|
|
5
|
-
import { i as genFileUpToDate, n as syncCatalogs, r as GEN_FILE, s as scanProject } from "./sync-
|
|
6
|
-
import { i as rootRelative, o as errorMessage, r as plural } from "./output-
|
|
7
|
-
import { r as resolveDuplicates, t as duplicateDiagnostics } from "./duplicates-
|
|
8
|
-
import { r as EXIT } from "./types-
|
|
1
|
+
import { A as usableTranslation, E as entryKey, i as libraryTranslation, l as loadCatalogSet, o as catalogLayout, s as catalogPath } from "./library-tp_xqzSY.js";
|
|
2
|
+
import { g as isManualId, p as createCodec, r as validateTranslation } from "./validate-CgzaDdOY.js";
|
|
3
|
+
import { r as loadConfig } from "./load-BSIeGJ5v.js";
|
|
4
|
+
import { n as isDiagnosticError } from "./diagnostics-LgIWyeSp.js";
|
|
5
|
+
import { i as genFileUpToDate, n as syncCatalogs, r as GEN_FILE, s as scanProject } from "./sync-BNIAwMhR.js";
|
|
6
|
+
import { i as rootRelative, o as errorMessage, r as plural } from "./output-D5tAt8q4.js";
|
|
7
|
+
import { r as resolveDuplicates, t as duplicateDiagnostics } from "./duplicates-8cudcHNj.js";
|
|
8
|
+
import { r as EXIT } from "./types-xR2exI3L.js";
|
|
9
9
|
import { dirname, join } from "node:path";
|
|
10
10
|
import { readFile } from "node:fs/promises";
|
|
11
11
|
// src/cli/diagnostics.ts
|
|
@@ -262,7 +262,7 @@ async function loadProject(ctx) {
|
|
|
262
262
|
* compiler, empty root) ends the command.
|
|
263
263
|
*/
|
|
264
264
|
async function scan(project, options = {}) {
|
|
265
|
-
const result = options.vite === true ? await (await import("./vite-scan-
|
|
265
|
+
const result = options.vite === true ? await (await import("./vite-scan-kM8Ta3rO.js")).scanWithVite(project.config) : await scanProject(project.config, project.codec, { cache: true });
|
|
266
266
|
if (result.aborted) throw new CliExit(EXIT.failure, result.diagnostics.filter(isAbortCause));
|
|
267
267
|
return result;
|
|
268
268
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { n as isDiagnosticError } from "./diagnostics-
|
|
2
|
-
import { C as langFromId, w as parseModule } from "./sources-
|
|
3
|
-
import {
|
|
4
|
-
import { a as annotateHandlers, g as formatjsId, h as formatjsAstToIcu, l as reactTypes, t as MODULE_EXTENSIONS, v as parseJson } from "./i18next-
|
|
5
|
-
import { B as objectProperties, C as attributeString, D as inJsxChildren, E as identifierName, F as jsxNameNodes, M as jsxAttributes, P as jsxName, S as attributeExpression, V as stringValue, W as unwrap, w as childEntries, x as analyzeModule, z as memberParts } from "./migrate-
|
|
6
|
-
import { t as ModuleBuilder } from "./rewrite-
|
|
1
|
+
import { n as isDiagnosticError } from "./diagnostics-LgIWyeSp.js";
|
|
2
|
+
import { C as langFromId, w as parseModule } from "./sources-DNVbYsgs.js";
|
|
3
|
+
import { b as printICU } from "./validate-CgzaDdOY.js";
|
|
4
|
+
import { a as annotateHandlers, g as formatjsId, h as formatjsAstToIcu, l as reactTypes, t as MODULE_EXTENSIONS, v as parseJson } from "./i18next-COO6o0tX.js";
|
|
5
|
+
import { B as objectProperties, C as attributeString, D as inJsxChildren, E as identifierName, F as jsxNameNodes, M as jsxAttributes, P as jsxName, S as attributeExpression, V as stringValue, W as unwrap, w as childEntries, x as analyzeModule, z as memberParts } from "./migrate-ChDGhlC-.js";
|
|
6
|
+
import { t as ModuleBuilder } from "./rewrite-BwzXoBiA.js";
|
|
7
7
|
import { extname } from "node:path";
|
|
8
8
|
import { readFileSync } from "node:fs";
|
|
9
9
|
// src/cli/commands/migrate/react-intl.ts
|
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
import { E as entryKey, T as entryCanonical, c as contentHash, k as translationErrors, p as serializeCatalog } from "./library-tp_xqzSY.js";
|
|
2
|
+
import { E as lineStartsOf, O as quoted } from "./sources-DNVbYsgs.js";
|
|
3
|
+
import { i as scanSource, r as scanConfig } from "./worker-zlhNj4y3.js";
|
|
4
|
+
import { i as structureOf, n as validateSource } from "./validate-CgzaDdOY.js";
|
|
5
|
+
import { t as VERSION } from "./version-DC-J5WvV.js";
|
|
6
|
+
import { t as diagnosticError } from "./diagnostics-LgIWyeSp.js";
|
|
7
|
+
import { i as rootRelative, o as errorMessage, r as plural } from "./output-D5tAt8q4.js";
|
|
8
|
+
import { f as readErrors, m as scan, o as loadCatalogs, s as loadProject, t as CliExit } from "./project-B5KIO0q-.js";
|
|
9
|
+
import { r as EXIT } from "./types-xR2exI3L.js";
|
|
10
|
+
import { basename, dirname, isAbsolute, join } from "node:path";
|
|
11
|
+
import { randomBytes } from "node:crypto";
|
|
12
|
+
import { readFile, rename as rename$1, rm, stat, writeFile } from "node:fs/promises";
|
|
13
|
+
// src/cli/commit.ts
|
|
14
|
+
/**
|
|
15
|
+
* Several files, all or none (`vi18n rename`): a rename touches call sites and
|
|
16
|
+
* catalogs, and a project with half of them changed is worse than one with
|
|
17
|
+
* none. Every new content is written to a temp file beside its target first;
|
|
18
|
+
* only when all of those exist are the targets replaced, and a failure
|
|
19
|
+
* puts back the ones already replaced.
|
|
20
|
+
*/
|
|
21
|
+
const nodeCommitIo = {
|
|
22
|
+
read: (path) => readFile(path),
|
|
23
|
+
write: (path, content, mode) => writeFile(path, content, { mode }),
|
|
24
|
+
rename: rename$1,
|
|
25
|
+
remove: (path) => rm(path, { force: true }),
|
|
26
|
+
mode: async (path) => (await stat(path)).mode & 4095
|
|
27
|
+
};
|
|
28
|
+
function tempName(path) {
|
|
29
|
+
return join(dirname(path), `.${basename(path)}.${process.pid}.${randomBytes(6).toString("hex")}.tmp`);
|
|
30
|
+
}
|
|
31
|
+
function changedError(path) {
|
|
32
|
+
return diagnosticError({
|
|
33
|
+
code: "catalog-write-conflict",
|
|
34
|
+
severity: "error",
|
|
35
|
+
message: `${basename(path)} changed while vi18n rename ran; nothing was written, run the command again`,
|
|
36
|
+
file: path
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/** Whether `path` still holds what the plan was computed from. */
|
|
40
|
+
async function unchanged(io, write) {
|
|
41
|
+
try {
|
|
42
|
+
return (await io.read(write.path)).equals(write.before);
|
|
43
|
+
} catch {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async function commitAll(writes, io = nodeCommitIo) {
|
|
48
|
+
const staged = [];
|
|
49
|
+
const cleanup = () => Promise.all(staged.map(({ temp }) => io.remove(temp).catch(() => void 0)));
|
|
50
|
+
try {
|
|
51
|
+
for (const write of writes) {
|
|
52
|
+
if (!await unchanged(io, write)) throw changedError(write.path);
|
|
53
|
+
const temp = tempName(write.path);
|
|
54
|
+
staged.push({
|
|
55
|
+
write,
|
|
56
|
+
temp
|
|
57
|
+
});
|
|
58
|
+
await io.write(temp, write.content, await io.mode(write.path));
|
|
59
|
+
}
|
|
60
|
+
} catch (error) {
|
|
61
|
+
await cleanup();
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
const replaced = [];
|
|
65
|
+
try {
|
|
66
|
+
for (const { write, temp } of staged) {
|
|
67
|
+
if (!await unchanged(io, write)) throw changedError(write.path);
|
|
68
|
+
await io.rename(temp, write.path);
|
|
69
|
+
replaced.push(write);
|
|
70
|
+
}
|
|
71
|
+
} catch (error) {
|
|
72
|
+
await cleanup();
|
|
73
|
+
await restore(io, replaced, error);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** Puts the replaced files' old bytes back, then rethrows what went wrong. */
|
|
77
|
+
async function restore(io, replaced, cause) {
|
|
78
|
+
const stuck = [];
|
|
79
|
+
for (const write of replaced) {
|
|
80
|
+
const temp = tempName(write.path);
|
|
81
|
+
try {
|
|
82
|
+
await io.write(temp, write.before, await io.mode(write.path));
|
|
83
|
+
await io.rename(temp, write.path);
|
|
84
|
+
} catch {
|
|
85
|
+
await io.remove(temp).catch(() => void 0);
|
|
86
|
+
stuck.push(write.path);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (stuck.length === 0) throw cause;
|
|
90
|
+
throw diagnosticError({
|
|
91
|
+
code: "catalog-write-conflict",
|
|
92
|
+
severity: "error",
|
|
93
|
+
message: `${errorMessage(cause)}; ${stuck.join(", ")} could not be put back: restore them with git checkout`,
|
|
94
|
+
file: stuck[0]
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
// src/cli/commands/rename.ts
|
|
98
|
+
/**
|
|
99
|
+
* `vi18n rename <old> <new> [--write] [--review] [--merge]` (PLAN §4.3,
|
|
100
|
+
* §5.19, INTERNALS §6 D269): one text changed at every call site and in
|
|
101
|
+
* every catalog, translations carried along. Editing the literal at each
|
|
102
|
+
* site by hand makes a new message with no translations (the old entry is
|
|
103
|
+
* deleted, or a manual id's translation cleared, by the next `extract`);
|
|
104
|
+
* rename moves the entries instead.
|
|
105
|
+
*
|
|
106
|
+
* `planRename` computes every edit from a scan and the loaded catalogs and
|
|
107
|
+
* changes nothing; `renameMessage` plans and commits all files or none. The
|
|
108
|
+
* a language-server quick fix (not built yet) would call those, not the command.
|
|
109
|
+
*/
|
|
110
|
+
function refuse(message, file) {
|
|
111
|
+
const diagnostic = {
|
|
112
|
+
code: "rename-refused",
|
|
113
|
+
severity: "error",
|
|
114
|
+
message
|
|
115
|
+
};
|
|
116
|
+
if (file !== void 0) diagnostic.file = file;
|
|
117
|
+
return new CliExit(EXIT.failure, [diagnostic]);
|
|
118
|
+
}
|
|
119
|
+
function sitesOf(scanned, canonical, skip) {
|
|
120
|
+
const sites = [];
|
|
121
|
+
for (const file of scanned.files) {
|
|
122
|
+
if (skip.includes(file.file)) continue;
|
|
123
|
+
for (const occurrence of file.occurrences) if (occurrence.canonical === canonical) sites.push({
|
|
124
|
+
file: file.file,
|
|
125
|
+
refFile: file.refFile,
|
|
126
|
+
occurrence
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return sites;
|
|
130
|
+
}
|
|
131
|
+
/** What differs between two texts' arguments and tags, in words; "" when nothing does. */
|
|
132
|
+
function structureChange(codec, from, to) {
|
|
133
|
+
const before = structureOf(codec.parse(from));
|
|
134
|
+
const after = structureOf(codec.parse(to));
|
|
135
|
+
const parts = [];
|
|
136
|
+
const names = (map, other) => [...map.keys()].filter((name) => !other.has(name));
|
|
137
|
+
const gone = [...names(before.args, after.args), ...names(before.tags, after.tags)];
|
|
138
|
+
const added = [...names(after.args, before.args), ...names(after.tags, before.tags)];
|
|
139
|
+
if (gone.length > 0) parts.push(`only the old text has ${gone.join(", ")}`);
|
|
140
|
+
if (added.length > 0) parts.push(`only the new text has ${added.join(", ")}`);
|
|
141
|
+
if (parts.length === 0 && structureErrors(codec, from, to)) parts.push("an argument is used as another kind (plural, number, date) in one of them");
|
|
142
|
+
return parts.join("; ");
|
|
143
|
+
}
|
|
144
|
+
/** Whether translations of `from` would fail against `to`, or the other way. */
|
|
145
|
+
function structureErrors(codec, from, to) {
|
|
146
|
+
const mismatch = (diagnostic) => diagnostic.code === "icu-argument-mismatch" || diagnostic.code === "icu-tag-mismatch";
|
|
147
|
+
return [translationErrors(to, from, codec, "und"), translationErrors(from, to, codec, "und")].some((errors) => errors.some(mismatch));
|
|
148
|
+
}
|
|
149
|
+
/** The new text as canonical ICU, or the refusal: invalid, or changing what a translation must keep. */
|
|
150
|
+
function newCanonical(project, request, oldCanonical) {
|
|
151
|
+
const { codec, config } = project;
|
|
152
|
+
const canonical = codec.canonicalize(request.to);
|
|
153
|
+
const invalid = validateSource(codec.parse(canonical), {
|
|
154
|
+
locale: config.sourceLocale,
|
|
155
|
+
text: request.to
|
|
156
|
+
}).filter((diagnostic) => diagnostic.severity === "error");
|
|
157
|
+
if (invalid.length > 0) throw new CliExit(EXIT.failure, invalid);
|
|
158
|
+
if (structureErrors(codec, oldCanonical, canonical)) throw refuse(`"${request.from}" and "${request.to}" differ in their arguments or tags (${structureChange(codec, oldCanonical, canonical)}), so the existing translations would break; rename only text around the arguments, or change the text in the code and run vi18n extract (the old translations are dropped and the new text is translated afresh)`);
|
|
159
|
+
return canonical;
|
|
160
|
+
}
|
|
161
|
+
const SIMPLE_ESCAPES = {
|
|
162
|
+
n: "\n",
|
|
163
|
+
r: "\r",
|
|
164
|
+
t: " ",
|
|
165
|
+
b: "\b",
|
|
166
|
+
f: "\f",
|
|
167
|
+
v: "\v",
|
|
168
|
+
"0": "\0"
|
|
169
|
+
};
|
|
170
|
+
/** One backslash escape at `at` (the character after the backslash): its value and length. */
|
|
171
|
+
function escapeAt(body, at) {
|
|
172
|
+
const char = body[at];
|
|
173
|
+
if (char === "x") {
|
|
174
|
+
const hex = body.slice(at + 1, at + 3);
|
|
175
|
+
return /^[0-9a-f]{2}$/i.test(hex) ? {
|
|
176
|
+
value: String.fromCharCode(parseInt(hex, 16)),
|
|
177
|
+
length: 3
|
|
178
|
+
} : null;
|
|
179
|
+
}
|
|
180
|
+
if (char === "u") return unicodeEscape(body, at);
|
|
181
|
+
if (char === "\r") return {
|
|
182
|
+
value: "",
|
|
183
|
+
length: body[at + 1] === "\n" ? 2 : 1
|
|
184
|
+
};
|
|
185
|
+
if (char === "\n" || char === "\u2028" || char === "\u2029") return {
|
|
186
|
+
value: "",
|
|
187
|
+
length: 1
|
|
188
|
+
};
|
|
189
|
+
if (char >= "0" && char <= "9" && !(char === "0" && !/[0-9]/.test(body[at + 1] ?? ""))) return null;
|
|
190
|
+
return {
|
|
191
|
+
value: SIMPLE_ESCAPES[char] ?? char,
|
|
192
|
+
length: 1
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function unicodeEscape(body, at) {
|
|
196
|
+
if (body[at + 1] === "{") {
|
|
197
|
+
const close = body.indexOf("}", at);
|
|
198
|
+
const hex = close === -1 ? "" : body.slice(at + 2, close);
|
|
199
|
+
if (!/^[0-9a-f]{1,6}$/i.test(hex) || parseInt(hex, 16) > 1114111) return null;
|
|
200
|
+
return {
|
|
201
|
+
value: String.fromCodePoint(parseInt(hex, 16)),
|
|
202
|
+
length: close - at + 1
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
const hex = body.slice(at + 1, at + 5);
|
|
206
|
+
return /^[0-9a-f]{4}$/i.test(hex) ? {
|
|
207
|
+
value: String.fromCharCode(parseInt(hex, 16)),
|
|
208
|
+
length: 5
|
|
209
|
+
} : null;
|
|
210
|
+
}
|
|
211
|
+
/** The value of a literal body, as JavaScript reads it; null for a form this does not write (`${}`, legacy octal). */
|
|
212
|
+
function decodeBody(body, quote) {
|
|
213
|
+
let value = "";
|
|
214
|
+
for (let i = 0; i < body.length; i++) {
|
|
215
|
+
const char = body[i];
|
|
216
|
+
if (char === "\\") {
|
|
217
|
+
const escape = escapeAt(body, i + 1);
|
|
218
|
+
if (escape === null) return null;
|
|
219
|
+
value += escape.value;
|
|
220
|
+
i += escape.length;
|
|
221
|
+
} else if (quote === "`" && char === "$" && body[i + 1] === "{") return null;
|
|
222
|
+
else if (quote === "`" && char === "\r") {
|
|
223
|
+
value += "\n";
|
|
224
|
+
if (body[i + 1] === "\n") i++;
|
|
225
|
+
} else value += char;
|
|
226
|
+
}
|
|
227
|
+
return value;
|
|
228
|
+
}
|
|
229
|
+
/** The string or template literal that starts at `start`, with its value; null when none does. */
|
|
230
|
+
function literalAt(text, start) {
|
|
231
|
+
const quote = text[start];
|
|
232
|
+
if (quote !== "'" && quote !== "\"" && quote !== "`") return null;
|
|
233
|
+
let end = start + 1;
|
|
234
|
+
while (end < text.length && text[end] !== quote) end += text[end] === "\\" ? 2 : 1;
|
|
235
|
+
if (end >= text.length) return null;
|
|
236
|
+
const body = text.slice(start + 1, end);
|
|
237
|
+
const value = decodeBody(body, quote);
|
|
238
|
+
return value === null ? null : {
|
|
239
|
+
end: end + 1,
|
|
240
|
+
quote,
|
|
241
|
+
body,
|
|
242
|
+
value
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
/** The literal as written for `value` with the old literal's quote; its whitespace style follows the old body. */
|
|
246
|
+
function encodeLiteral(value, quote, oldBody) {
|
|
247
|
+
if (quote === "\"") return JSON.stringify(value);
|
|
248
|
+
const literal = quoted(value, quote, quote === "'" || !oldBody.includes("\n"));
|
|
249
|
+
return quote === "`" && oldBody.includes("\r\n") ? literal.replace(/\n/g, "\r\n") : literal;
|
|
250
|
+
}
|
|
251
|
+
function unrewritable(site, to) {
|
|
252
|
+
const { occurrence } = site;
|
|
253
|
+
return refuse(`${site.refFile}:${occurrence.line}: the text here is not a plain string or template literal vi18n rename can rewrite (a JSX or template macro, an HTML attribute, a library's message or an escape it does not read); change it by hand to ${JSON.stringify(to)} and run vi18n rename again for the other sites; nothing was changed`, site.refFile);
|
|
254
|
+
}
|
|
255
|
+
/** One site's edit, checked against the file: the literal found there must hold exactly the scanned text. */
|
|
256
|
+
function editFor(site, text, starts, to) {
|
|
257
|
+
const { occurrence } = site;
|
|
258
|
+
const lineStart = starts[occurrence.line - 1];
|
|
259
|
+
if (lineStart === void 0 || occurrence.line < 1) throw unrewritable(site, to);
|
|
260
|
+
const start = lineStart + occurrence.column - 1;
|
|
261
|
+
const found = literalAt(text, start);
|
|
262
|
+
if (found === null || found.value !== occurrence.source) throw unrewritable(site, to);
|
|
263
|
+
return {
|
|
264
|
+
file: site.file,
|
|
265
|
+
refFile: site.refFile,
|
|
266
|
+
line: occurrence.line,
|
|
267
|
+
column: occurrence.column,
|
|
268
|
+
start,
|
|
269
|
+
end: found.end,
|
|
270
|
+
replacement: encodeLiteral(to, found.quote, found.body),
|
|
271
|
+
occurrence
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
/** Sites in the text of an opted-in package are not this project's to edit. */
|
|
275
|
+
function checkOwnFiles(sites) {
|
|
276
|
+
for (const site of sites) if (site.refFile.startsWith("..") || site.file.replace(/\\/g, "/").includes("/node_modules/") || isAbsolute(site.refFile)) throw refuse(`${site.refFile} belongs to an opted-in package (extraction.dependencies); rename the text in that package`, site.refFile);
|
|
277
|
+
}
|
|
278
|
+
/** Every site's edit, grouped by file in file order; one literal found twice (compiled `.vue`) is edited once. */
|
|
279
|
+
async function siteEdits(sites, to) {
|
|
280
|
+
const byFile = /* @__PURE__ */ new Map();
|
|
281
|
+
for (const site of sites) {
|
|
282
|
+
const list = byFile.get(site.file);
|
|
283
|
+
if (list === void 0) byFile.set(site.file, [site]);
|
|
284
|
+
else list.push(site);
|
|
285
|
+
}
|
|
286
|
+
const files = [];
|
|
287
|
+
for (const [file, list] of byFile) {
|
|
288
|
+
const before = await readFile(file);
|
|
289
|
+
const text = before.toString("utf8");
|
|
290
|
+
const starts = lineStartsOf(text);
|
|
291
|
+
const edits = /* @__PURE__ */ new Map();
|
|
292
|
+
for (const site of list) {
|
|
293
|
+
const edit = editFor(site, text, starts, to);
|
|
294
|
+
edits.set(edit.start, edit);
|
|
295
|
+
}
|
|
296
|
+
const ordered = [...edits.values()].sort((a, b) => a.start - b.start);
|
|
297
|
+
files.push({
|
|
298
|
+
file,
|
|
299
|
+
refFile: list[0].refFile,
|
|
300
|
+
before,
|
|
301
|
+
text,
|
|
302
|
+
edits: ordered
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
return files;
|
|
306
|
+
}
|
|
307
|
+
function applyEdits(text, edits) {
|
|
308
|
+
let out = text;
|
|
309
|
+
for (const edit of edits.slice().reverse()) out = out.slice(0, edit.start) + edit.replacement + out.slice(edit.end);
|
|
310
|
+
return out;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Re-scans each edited file in memory: the old text must be gone from it and
|
|
314
|
+
* the new one used exactly as often as the old one was. This is what catches
|
|
315
|
+
* a rewrite that is valid JavaScript but not what the file's framework reads
|
|
316
|
+
* (an attribute that needs entities, a macro).
|
|
317
|
+
*/
|
|
318
|
+
async function verifyEdited(project, scanned, files, canonicals) {
|
|
319
|
+
const config = scanConfig(project.config);
|
|
320
|
+
const count = (list, canonical) => list.filter((occurrence) => occurrence.canonical === canonical).length;
|
|
321
|
+
for (const file of files) {
|
|
322
|
+
const known = scanned.files.find((entry) => entry.file === file.file)?.occurrences ?? [];
|
|
323
|
+
const result = await scanSource({
|
|
324
|
+
file: file.file,
|
|
325
|
+
refFile: file.refFile,
|
|
326
|
+
text: file.after
|
|
327
|
+
}, config, project.codec);
|
|
328
|
+
const expected = count(known, canonicals.to) + count(known, canonicals.from);
|
|
329
|
+
if (result.aborted || count(result.occurrences, canonicals.from) !== 0 || count(result.occurrences, canonicals.to) !== expected) {
|
|
330
|
+
const first = file.edits[0];
|
|
331
|
+
throw refuse(`${file.refFile}:${first.line}: writing ${JSON.stringify(canonicals.to)} here would not read back as the same call (the file's own syntax needs the text written another way); change the file by hand, then run vi18n rename again for the other sites; nothing was changed`, file.refFile);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
/** The entry's new form under the new text; its translation and notes stay. */
|
|
336
|
+
function moved(entry, rename) {
|
|
337
|
+
const { layout } = rename;
|
|
338
|
+
const manual = entry.manualId !== null;
|
|
339
|
+
let label = entry.label;
|
|
340
|
+
if (layout === "id" || layout === "json" && !manual) label = rename.to.label;
|
|
341
|
+
const review = rename.review && !rename.isSource && entry.translation !== "";
|
|
342
|
+
return {
|
|
343
|
+
...entry,
|
|
344
|
+
source: rename.to.canonical,
|
|
345
|
+
label,
|
|
346
|
+
fuzzy: review ? true : entry.fuzzy
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
/** Whether `entry` is a copy of the text being renamed: a generated entry by key, a manual one by id and text. */
|
|
350
|
+
function isRenamed(entry, rename) {
|
|
351
|
+
if (entry.foreignContext !== null) return false;
|
|
352
|
+
if (entry.manualId === null) return entryKey(entry, rename.layout, rename.codec) === rename.from.label;
|
|
353
|
+
if (!rename.manualIds.has(entry.manualId)) return false;
|
|
354
|
+
if (rename.layout === "id") return entry.label === rename.from.label;
|
|
355
|
+
return entryCanonical(entry, rename.codec) === rename.from.canonical;
|
|
356
|
+
}
|
|
357
|
+
/** The catalog with the renamed text's entries moved; null when it has none. */
|
|
358
|
+
function renameInCatalog(catalog, rename) {
|
|
359
|
+
let action = "none";
|
|
360
|
+
const entries = [];
|
|
361
|
+
const renamed = catalog.entries.filter((entry) => isRenamed(entry, rename));
|
|
362
|
+
if (renamed.length === 0) return null;
|
|
363
|
+
const taken = rename.from.label === rename.to.label ? null : rename.to.label;
|
|
364
|
+
let stale = null;
|
|
365
|
+
for (const entry of catalog.entries) {
|
|
366
|
+
if (renamed.includes(entry)) continue;
|
|
367
|
+
const isNew = taken !== null && entry.manualId === null && entry.foreignContext === null && entryKey(entry, rename.layout, rename.codec) === taken;
|
|
368
|
+
if (isNew && !rename.joined) {
|
|
369
|
+
stale = entry;
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
entries.push(isNew ? withCarried(entry, renamed, rename) : entry);
|
|
373
|
+
}
|
|
374
|
+
const keepsExisting = entries.some((entry) => entry.manualId === null && entryKey(entry, rename.layout, rename.codec) === taken);
|
|
375
|
+
for (const entry of renamed) {
|
|
376
|
+
if (entry.manualId === null && keepsExisting) {
|
|
377
|
+
action = "merged";
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
entries.push(withStale(moved(entry, rename), stale));
|
|
381
|
+
if (action === "none") action = entry.manualId === null ? "moved" : "rewritten";
|
|
382
|
+
else if (action === "rewritten" && entry.manualId === null) action = "moved";
|
|
383
|
+
}
|
|
384
|
+
return {
|
|
385
|
+
catalog: {
|
|
386
|
+
...catalog,
|
|
387
|
+
entries
|
|
388
|
+
},
|
|
389
|
+
action
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
/** A translation the old text lacks is not lost to the stale entry of the new text that gives way to it. */
|
|
393
|
+
function withStale(entry, stale) {
|
|
394
|
+
if (stale === null || entry.manualId !== null || entry.translation !== "") return entry;
|
|
395
|
+
const { translation, state, fuzzy } = stale;
|
|
396
|
+
return {
|
|
397
|
+
...entry,
|
|
398
|
+
translation,
|
|
399
|
+
state,
|
|
400
|
+
fuzzy
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
/** The existing entry of the new text, which takes the old translation only where it has none of its own. */
|
|
404
|
+
function withCarried(existing, renamed, rename) {
|
|
405
|
+
if (existing.translation !== "") return existing;
|
|
406
|
+
const carry = renamed.find((entry) => entry.manualId === null && entry.translation !== "");
|
|
407
|
+
if (carry === void 0) return existing;
|
|
408
|
+
const { translation, state, fuzzy } = moved(carry, rename);
|
|
409
|
+
return {
|
|
410
|
+
...existing,
|
|
411
|
+
translation,
|
|
412
|
+
state,
|
|
413
|
+
fuzzy
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
async function catalogWrites(project, set, rename) {
|
|
417
|
+
const { config } = project;
|
|
418
|
+
const rows = [];
|
|
419
|
+
const writes = [];
|
|
420
|
+
for (const locale of config.locales) {
|
|
421
|
+
const loaded = set.get(locale);
|
|
422
|
+
if (loaded === void 0 || loaded.catalog === null) continue;
|
|
423
|
+
const isSource = locale === config.sourceLocale;
|
|
424
|
+
const file = rootRelative(config.root, loaded.path);
|
|
425
|
+
const result = renameInCatalog(loaded.catalog, {
|
|
426
|
+
...rename,
|
|
427
|
+
isSource
|
|
428
|
+
});
|
|
429
|
+
rows.push({
|
|
430
|
+
locale,
|
|
431
|
+
file,
|
|
432
|
+
action: result?.action ?? "none"
|
|
433
|
+
});
|
|
434
|
+
if (result === null || rename.from.label === rename.to.label) continue;
|
|
435
|
+
const before = await readFile(loaded.path);
|
|
436
|
+
if (contentHash(before) !== loaded.hash) throw refuse(`${file} changed while vi18n rename was planning; run the command again`, file);
|
|
437
|
+
const content = serializeCatalog(result.catalog, {
|
|
438
|
+
isSourceLocale: isSource,
|
|
439
|
+
version: VERSION
|
|
440
|
+
});
|
|
441
|
+
writes.push({
|
|
442
|
+
path: loaded.path,
|
|
443
|
+
before,
|
|
444
|
+
content
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
return {
|
|
448
|
+
rows,
|
|
449
|
+
writes
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
/** The manual ids and the generated label among the sites. */
|
|
453
|
+
function identitiesOf(sites) {
|
|
454
|
+
const manualIds = /* @__PURE__ */ new Set();
|
|
455
|
+
let generated = false;
|
|
456
|
+
for (const { occurrence } of sites) if (occurrence.manual) manualIds.add(occurrence.id);
|
|
457
|
+
else generated = true;
|
|
458
|
+
return {
|
|
459
|
+
manualIds,
|
|
460
|
+
generated
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
/** Another message the code already has under the new text's id, as a refusal unless `--merge`. */
|
|
464
|
+
function checkJoin(project, scanned, request, ids) {
|
|
465
|
+
const existing = scanned.messages.get(ids.label);
|
|
466
|
+
const kept = project.config.extraction.keep.includes(ids.label);
|
|
467
|
+
if (existing === void 0 && !kept) return false;
|
|
468
|
+
if (!request.merge) {
|
|
469
|
+
const where = existing?.locations[0];
|
|
470
|
+
const at = where === void 0 ? "" : ` (at ${where.file}:${where.line})`;
|
|
471
|
+
throw refuse(`${JSON.stringify(request.to)} is already a message${at}: renaming ${JSON.stringify(request.from)} to it joins the two, and the translations the existing one has win where both have one; run again with --merge to join them, or choose another text`);
|
|
472
|
+
}
|
|
473
|
+
return true;
|
|
474
|
+
}
|
|
475
|
+
async function planRename(project, input, request) {
|
|
476
|
+
const { codec } = project;
|
|
477
|
+
const { scanned } = input;
|
|
478
|
+
const fromCanonical = codec.canonicalize(request.from);
|
|
479
|
+
const sites = sitesOf(scanned, fromCanonical, request.skip ?? []);
|
|
480
|
+
if (sites.length === 0) throw refuse(`no t(), msg() or rich() call has the text ${JSON.stringify(request.from)}; write it as it appears in the code (vi18n which finds a message by part of its text)`);
|
|
481
|
+
checkOwnFiles(sites);
|
|
482
|
+
const toCanonical = newCanonical(project, request, fromCanonical);
|
|
483
|
+
const labels = {
|
|
484
|
+
from: codec.label(fromCanonical),
|
|
485
|
+
to: codec.label(toCanonical)
|
|
486
|
+
};
|
|
487
|
+
const identities = identitiesOf(sites);
|
|
488
|
+
const joined = identities.generated && labels.from !== labels.to ? checkJoin(project, scanned, request, { label: labels.to }) : false;
|
|
489
|
+
const edited = (await siteEdits(sites, request.to)).map((file) => ({
|
|
490
|
+
...file,
|
|
491
|
+
after: applyEdits(file.text, file.edits)
|
|
492
|
+
}));
|
|
493
|
+
await verifyEdited(project, scanned, edited, {
|
|
494
|
+
from: fromCanonical,
|
|
495
|
+
to: toCanonical
|
|
496
|
+
});
|
|
497
|
+
const catalogs = await catalogWrites(project, input.catalogs, {
|
|
498
|
+
layout: project.layout,
|
|
499
|
+
codec,
|
|
500
|
+
from: {
|
|
501
|
+
canonical: fromCanonical,
|
|
502
|
+
label: labels.from
|
|
503
|
+
},
|
|
504
|
+
to: {
|
|
505
|
+
canonical: toCanonical,
|
|
506
|
+
label: labels.to
|
|
507
|
+
},
|
|
508
|
+
manualIds: identities.manualIds,
|
|
509
|
+
joined,
|
|
510
|
+
review: request.review
|
|
511
|
+
});
|
|
512
|
+
const writes = [...edited.map((file) => ({
|
|
513
|
+
path: file.file,
|
|
514
|
+
before: file.before,
|
|
515
|
+
content: file.after
|
|
516
|
+
})), ...catalogs.writes];
|
|
517
|
+
return {
|
|
518
|
+
data: {
|
|
519
|
+
from: request.from,
|
|
520
|
+
to: request.to,
|
|
521
|
+
review: request.review,
|
|
522
|
+
merge: request.merge,
|
|
523
|
+
sites: edited.flatMap((file) => file.edits.map(({ refFile, line, column, occurrence }) => ({
|
|
524
|
+
file: refFile,
|
|
525
|
+
line,
|
|
526
|
+
column,
|
|
527
|
+
id: occurrence.id,
|
|
528
|
+
manual: occurrence.manual
|
|
529
|
+
}))),
|
|
530
|
+
catalogs: catalogs.rows
|
|
531
|
+
},
|
|
532
|
+
writes
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
/** Plans and, with `write`, commits: the one call the command and the editor's "Replace in all places" share. */
|
|
536
|
+
async function renameMessage(project, request, options, inputs) {
|
|
537
|
+
const scanned = inputs?.scanned ?? await scan(project);
|
|
538
|
+
let catalogs = inputs?.catalogs;
|
|
539
|
+
if (catalogs === void 0) {
|
|
540
|
+
const loaded = await loadCatalogs(project);
|
|
541
|
+
const unreadable = readErrors(loaded.diagnostics);
|
|
542
|
+
if (unreadable.length > 0) throw new CliExit(EXIT.failure, unreadable);
|
|
543
|
+
catalogs = loaded.set;
|
|
544
|
+
}
|
|
545
|
+
const plan = await planRename(project, {
|
|
546
|
+
scanned,
|
|
547
|
+
catalogs
|
|
548
|
+
}, request);
|
|
549
|
+
if (options.write) await commitAll(plan.writes);
|
|
550
|
+
const { root } = project.config;
|
|
551
|
+
return {
|
|
552
|
+
...plan.data,
|
|
553
|
+
write: options.write,
|
|
554
|
+
files: options.write ? plan.writes.map((write) => rootRelative(root, write.path)) : []
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
async function rename(ctx, flags) {
|
|
558
|
+
const data = await renameMessage(await loadProject(ctx), {
|
|
559
|
+
from: flags.from,
|
|
560
|
+
to: flags.to,
|
|
561
|
+
review: flags.review,
|
|
562
|
+
merge: flags.merge
|
|
563
|
+
}, { write: flags.write });
|
|
564
|
+
print(ctx, data);
|
|
565
|
+
return {
|
|
566
|
+
exitCode: EXIT.ok,
|
|
567
|
+
data
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
function print(ctx, data) {
|
|
571
|
+
const files = new Set(data.sites.map((site) => site.file));
|
|
572
|
+
ctx.out.result(`${JSON.stringify(data.from)} -> ${JSON.stringify(data.to)}: ${plural(data.sites.length, "call site")} in ${plural(files.size, "file")}`);
|
|
573
|
+
for (const site of data.sites) ctx.out.result(` ${site.file}:${site.line}:${site.column}${site.manual ? ` (${site.id})` : ""}`);
|
|
574
|
+
for (const catalog of data.catalogs) if (catalog.action !== "none") ctx.out.result(` ${catalog.file}: ${catalog.action}`);
|
|
575
|
+
ctx.out.result(data.write ? `${plural(data.files.length, "file")} written` : "nothing written; run again with --write to apply");
|
|
576
|
+
}
|
|
577
|
+
export { rename, renameMessage };
|