@worldware/msg-cli 0.6.0 → 0.6.2

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 CHANGED
@@ -291,7 +291,9 @@ msg import -l zh
291
291
  **Behavior:**
292
292
 
293
293
  - Recursively finds all `.xliff` and `.xlf` files under `l10n/xliff`.
294
- - Project and target locale are taken from the filename (e.g. `myApp.zh.xliff`) or from path segments (e.g. `myApp/zh/messages.xliff`).
294
+ - Project and target locale are taken from the filename (e.g. `myApp.zh.xliff`) or, when the filename has no locale, from a directory under `l10n/xliff` (e.g. `l10n/xliff/zh/myApp.xliff`). Directories above `l10n/xliff` are ignored, so a path like `/var/…` or `/tmp/…` is never read as a locale.
295
+ - `--project` and `--language` match the filename or a directory under `l10n/xliff` the same way.
296
+ - Skips (with a warning) files with no locale in the filename or under `l10n/xliff`, such as the monolingual `myApp.xliff` written by `msg export`.
295
297
  - Dynamically imports `MsgProject` from `i18n/projects` to validate target locales.
296
298
  - Skips monolingual XLIFF files (no `trgLang`).
297
299
  - Skips files whose target locale is not in the project's `targetLocales`.
@@ -394,7 +396,8 @@ The CLI does not expose a programmatic API. For library usage, see [@worldware/m
394
396
  - **Setup:** `npm install`
395
397
  - **Build:** `npm run build`
396
398
  - **Tests:** `npm run test`
397
- - **Coverage:** `npm run coverage`
399
+ - **Type-check:** `npm run typecheck` (source and tests)
400
+ - **Coverage:** `npm run coverage` (fails below 90% lines, branches, functions, or statements)
398
401
 
399
402
  Source layout:
400
403
 
@@ -4,10 +4,10 @@ import {
4
4
  } from "./chunk-Q775ZGC5.mjs";
5
5
  import {
6
6
  mf1MessageToPgsExport
7
- } from "./chunk-PQJA3H4Z.mjs";
7
+ } from "./chunk-QMSV2PP2.mjs";
8
8
  import {
9
9
  selectMessageToPgsExport
10
- } from "./chunk-HH6ZOHO4.mjs";
10
+ } from "./chunk-Z7H6FUD6.mjs";
11
11
  import {
12
12
  dynamicImportFromUrl
13
13
  } from "./chunk-KRQA4FRY.mjs";
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  parsePgsSwitch,
3
3
  selectMessageDataToPgsExport
4
- } from "./chunk-HH6ZOHO4.mjs";
4
+ } from "./chunk-Z7H6FUD6.mjs";
5
5
 
6
6
  // src/lib/pgs-mf1.ts
7
7
  import { parse } from "@messageformat/parser";
@@ -3,10 +3,10 @@ import {
3
3
  } from "./chunk-Q775ZGC5.mjs";
4
4
  import {
5
5
  pgsImportToMf1Message
6
- } from "./chunk-PQJA3H4Z.mjs";
6
+ } from "./chunk-QMSV2PP2.mjs";
7
7
  import {
8
8
  pgsImportToSelectMessage
9
- } from "./chunk-HH6ZOHO4.mjs";
9
+ } from "./chunk-Z7H6FUD6.mjs";
10
10
  import {
11
11
  dynamicImportFromUrl
12
12
  } from "./chunk-KRQA4FRY.mjs";
@@ -14,7 +14,7 @@ import {
14
14
  // src/lib/import-helpers.ts
15
15
  import { MsgResource } from "@worldware/msg";
16
16
  import { readdir, readFile, writeFile, mkdir } from "fs/promises";
17
- import { basename, extname, join } from "path";
17
+ import { basename, extname, isAbsolute, join, relative, sep } from "path";
18
18
  import { pathToFileURL } from "url";
19
19
  import { XMLParser } from "fast-xml-parser";
20
20
  import { existsSync } from "fs";
@@ -36,52 +36,33 @@ async function findXliffFilePaths(directory) {
36
36
  }
37
37
  return result;
38
38
  }
39
- function filterXliffPathsByProject(filePaths, projectName) {
39
+ function xliffDirSegments(xliffDir, filePath) {
40
+ const rel = relative(xliffDir, filePath);
41
+ if (!rel || rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel)) {
42
+ return [];
43
+ }
44
+ return rel.split(/[/\\]/).slice(0, -1);
45
+ }
46
+ function filterXliffPathsByProject(filePaths, projectName, xliffDir) {
40
47
  return filePaths.filter((p) => {
41
48
  const filename = basename(p);
42
49
  if (filename.startsWith(projectName)) return true;
43
- const pathParts = p.split(/[/\\]/);
44
- return pathParts.some((part) => part === projectName);
50
+ return xliffDirSegments(xliffDir, p).some((part) => part === projectName);
45
51
  });
46
52
  }
47
- function filterXliffPathsByLocale(filePaths, locale) {
53
+ function filterXliffPathsByLocale(filePaths, locale, xliffDir) {
48
54
  return filePaths.filter((p) => {
49
55
  const filename = basename(p);
50
56
  if (filename.includes(`.${locale}.`)) return true;
51
- const pathParts = p.split(/[/\\]/);
52
- return pathParts.some((part) => part === locale);
57
+ return xliffDirSegments(xliffDir, p).some((part) => part === locale);
53
58
  });
54
59
  }
55
60
  var LOCALE_PATTERN = /^[a-zA-Z]{2,3}(-[a-zA-Z0-9]+)*$/;
56
- function parseXliffFilename(filePath) {
61
+ function parseXliffFilename(filePath, xliffDir) {
57
62
  const filename = basename(filePath);
58
63
  const match = filename.match(XLIFF_FILENAME_PATTERN);
59
- const pathParts = filePath.split(/[/\\]/);
60
- let project;
61
- let locale;
62
- if (match) {
63
- const [, proj, loc] = match;
64
- project = proj ?? basename(filename, extname(filename));
65
- locale = loc;
66
- } else {
67
- project = basename(filename, extname(filename));
68
- }
69
- if (!locale) {
70
- for (const part of pathParts) {
71
- if (LOCALE_PATTERN.test(part) && part.length >= 2) {
72
- locale = part;
73
- break;
74
- }
75
- }
76
- }
77
- if (!project && pathParts.length >= 2) {
78
- for (const part of pathParts) {
79
- if (part && !XLIFF_EXTENSIONS.some((e) => part.endsWith(e))) {
80
- project = part;
81
- break;
82
- }
83
- }
84
- }
64
+ const project = match?.[1] ?? basename(filename, extname(filename));
65
+ const locale = match?.[2] ?? xliffDirSegments(xliffDir, filePath).find((part) => LOCALE_PATTERN.test(part));
85
66
  return { project, locale };
86
67
  }
87
68
  function isMsgProjectLike(value) {
@@ -108,7 +89,18 @@ async function importMsgProject(projectsDir, projectName) {
108
89
  }
109
90
  var xmlParser = new XMLParser({
110
91
  ignoreAttributes: false,
111
- attributeNamePrefix: "@_"
92
+ attributeNamePrefix: "@_",
93
+ // Keep segment content as raw XML: the default (non-order-preserving) mode
94
+ // groups mixed content by tag, scrambling text around inline elements.
95
+ // extractSegmentText re-parses it in document order.
96
+ stopNodes: ["*.source", "*.target"]
97
+ });
98
+ var inlineContentParser = new XMLParser({
99
+ ignoreAttributes: false,
100
+ attributeNamePrefix: "@_",
101
+ preserveOrder: true,
102
+ trimValues: false,
103
+ parseTagValue: false
112
104
  });
113
105
  function parseXliff20(xml) {
114
106
  try {
@@ -137,28 +129,39 @@ function categoryToNoteType(category) {
137
129
  function extractSegmentText(segment) {
138
130
  if (segment == null) return "";
139
131
  const seg = segment;
140
- const target = seg.target ?? seg.source;
141
- if (typeof target === "string") return target;
142
- if (target && typeof target === "object") {
143
- return extractInlineText(target);
144
- }
145
- return "";
132
+ const content = seg.target ?? seg.source;
133
+ const raw = content && typeof content === "object" ? content["#text"] : content;
134
+ if (typeof raw !== "string" || raw === "") return "";
135
+ const [root] = inlineContentParser.parse(`<c>${raw}</c>`);
136
+ return extractInlineText(root.c).trim();
137
+ }
138
+ function codePointFromHex(hex) {
139
+ const cp = /^[0-9a-f]{1,6}$/i.test(hex ?? "") ? parseInt(hex, 16) : NaN;
140
+ return cp <= 1114111 ? String.fromCodePoint(cp) : "\uFFFD";
146
141
  }
147
- function extractInlineText(node) {
142
+ function extractInlineText(nodes) {
148
143
  let text = "";
149
- if ("#text" in node && typeof node["#text"] === "string") {
150
- text += node["#text"];
151
- }
152
- const children = ["pc", "sc", "ec", "ph", "mrk", "sm", "em", "cp", "target", "source"];
153
- for (const tag of children) {
154
- const child = node[tag];
155
- if (Array.isArray(child)) {
156
- for (const c of child) {
157
- const childObj = typeof c === "object" && c !== null ? c : {};
158
- text += extractInlineText(childObj);
159
- }
160
- } else if (child && typeof child === "object") {
161
- text += extractInlineText(child);
144
+ for (const node of nodes) {
145
+ if ("#text" in node) {
146
+ text += String(node["#text"]);
147
+ continue;
148
+ }
149
+ const tag = Object.keys(node).find((k) => k !== ":@");
150
+ const attrs = node[":@"] ?? {};
151
+ switch (tag) {
152
+ case "ph":
153
+ case "sc":
154
+ case "ec":
155
+ text += attrs["@_equiv"] ?? attrs["@_disp"] ?? "";
156
+ break;
157
+ case "cp":
158
+ text += codePointFromHex(attrs["@_hex"]);
159
+ break;
160
+ case "sm":
161
+ case "em":
162
+ break;
163
+ default:
164
+ if (tag) text += extractInlineText(node[tag]);
162
165
  }
163
166
  }
164
167
  return text;
@@ -334,6 +337,7 @@ async function writeTranslationFiles(translationsDir, result) {
334
337
 
335
338
  export {
336
339
  findXliffFilePaths,
340
+ xliffDirSegments,
337
341
  filterXliffPathsByProject,
338
342
  filterXliffPathsByLocale,
339
343
  parseXliffFilename,
@@ -39,9 +39,7 @@ function getFunctionName(expr) {
39
39
  return expr.functionRef?.name;
40
40
  }
41
41
  function getOption(opts, key) {
42
- if (!opts) return void 0;
43
- if (opts instanceof Map) return opts.get(key);
44
- return opts[key];
42
+ return opts?.[key];
45
43
  }
46
44
  function getSelectOptionOrdinal(expr) {
47
45
  const sel = getOption(expr.functionRef?.options, "select");
@@ -183,9 +181,7 @@ function pgsImportToSelectMessage(switchAttr, segments) {
183
181
  }
184
182
  });
185
183
  } else if (kind === "ordinal") {
186
- const opts = /* @__PURE__ */ new Map([
187
- ["select", { type: "literal", value: "ordinal" }]
188
- ]);
184
+ const opts = { select: { type: "literal", value: "ordinal" } };
189
185
  declarations.push({
190
186
  type: "input",
191
187
  name,
@@ -171,9 +171,7 @@ function getFunctionName(expr) {
171
171
  return expr.functionRef?.name;
172
172
  }
173
173
  function getOption(opts, key) {
174
- if (!opts) return void 0;
175
- if (opts instanceof Map) return opts.get(key);
176
- return opts[key];
174
+ return opts?.[key];
177
175
  }
178
176
  function getSelectOptionOrdinal(expr) {
179
177
  const sel = getOption(expr.functionRef?.options, "select");
@@ -5,10 +5,10 @@ import {
5
5
  importMsgResourcesFromPaths,
6
6
  serializeResourceGroupsToXliff,
7
7
  writeXliffFiles
8
- } from "../chunk-WGI2EFOS.mjs";
8
+ } from "../chunk-HZHAP6XM.mjs";
9
9
  import "../chunk-Q775ZGC5.mjs";
10
- import "../chunk-PQJA3H4Z.mjs";
11
- import "../chunk-HH6ZOHO4.mjs";
10
+ import "../chunk-QMSV2PP2.mjs";
11
+ import "../chunk-Z7H6FUD6.mjs";
12
12
  import "../chunk-KRQA4FRY.mjs";
13
13
  import {
14
14
  loadPackageJsonForMsg
@@ -200,9 +200,7 @@ function pgsImportToSelectMessage(switchAttr, segments) {
200
200
  }
201
201
  });
202
202
  } else if (kind === "ordinal") {
203
- const opts = /* @__PURE__ */ new Map([
204
- ["select", { type: "literal", value: "ordinal" }]
205
- ]);
203
+ const opts = { select: { type: "literal", value: "ordinal" } };
206
204
  declarations.push({
207
205
  type: "input",
208
206
  name,
@@ -390,52 +388,33 @@ async function findXliffFilePaths(directory) {
390
388
  }
391
389
  return result;
392
390
  }
393
- function filterXliffPathsByProject(filePaths, projectName) {
391
+ function xliffDirSegments(xliffDir, filePath) {
392
+ const rel = (0, import_path2.relative)(xliffDir, filePath);
393
+ if (!rel || rel === ".." || rel.startsWith(`..${import_path2.sep}`) || (0, import_path2.isAbsolute)(rel)) {
394
+ return [];
395
+ }
396
+ return rel.split(/[/\\]/).slice(0, -1);
397
+ }
398
+ function filterXliffPathsByProject(filePaths, projectName, xliffDir) {
394
399
  return filePaths.filter((p) => {
395
400
  const filename = (0, import_path2.basename)(p);
396
401
  if (filename.startsWith(projectName)) return true;
397
- const pathParts = p.split(/[/\\]/);
398
- return pathParts.some((part) => part === projectName);
402
+ return xliffDirSegments(xliffDir, p).some((part) => part === projectName);
399
403
  });
400
404
  }
401
- function filterXliffPathsByLocale(filePaths, locale) {
405
+ function filterXliffPathsByLocale(filePaths, locale, xliffDir) {
402
406
  return filePaths.filter((p) => {
403
407
  const filename = (0, import_path2.basename)(p);
404
408
  if (filename.includes(`.${locale}.`)) return true;
405
- const pathParts = p.split(/[/\\]/);
406
- return pathParts.some((part) => part === locale);
409
+ return xliffDirSegments(xliffDir, p).some((part) => part === locale);
407
410
  });
408
411
  }
409
412
  var LOCALE_PATTERN = /^[a-zA-Z]{2,3}(-[a-zA-Z0-9]+)*$/;
410
- function parseXliffFilename(filePath) {
413
+ function parseXliffFilename(filePath, xliffDir) {
411
414
  const filename = (0, import_path2.basename)(filePath);
412
415
  const match = filename.match(XLIFF_FILENAME_PATTERN);
413
- const pathParts = filePath.split(/[/\\]/);
414
- let project;
415
- let locale;
416
- if (match) {
417
- const [, proj, loc] = match;
418
- project = proj ?? (0, import_path2.basename)(filename, (0, import_path2.extname)(filename));
419
- locale = loc;
420
- } else {
421
- project = (0, import_path2.basename)(filename, (0, import_path2.extname)(filename));
422
- }
423
- if (!locale) {
424
- for (const part of pathParts) {
425
- if (LOCALE_PATTERN.test(part) && part.length >= 2) {
426
- locale = part;
427
- break;
428
- }
429
- }
430
- }
431
- if (!project && pathParts.length >= 2) {
432
- for (const part of pathParts) {
433
- if (part && !XLIFF_EXTENSIONS.some((e) => part.endsWith(e))) {
434
- project = part;
435
- break;
436
- }
437
- }
438
- }
416
+ const project = match?.[1] ?? (0, import_path2.basename)(filename, (0, import_path2.extname)(filename));
417
+ const locale = match?.[2] ?? xliffDirSegments(xliffDir, filePath).find((part) => LOCALE_PATTERN.test(part));
439
418
  return { project, locale };
440
419
  }
441
420
  function isMsgProjectLike(value) {
@@ -462,7 +441,18 @@ async function importMsgProject(projectsDir, projectName) {
462
441
  }
463
442
  var xmlParser = new import_fast_xml_parser.XMLParser({
464
443
  ignoreAttributes: false,
465
- attributeNamePrefix: "@_"
444
+ attributeNamePrefix: "@_",
445
+ // Keep segment content as raw XML: the default (non-order-preserving) mode
446
+ // groups mixed content by tag, scrambling text around inline elements.
447
+ // extractSegmentText re-parses it in document order.
448
+ stopNodes: ["*.source", "*.target"]
449
+ });
450
+ var inlineContentParser = new import_fast_xml_parser.XMLParser({
451
+ ignoreAttributes: false,
452
+ attributeNamePrefix: "@_",
453
+ preserveOrder: true,
454
+ trimValues: false,
455
+ parseTagValue: false
466
456
  });
467
457
  function parseXliff20(xml) {
468
458
  try {
@@ -491,28 +481,39 @@ function categoryToNoteType(category) {
491
481
  function extractSegmentText(segment) {
492
482
  if (segment == null) return "";
493
483
  const seg = segment;
494
- const target = seg.target ?? seg.source;
495
- if (typeof target === "string") return target;
496
- if (target && typeof target === "object") {
497
- return extractInlineText(target);
498
- }
499
- return "";
484
+ const content = seg.target ?? seg.source;
485
+ const raw = content && typeof content === "object" ? content["#text"] : content;
486
+ if (typeof raw !== "string" || raw === "") return "";
487
+ const [root] = inlineContentParser.parse(`<c>${raw}</c>`);
488
+ return extractInlineText(root.c).trim();
489
+ }
490
+ function codePointFromHex(hex) {
491
+ const cp = /^[0-9a-f]{1,6}$/i.test(hex ?? "") ? parseInt(hex, 16) : NaN;
492
+ return cp <= 1114111 ? String.fromCodePoint(cp) : "\uFFFD";
500
493
  }
501
- function extractInlineText(node) {
494
+ function extractInlineText(nodes) {
502
495
  let text = "";
503
- if ("#text" in node && typeof node["#text"] === "string") {
504
- text += node["#text"];
505
- }
506
- const children = ["pc", "sc", "ec", "ph", "mrk", "sm", "em", "cp", "target", "source"];
507
- for (const tag of children) {
508
- const child = node[tag];
509
- if (Array.isArray(child)) {
510
- for (const c of child) {
511
- const childObj = typeof c === "object" && c !== null ? c : {};
512
- text += extractInlineText(childObj);
513
- }
514
- } else if (child && typeof child === "object") {
515
- text += extractInlineText(child);
496
+ for (const node of nodes) {
497
+ if ("#text" in node) {
498
+ text += String(node["#text"]);
499
+ continue;
500
+ }
501
+ const tag = Object.keys(node).find((k) => k !== ":@");
502
+ const attrs = node[":@"] ?? {};
503
+ switch (tag) {
504
+ case "ph":
505
+ case "sc":
506
+ case "ec":
507
+ text += attrs["@_equiv"] ?? attrs["@_disp"] ?? "";
508
+ break;
509
+ case "cp":
510
+ text += codePointFromHex(attrs["@_hex"]);
511
+ break;
512
+ case "sm":
513
+ case "em":
514
+ break;
515
+ default:
516
+ if (tag) text += extractInlineText(node[tag]);
516
517
  }
517
518
  }
518
519
  return text;
@@ -733,7 +734,7 @@ var Import = class _Import extends import_core.Command {
733
734
  }
734
735
  this.log(`Found ${filePaths.length} XLIFF file(s).`);
735
736
  if (flags.project) {
736
- filePaths = filterXliffPathsByProject(filePaths, flags.project);
737
+ filePaths = filterXliffPathsByProject(filePaths, flags.project, xliffDir);
737
738
  if (filePaths.length === 0) {
738
739
  this.log(`No XLIFF files found for project '${flags.project}'. Nothing to import.`);
739
740
  return;
@@ -741,7 +742,7 @@ var Import = class _Import extends import_core.Command {
741
742
  this.log(`Filtered to project '${flags.project}'.`);
742
743
  }
743
744
  if (flags.language) {
744
- filePaths = filterXliffPathsByLocale(filePaths, flags.language);
745
+ filePaths = filterXliffPathsByLocale(filePaths, flags.language, xliffDir);
745
746
  if (filePaths.length === 0) {
746
747
  this.log(`No XLIFF files found for language '${flags.language}'. Nothing to import.`);
747
748
  return;
@@ -751,7 +752,7 @@ var Import = class _Import extends import_core.Command {
751
752
  let wroteCount = 0;
752
753
  let errorCount = 0;
753
754
  for (const xliffPath of filePaths) {
754
- const { project: projectName, locale } = parseXliffFilename(xliffPath);
755
+ const { project: projectName, locale } = parseXliffFilename(xliffPath, xliffDir);
755
756
  if (!locale) {
756
757
  this.warn(`Skipping monolingual file (no target locale): ${xliffPath}`);
757
758
  continue;
@@ -5,10 +5,10 @@ import {
5
5
  parseXliffFilename,
6
6
  processXliffFile,
7
7
  writeTranslationFiles
8
- } from "../chunk-VJKOG7EA.mjs";
8
+ } from "../chunk-WOGVKE3R.mjs";
9
9
  import "../chunk-Q775ZGC5.mjs";
10
- import "../chunk-PQJA3H4Z.mjs";
11
- import "../chunk-HH6ZOHO4.mjs";
10
+ import "../chunk-QMSV2PP2.mjs";
11
+ import "../chunk-Z7H6FUD6.mjs";
12
12
  import "../chunk-KRQA4FRY.mjs";
13
13
  import {
14
14
  loadPackageJsonForMsg
@@ -64,7 +64,7 @@ var Import = class _Import extends Command {
64
64
  }
65
65
  this.log(`Found ${filePaths.length} XLIFF file(s).`);
66
66
  if (flags.project) {
67
- filePaths = filterXliffPathsByProject(filePaths, flags.project);
67
+ filePaths = filterXliffPathsByProject(filePaths, flags.project, xliffDir);
68
68
  if (filePaths.length === 0) {
69
69
  this.log(`No XLIFF files found for project '${flags.project}'. Nothing to import.`);
70
70
  return;
@@ -72,7 +72,7 @@ var Import = class _Import extends Command {
72
72
  this.log(`Filtered to project '${flags.project}'.`);
73
73
  }
74
74
  if (flags.language) {
75
- filePaths = filterXliffPathsByLocale(filePaths, flags.language);
75
+ filePaths = filterXliffPathsByLocale(filePaths, flags.language, xliffDir);
76
76
  if (filePaths.length === 0) {
77
77
  this.log(`No XLIFF files found for language '${flags.language}'. Nothing to import.`);
78
78
  return;
@@ -82,7 +82,7 @@ var Import = class _Import extends Command {
82
82
  let wroteCount = 0;
83
83
  let errorCount = 0;
84
84
  for (const xliffPath of filePaths) {
85
- const { project: projectName, locale } = parseXliffFilename(xliffPath);
85
+ const { project: projectName, locale } = parseXliffFilename(xliffPath, xliffDir);
86
86
  if (!locale) {
87
87
  this.warn(`Skipping monolingual file (no target locale): ${xliffPath}`);
88
88
  continue;
@@ -108,9 +108,7 @@ function getFunctionName(expr) {
108
108
  return expr.functionRef?.name;
109
109
  }
110
110
  function getOption(opts, key) {
111
- if (!opts) return void 0;
112
- if (opts instanceof Map) return opts.get(key);
113
- return opts[key];
111
+ return opts?.[key];
114
112
  }
115
113
  function getSelectOptionOrdinal(expr) {
116
114
  const sel = getOption(expr.functionRef?.options, "select");
@@ -5,10 +5,10 @@ import {
5
5
  importMsgResourcesFromPaths,
6
6
  serializeResourceGroupsToXliff,
7
7
  writeXliffFiles
8
- } from "../chunk-WGI2EFOS.mjs";
8
+ } from "../chunk-HZHAP6XM.mjs";
9
9
  import "../chunk-Q775ZGC5.mjs";
10
- import "../chunk-PQJA3H4Z.mjs";
11
- import "../chunk-HH6ZOHO4.mjs";
10
+ import "../chunk-QMSV2PP2.mjs";
11
+ import "../chunk-Z7H6FUD6.mjs";
12
12
  import "../chunk-KRQA4FRY.mjs";
13
13
  import "../chunk-F7XZRKP7.mjs";
14
14
  export {
@@ -28,7 +28,8 @@ __export(import_helpers_exports, {
28
28
  parseXliff20: () => parseXliff20,
29
29
  parseXliffFilename: () => parseXliffFilename,
30
30
  processXliffFile: () => processXliffFile,
31
- writeTranslationFiles: () => writeTranslationFiles
31
+ writeTranslationFiles: () => writeTranslationFiles,
32
+ xliffDirSegments: () => xliffDirSegments
32
33
  });
33
34
  module.exports = __toCommonJS(import_helpers_exports);
34
35
  var import_msg2 = require("@worldware/msg");
@@ -140,9 +141,7 @@ function pgsImportToSelectMessage(switchAttr, segments) {
140
141
  }
141
142
  });
142
143
  } else if (kind === "ordinal") {
143
- const opts = /* @__PURE__ */ new Map([
144
- ["select", { type: "literal", value: "ordinal" }]
145
- ]);
144
+ const opts = { select: { type: "literal", value: "ordinal" } };
146
145
  declarations.push({
147
146
  type: "input",
148
147
  name,
@@ -330,52 +329,33 @@ async function findXliffFilePaths(directory) {
330
329
  }
331
330
  return result;
332
331
  }
333
- function filterXliffPathsByProject(filePaths, projectName) {
332
+ function xliffDirSegments(xliffDir, filePath) {
333
+ const rel = (0, import_path.relative)(xliffDir, filePath);
334
+ if (!rel || rel === ".." || rel.startsWith(`..${import_path.sep}`) || (0, import_path.isAbsolute)(rel)) {
335
+ return [];
336
+ }
337
+ return rel.split(/[/\\]/).slice(0, -1);
338
+ }
339
+ function filterXliffPathsByProject(filePaths, projectName, xliffDir) {
334
340
  return filePaths.filter((p) => {
335
341
  const filename = (0, import_path.basename)(p);
336
342
  if (filename.startsWith(projectName)) return true;
337
- const pathParts = p.split(/[/\\]/);
338
- return pathParts.some((part) => part === projectName);
343
+ return xliffDirSegments(xliffDir, p).some((part) => part === projectName);
339
344
  });
340
345
  }
341
- function filterXliffPathsByLocale(filePaths, locale) {
346
+ function filterXliffPathsByLocale(filePaths, locale, xliffDir) {
342
347
  return filePaths.filter((p) => {
343
348
  const filename = (0, import_path.basename)(p);
344
349
  if (filename.includes(`.${locale}.`)) return true;
345
- const pathParts = p.split(/[/\\]/);
346
- return pathParts.some((part) => part === locale);
350
+ return xliffDirSegments(xliffDir, p).some((part) => part === locale);
347
351
  });
348
352
  }
349
353
  var LOCALE_PATTERN = /^[a-zA-Z]{2,3}(-[a-zA-Z0-9]+)*$/;
350
- function parseXliffFilename(filePath) {
354
+ function parseXliffFilename(filePath, xliffDir) {
351
355
  const filename = (0, import_path.basename)(filePath);
352
356
  const match = filename.match(XLIFF_FILENAME_PATTERN);
353
- const pathParts = filePath.split(/[/\\]/);
354
- let project;
355
- let locale;
356
- if (match) {
357
- const [, proj, loc] = match;
358
- project = proj ?? (0, import_path.basename)(filename, (0, import_path.extname)(filename));
359
- locale = loc;
360
- } else {
361
- project = (0, import_path.basename)(filename, (0, import_path.extname)(filename));
362
- }
363
- if (!locale) {
364
- for (const part of pathParts) {
365
- if (LOCALE_PATTERN.test(part) && part.length >= 2) {
366
- locale = part;
367
- break;
368
- }
369
- }
370
- }
371
- if (!project && pathParts.length >= 2) {
372
- for (const part of pathParts) {
373
- if (part && !XLIFF_EXTENSIONS.some((e) => part.endsWith(e))) {
374
- project = part;
375
- break;
376
- }
377
- }
378
- }
357
+ const project = match?.[1] ?? (0, import_path.basename)(filename, (0, import_path.extname)(filename));
358
+ const locale = match?.[2] ?? xliffDirSegments(xliffDir, filePath).find((part) => LOCALE_PATTERN.test(part));
379
359
  return { project, locale };
380
360
  }
381
361
  function isMsgProjectLike(value) {
@@ -402,7 +382,18 @@ async function importMsgProject(projectsDir, projectName) {
402
382
  }
403
383
  var xmlParser = new import_fast_xml_parser.XMLParser({
404
384
  ignoreAttributes: false,
405
- attributeNamePrefix: "@_"
385
+ attributeNamePrefix: "@_",
386
+ // Keep segment content as raw XML: the default (non-order-preserving) mode
387
+ // groups mixed content by tag, scrambling text around inline elements.
388
+ // extractSegmentText re-parses it in document order.
389
+ stopNodes: ["*.source", "*.target"]
390
+ });
391
+ var inlineContentParser = new import_fast_xml_parser.XMLParser({
392
+ ignoreAttributes: false,
393
+ attributeNamePrefix: "@_",
394
+ preserveOrder: true,
395
+ trimValues: false,
396
+ parseTagValue: false
406
397
  });
407
398
  function parseXliff20(xml) {
408
399
  try {
@@ -431,28 +422,39 @@ function categoryToNoteType(category) {
431
422
  function extractSegmentText(segment) {
432
423
  if (segment == null) return "";
433
424
  const seg = segment;
434
- const target = seg.target ?? seg.source;
435
- if (typeof target === "string") return target;
436
- if (target && typeof target === "object") {
437
- return extractInlineText(target);
438
- }
439
- return "";
425
+ const content = seg.target ?? seg.source;
426
+ const raw = content && typeof content === "object" ? content["#text"] : content;
427
+ if (typeof raw !== "string" || raw === "") return "";
428
+ const [root] = inlineContentParser.parse(`<c>${raw}</c>`);
429
+ return extractInlineText(root.c).trim();
430
+ }
431
+ function codePointFromHex(hex) {
432
+ const cp = /^[0-9a-f]{1,6}$/i.test(hex ?? "") ? parseInt(hex, 16) : NaN;
433
+ return cp <= 1114111 ? String.fromCodePoint(cp) : "\uFFFD";
440
434
  }
441
- function extractInlineText(node) {
435
+ function extractInlineText(nodes) {
442
436
  let text = "";
443
- if ("#text" in node && typeof node["#text"] === "string") {
444
- text += node["#text"];
445
- }
446
- const children = ["pc", "sc", "ec", "ph", "mrk", "sm", "em", "cp", "target", "source"];
447
- for (const tag of children) {
448
- const child = node[tag];
449
- if (Array.isArray(child)) {
450
- for (const c of child) {
451
- const childObj = typeof c === "object" && c !== null ? c : {};
452
- text += extractInlineText(childObj);
453
- }
454
- } else if (child && typeof child === "object") {
455
- text += extractInlineText(child);
437
+ for (const node of nodes) {
438
+ if ("#text" in node) {
439
+ text += String(node["#text"]);
440
+ continue;
441
+ }
442
+ const tag = Object.keys(node).find((k) => k !== ":@");
443
+ const attrs = node[":@"] ?? {};
444
+ switch (tag) {
445
+ case "ph":
446
+ case "sc":
447
+ case "ec":
448
+ text += attrs["@_equiv"] ?? attrs["@_disp"] ?? "";
449
+ break;
450
+ case "cp":
451
+ text += codePointFromHex(attrs["@_hex"]);
452
+ break;
453
+ case "sm":
454
+ case "em":
455
+ break;
456
+ default:
457
+ if (tag) text += extractInlineText(node[tag]);
456
458
  }
457
459
  }
458
460
  return text;
@@ -635,5 +637,6 @@ async function writeTranslationFiles(translationsDir, result) {
635
637
  parseXliff20,
636
638
  parseXliffFilename,
637
639
  processXliffFile,
638
- writeTranslationFiles
640
+ writeTranslationFiles,
641
+ xliffDirSegments
639
642
  });
@@ -6,29 +6,45 @@ import { MsgResource } from '@worldware/msg';
6
6
  * @returns Promise resolving to array of absolute file paths
7
7
  */
8
8
  declare function findXliffFilePaths(directory: string): Promise<string[]>;
9
+ /**
10
+ * Returns the directory names between the xliff root and an XLIFF file.
11
+ *
12
+ * Path-based project/locale detection must only look below the xliff root:
13
+ * directories above it (`/var`, `/tmp`, `src`, ...) belong to the machine,
14
+ * not to the msg layout, and many of them look like locale codes.
15
+ * @param xliffDir - Absolute path to the xliff directory (e.g. l10n/xliff)
16
+ * @param filePath - Absolute path to an XLIFF file
17
+ * @returns Directory segments under xliffDir, excluding the file name; empty
18
+ * when the file is directly in xliffDir or outside it
19
+ */
20
+ declare function xliffDirSegments(xliffDir: string, filePath: string): string[];
9
21
  /**
10
22
  * Filters XLIFF file paths by project name.
11
- * Keeps paths where filename starts with projectName or projectName is a directory in the path.
23
+ * Keeps paths where filename starts with projectName or projectName is a directory under xliffDir.
12
24
  * @param filePaths - Array of absolute XLIFF file paths
13
25
  * @param projectName - Project name to filter by
26
+ * @param xliffDir - Absolute path to the xliff directory the paths were found in
14
27
  * @returns Filtered array of paths
15
28
  */
16
- declare function filterXliffPathsByProject(filePaths: string[], projectName: string): string[];
29
+ declare function filterXliffPathsByProject(filePaths: string[], projectName: string, xliffDir: string): string[];
17
30
  /**
18
31
  * Filters XLIFF file paths by locale.
19
- * Keeps paths that have .[locale]. in the filename or locale as a directory in the path.
32
+ * Keeps paths that have .[locale]. in the filename or locale as a directory under xliffDir.
20
33
  * @param filePaths - Array of absolute XLIFF file paths
21
34
  * @param locale - Locale code to filter by
35
+ * @param xliffDir - Absolute path to the xliff directory the paths were found in
22
36
  * @returns Filtered array of paths
23
37
  */
24
- declare function filterXliffPathsByLocale(filePaths: string[], locale: string): string[];
38
+ declare function filterXliffPathsByLocale(filePaths: string[], locale: string, xliffDir: string): string[];
25
39
  /**
26
40
  * Extracts project name and locale from an XLIFF file path.
27
- * Filename patterns: project.locale.xliff, project.xliff. Path segments may also indicate project/locale.
41
+ * Filename patterns: project.locale.xliff, project.xliff. Directories under
42
+ * xliffDir may also indicate project/locale (e.g. xliff/zh/project.xliff).
28
43
  * @param filePath - Absolute path to XLIFF file
44
+ * @param xliffDir - Absolute path to the xliff directory the file was found in
29
45
  * @returns Object with project and locale (locale may be undefined for monolingual)
30
46
  */
31
- declare function parseXliffFilename(filePath: string): {
47
+ declare function parseXliffFilename(filePath: string, xliffDir: string): {
32
48
  project: string;
33
49
  locale?: string;
34
50
  };
@@ -89,4 +105,4 @@ declare function processXliffFile(xliffPath: string, projectsDir: string, projec
89
105
  */
90
106
  declare function writeTranslationFiles(translationsDir: string, result: ImportResult): Promise<void>;
91
107
 
92
- export { type ImportResult, extractResourceFromXliffFile, filterXliffPathsByLocale, filterXliffPathsByProject, findXliffFilePaths, importMsgProject, parseXliff20, parseXliffFilename, processXliffFile, writeTranslationFiles };
108
+ export { type ImportResult, extractResourceFromXliffFile, filterXliffPathsByLocale, filterXliffPathsByProject, findXliffFilePaths, importMsgProject, parseXliff20, parseXliffFilename, processXliffFile, writeTranslationFiles, xliffDirSegments };
@@ -6,29 +6,45 @@ import { MsgResource } from '@worldware/msg';
6
6
  * @returns Promise resolving to array of absolute file paths
7
7
  */
8
8
  declare function findXliffFilePaths(directory: string): Promise<string[]>;
9
+ /**
10
+ * Returns the directory names between the xliff root and an XLIFF file.
11
+ *
12
+ * Path-based project/locale detection must only look below the xliff root:
13
+ * directories above it (`/var`, `/tmp`, `src`, ...) belong to the machine,
14
+ * not to the msg layout, and many of them look like locale codes.
15
+ * @param xliffDir - Absolute path to the xliff directory (e.g. l10n/xliff)
16
+ * @param filePath - Absolute path to an XLIFF file
17
+ * @returns Directory segments under xliffDir, excluding the file name; empty
18
+ * when the file is directly in xliffDir or outside it
19
+ */
20
+ declare function xliffDirSegments(xliffDir: string, filePath: string): string[];
9
21
  /**
10
22
  * Filters XLIFF file paths by project name.
11
- * Keeps paths where filename starts with projectName or projectName is a directory in the path.
23
+ * Keeps paths where filename starts with projectName or projectName is a directory under xliffDir.
12
24
  * @param filePaths - Array of absolute XLIFF file paths
13
25
  * @param projectName - Project name to filter by
26
+ * @param xliffDir - Absolute path to the xliff directory the paths were found in
14
27
  * @returns Filtered array of paths
15
28
  */
16
- declare function filterXliffPathsByProject(filePaths: string[], projectName: string): string[];
29
+ declare function filterXliffPathsByProject(filePaths: string[], projectName: string, xliffDir: string): string[];
17
30
  /**
18
31
  * Filters XLIFF file paths by locale.
19
- * Keeps paths that have .[locale]. in the filename or locale as a directory in the path.
32
+ * Keeps paths that have .[locale]. in the filename or locale as a directory under xliffDir.
20
33
  * @param filePaths - Array of absolute XLIFF file paths
21
34
  * @param locale - Locale code to filter by
35
+ * @param xliffDir - Absolute path to the xliff directory the paths were found in
22
36
  * @returns Filtered array of paths
23
37
  */
24
- declare function filterXliffPathsByLocale(filePaths: string[], locale: string): string[];
38
+ declare function filterXliffPathsByLocale(filePaths: string[], locale: string, xliffDir: string): string[];
25
39
  /**
26
40
  * Extracts project name and locale from an XLIFF file path.
27
- * Filename patterns: project.locale.xliff, project.xliff. Path segments may also indicate project/locale.
41
+ * Filename patterns: project.locale.xliff, project.xliff. Directories under
42
+ * xliffDir may also indicate project/locale (e.g. xliff/zh/project.xliff).
28
43
  * @param filePath - Absolute path to XLIFF file
44
+ * @param xliffDir - Absolute path to the xliff directory the file was found in
29
45
  * @returns Object with project and locale (locale may be undefined for monolingual)
30
46
  */
31
- declare function parseXliffFilename(filePath: string): {
47
+ declare function parseXliffFilename(filePath: string, xliffDir: string): {
32
48
  project: string;
33
49
  locale?: string;
34
50
  };
@@ -89,4 +105,4 @@ declare function processXliffFile(xliffPath: string, projectsDir: string, projec
89
105
  */
90
106
  declare function writeTranslationFiles(translationsDir: string, result: ImportResult): Promise<void>;
91
107
 
92
- export { type ImportResult, extractResourceFromXliffFile, filterXliffPathsByLocale, filterXliffPathsByProject, findXliffFilePaths, importMsgProject, parseXliff20, parseXliffFilename, processXliffFile, writeTranslationFiles };
108
+ export { type ImportResult, extractResourceFromXliffFile, filterXliffPathsByLocale, filterXliffPathsByProject, findXliffFilePaths, importMsgProject, parseXliff20, parseXliffFilename, processXliffFile, writeTranslationFiles, xliffDirSegments };
@@ -7,11 +7,12 @@ import {
7
7
  parseXliff20,
8
8
  parseXliffFilename,
9
9
  processXliffFile,
10
- writeTranslationFiles
11
- } from "../chunk-VJKOG7EA.mjs";
10
+ writeTranslationFiles,
11
+ xliffDirSegments
12
+ } from "../chunk-WOGVKE3R.mjs";
12
13
  import "../chunk-Q775ZGC5.mjs";
13
- import "../chunk-PQJA3H4Z.mjs";
14
- import "../chunk-HH6ZOHO4.mjs";
14
+ import "../chunk-QMSV2PP2.mjs";
15
+ import "../chunk-Z7H6FUD6.mjs";
15
16
  import "../chunk-KRQA4FRY.mjs";
16
17
  import "../chunk-F7XZRKP7.mjs";
17
18
  export {
@@ -23,5 +24,6 @@ export {
23
24
  parseXliff20,
24
25
  parseXliffFilename,
25
26
  processXliffFile,
26
- writeTranslationFiles
27
+ writeTranslationFiles,
28
+ xliffDirSegments
27
29
  };
@@ -62,9 +62,7 @@ function getFunctionName(expr) {
62
62
  return expr.functionRef?.name;
63
63
  }
64
64
  function getOption(opts, key) {
65
- if (!opts) return void 0;
66
- if (opts instanceof Map) return opts.get(key);
67
- return opts[key];
65
+ return opts?.[key];
68
66
  }
69
67
  function getSelectOptionOrdinal(expr) {
70
68
  const sel = getOption(expr.functionRef?.options, "select");
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  mf1MessageToPgsExport,
3
3
  pgsImportToMf1Message
4
- } from "../chunk-PQJA3H4Z.mjs";
5
- import "../chunk-HH6ZOHO4.mjs";
4
+ } from "../chunk-QMSV2PP2.mjs";
5
+ import "../chunk-Z7H6FUD6.mjs";
6
6
  export {
7
7
  mf1MessageToPgsExport,
8
8
  pgsImportToMf1Message
@@ -63,9 +63,7 @@ function getFunctionName(expr) {
63
63
  return expr.functionRef?.name;
64
64
  }
65
65
  function getOption(opts, key) {
66
- if (!opts) return void 0;
67
- if (opts instanceof Map) return opts.get(key);
68
- return opts[key];
66
+ return opts?.[key];
69
67
  }
70
68
  function getSelectOptionOrdinal(expr) {
71
69
  const sel = getOption(expr.functionRef?.options, "select");
@@ -207,9 +205,7 @@ function pgsImportToSelectMessage(switchAttr, segments) {
207
205
  }
208
206
  });
209
207
  } else if (kind === "ordinal") {
210
- const opts = /* @__PURE__ */ new Map([
211
- ["select", { type: "literal", value: "ordinal" }]
212
- ]);
208
+ const opts = { select: { type: "literal", value: "ordinal" } };
213
209
  declarations.push({
214
210
  type: "input",
215
211
  name,
@@ -3,7 +3,7 @@
3
3
  * (`pgs:switch` / `pgs:case`, urn:oasis:names:tc:xliff:pgs:1.0).
4
4
  *
5
5
  * Ordinal vs cardinal :number uses the `select` option passed to PluralRules
6
- * (`select=ordinal` in MF2 syntax → options Map key `select`, literal `ordinal`).
6
+ * (`select=ordinal` in MF2 syntax → options key `select`, literal `ordinal`).
7
7
  */
8
8
  /** PGS selector keywords (XLIFF 2.2 Extended Part 2 §4.9). */
9
9
  type PgsSelectorKind = "plural" | "ordinal" | "gender" | "select";
@@ -3,7 +3,7 @@
3
3
  * (`pgs:switch` / `pgs:case`, urn:oasis:names:tc:xliff:pgs:1.0).
4
4
  *
5
5
  * Ordinal vs cardinal :number uses the `select` option passed to PluralRules
6
- * (`select=ordinal` in MF2 syntax → options Map key `select`, literal `ordinal`).
6
+ * (`select=ordinal` in MF2 syntax → options key `select`, literal `ordinal`).
7
7
  */
8
8
  /** PGS selector keywords (XLIFF 2.2 Extended Part 2 §4.9). */
9
9
  type PgsSelectorKind = "plural" | "ordinal" | "gender" | "select";
@@ -7,7 +7,7 @@ import {
7
7
  pgsTokenToMf2Key,
8
8
  selectMessageDataToPgsExport,
9
9
  selectMessageToPgsExport
10
- } from "../chunk-HH6ZOHO4.mjs";
10
+ } from "../chunk-Z7H6FUD6.mjs";
11
11
  export {
12
12
  classifySelectMessageForPgs,
13
13
  messagesStructurallyEqual,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@worldware/msg-cli",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "CLI for msg library",
5
5
  "main": "dist/commands/init.cjs",
6
6
  "module": "dist/commands/init.mjs",
@@ -10,6 +10,7 @@
10
10
  "build": "tsup",
11
11
  "prepack": "npm run build",
12
12
  "coverage": "vitest run --coverage",
13
+ "typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json",
13
14
  "docs": "typedoc --entryPointStrategy expand src/commands src/lib"
14
15
  },
15
16
  "repository": {