@menapo/vi18n 0.1.1 → 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.
Files changed (121) hide show
  1. package/README.md +19 -0
  2. package/dist/{apply-Bre-lY9J.js → apply-Bg-3rB2m.js} +69 -89
  3. package/dist/{captured-Bj8bO6vd.js → captured-DfLk50Ji.js} +196 -217
  4. package/dist/{check-BIKEuu6B.js → check-CunekuYs.js} +76 -85
  5. package/dist/cli.js +2 -3
  6. package/dist/{collect-Cx4_SKOe.js → collect-CYATDjdo.js} +1 -2
  7. package/dist/collisions-CDSdtLQm.js +215 -0
  8. package/dist/{compile-Cc97JaW2.js → compile-DqkofbiO.js} +376 -331
  9. package/dist/compile-hva0qjhZ.js +240 -0
  10. package/dist/concurrency-BCY_4zcr.js +1 -0
  11. package/dist/concurrency-Dy2ny50V.js +107 -0
  12. package/dist/config.d.ts +1 -5
  13. package/dist/config.js +4 -5
  14. package/dist/{types-D3QGM8XO.js → diagnostics-LgIWyeSp.js} +28 -27
  15. package/dist/{dir-zw6bGMKQ.js → dir-TGLYuOZb.js} +34 -37
  16. package/dist/{duplicates-yuRljQ6h.js → duplicates-8cudcHNj.js} +10 -27
  17. package/dist/engine-ecRD8-IE.js +556 -0
  18. package/dist/engine.d.ts +1 -5
  19. package/dist/engine.js +297 -168
  20. package/dist/exchange-DjwlrTQ9.js +41 -0
  21. package/dist/{exclude-Du-Zd_qI.js → exclude-BFETmvde.js} +2 -3
  22. package/dist/{export-C9Sy6UWv.js → export-D2tsqWIr.js} +17 -38
  23. package/dist/{extract-C3S8btx0.js → extract-BxYchVmY.js} +114 -82
  24. package/dist/{formats-9unqj_fZ.js → formats-BaryZ2N7.js} +352 -276
  25. package/dist/fs-ezCMZeDV.js +52 -0
  26. package/dist/i18next-COO6o0tX.js +1907 -0
  27. package/dist/i18next-Dr7DCQlQ.js +1 -0
  28. package/dist/{import-nANvQutw.js → import-vSC29Cn4.js} +81 -75
  29. package/dist/index.d.ts +1 -13
  30. package/dist/index.js +14 -16
  31. package/dist/{init-C2ScuW6a.js → init-Cc7A-8VX.js} +466 -501
  32. package/dist/{library-D5CWvTQs.js → library-tp_xqzSY.js} +975 -451
  33. package/dist/lingui-BnnlqGXT.js +902 -0
  34. package/dist/lint.d.ts +0 -4
  35. package/dist/lint.js +70 -82
  36. package/dist/{load-gW-oJt1Q.js → load-BSIeGJ5v.js} +275 -148
  37. package/dist/{load-CQRz4PpM.js → load-SSrAeJVB.js} +1214 -1116
  38. package/dist/{locales-DX4MOBFE.js → locales-D0U-yVRm.js} +271 -237
  39. package/dist/{lsp-RMMMc88P.js → lsp-DvC11pBZ.js} +4 -5
  40. package/dist/{match-BGr8EXB3.js → match-BLDcj610.js} +11 -4
  41. package/dist/{mcp-CHVwBoRP.js → mcp-D2zzOTBy.js} +4 -5
  42. package/dist/{merge-driver-BTX87aVm.js → merge-driver-BLI8OtAc.js} +138 -122
  43. package/dist/migrate-1r43OBfe.js +1 -0
  44. package/dist/migrate-ChDGhlC-.js +2169 -0
  45. package/dist/{openai-CTaMDcl-.js → openai-CLn4puY8.js} +128 -87
  46. package/dist/{output-DWE3SIUA.js → output-D5tAt8q4.js} +15 -9
  47. package/dist/parse.d.ts +1 -3
  48. package/dist/parse.js +7 -8
  49. package/dist/{pipeline-C9Dbotkh.js → pipeline-BJSkkRYC.js} +387 -359
  50. package/dist/{pipeline-DbX6rlo5.js → pipeline-BeCc441z.js} +1 -1
  51. package/dist/project-B5KIO0q-.js +544 -0
  52. package/dist/{pseudo-DRv30iN5.js → pseudo-B2DUpoLu.js} +126 -3
  53. package/dist/{raw-C4Y6vyGY.js → raw-YsYZMS9n.js} +52 -29
  54. package/dist/react-intl-C8JehBBL.js +634 -0
  55. package/dist/rename-ZVGq9ZG1.js +577 -0
  56. package/dist/repeated-uuI1tUGg.js +85 -0
  57. package/dist/restore-xlBIrtRt.js +199 -0
  58. package/dist/rewrite-BwzXoBiA.js +415 -0
  59. package/dist/{rich-DCUKl5HM.js → rich-ApY1Dm4S.js} +35 -28
  60. package/dist/rolldown-runtime-C1I3wEg2.js +43 -0
  61. package/dist/{rolldown-runtime-D7D4PA-g.js → rolldown-runtime-D8jQpDG7.js} +1 -2
  62. package/dist/run-CQ9pDLSa.js +674 -0
  63. package/dist/scan-worker.d.ts +2 -4
  64. package/dist/scan-worker.js +1 -1
  65. package/dist/{scoped-BMYpSthj.js → scoped-h3KE8XWW.js} +3 -4
  66. package/dist/{server-Da1Ot9IN.js → server-Br8tAuG2.js} +312 -386
  67. package/dist/{server-deBOq2vq.js → server-DJRpUQVZ.js} +1059 -333
  68. package/dist/server.d.ts +1 -17
  69. package/dist/server.js +419 -379
  70. package/dist/{sitemap-DJXzKoza.js → sitemap-CSAkm6oi.js} +21 -33
  71. package/dist/{sources-D19czDSA.js → sources-DNVbYsgs.js} +713 -373
  72. package/dist/{status-zr4jY59Y.js → status-q6M3rBrh.js} +30 -30
  73. package/dist/{stdio-FPOKQAgu.js → stdio-D9kTzyqB.js} +2 -4
  74. package/dist/{stdio-CVaJfxA7.js → stdio-FWIVU3P4.js} +1 -2
  75. package/dist/sync-BNIAwMhR.js +1352 -0
  76. package/dist/{t-DC-c9zE5.js → t-gvEXx25s.js} +76 -53
  77. package/dist/test.d.ts +5 -16
  78. package/dist/test.js +126 -56
  79. package/dist/translate-BP4iVhOR.js +336 -0
  80. package/dist/{ts-BaU5yOf0.js → ts-BX6kwig5.js} +25 -14
  81. package/dist/{types-DtvQ7UdD.js → types-3kwyaCAm.js} +1 -2
  82. package/dist/{types-DsdWBG0b.d.ts → types-B4wNX6PA.d.ts} +6 -8
  83. package/dist/{types-CgUCkFU1.d.ts → types-_4UdFhR9.d.ts} +24 -21
  84. package/dist/types-xR2exI3L.js +42 -0
  85. package/dist/{units-CQ5Q99RC.js → units-DPPfCN7j.js} +7 -16
  86. package/dist/{url-CRfCOhUI.js → url-Bpc6wLea.js} +24 -11
  87. package/dist/{url-CsBkRaJy.js → url-D2_VlEeH.js} +25 -12
  88. package/dist/{validate-BxpupdAf.js → validate-CgzaDdOY.js} +690 -377
  89. package/dist/{validate-D6DJepZv.js → validate-PGgMsasz.js} +3 -5
  90. package/dist/{version-Byqi2guX.js → version-DC-J5WvV.js} +3 -5
  91. package/dist/{vite-scan-CT_vsp4g.js → vite-scan-kM8Ta3rO.js} +87 -92
  92. package/dist/vite.d.ts +2 -6
  93. package/dist/vite.js +6500 -6032
  94. package/dist/vue-i18n-_uIXlFVh.js +806 -0
  95. package/dist/{which-BOemZ5Q8.js → which-DFWk_KwN.js} +44 -37
  96. package/dist/{worker-84U6uYB3.js → worker-zlhNj4y3.js} +454 -608
  97. package/package.json +5 -5
  98. package/dist/check-HsmmpJZg.js +0 -1
  99. package/dist/codec-DuvU2VQy.js +0 -207
  100. package/dist/collisions-CfpZU8e4.js +0 -217
  101. package/dist/compile-BaDh0cHG.js +0 -245
  102. package/dist/concurrency-C9UUWuDC.js +0 -96
  103. package/dist/concurrency-DONL7t4V.js +0 -1
  104. package/dist/engine-C2ENBRJe.js +0 -531
  105. package/dist/i18next-9HAoxwUI.js +0 -1
  106. package/dist/i18next-BEJgv_S8.js +0 -1596
  107. package/dist/lingui-zUiCl9Um.js +0 -833
  108. package/dist/messages-BfjIExyB.js +0 -166
  109. package/dist/migrate-IpLDMXmq.js +0 -1484
  110. package/dist/migrate-rCQuOGsf.js +0 -1
  111. package/dist/project-Bw9UPIdx.js +0 -1046
  112. package/dist/project-C4GpaN-n.js +0 -381
  113. package/dist/react-intl-BP0u-D5m.js +0 -519
  114. package/dist/restore-CiYv-jxS.js +0 -175
  115. package/dist/rewrite-EsIw_lsq.js +0 -803
  116. package/dist/run-BEcnBbu-.js +0 -694
  117. package/dist/runtime-DTMmliT1.js +0 -64
  118. package/dist/sync-B9V2KFwb.js +0 -658
  119. package/dist/translate-CQnJfLjO.js +0 -299
  120. package/dist/types-wSh9fCBY.js +0 -11
  121. package/dist/vue-i18n-g0iGy6CZ.js +0 -651
@@ -0,0 +1,41 @@
1
+ import { h as updateCatalogFile } from "./library-tp_xqzSY.js";
2
+ import { s as sourceIndexOf } from "./units-DPPfCN7j.js";
3
+ import { t as VERSION } from "./version-DC-J5WvV.js";
4
+ // src/cli/exchange.ts
5
+ /**
6
+ * What `export`, `import` and `restore` share: the source texts exchange
7
+ * rows are matched against, and one way to apply a change to a target
8
+ * locale's catalog, dry or written.
9
+ */
10
+ /** What exchange readers and writers match rows against: the source locale's texts. */
11
+ function sourceContext(project, set) {
12
+ const { codec, layout, config } = project;
13
+ return {
14
+ layout,
15
+ codec,
16
+ sourceIndex: sourceIndexOf(set.get(config.sourceLocale)?.catalog ?? null, layout, codec)
17
+ };
18
+ }
19
+ /**
20
+ * Applies `apply` to a target locale's catalog: to the catalog as loaded on
21
+ * a dry run, else through the compare-and-swap writer, which re-reads the
22
+ * file and writes only a changed result. Null when the catalog vanished
23
+ * meanwhile.
24
+ */
25
+ async function applyToCatalog(project, target, options, apply) {
26
+ if (options.dryRun) return apply(target.catalog);
27
+ let outcome = null;
28
+ await updateCatalogFile(target.path, target.hash, (current) => {
29
+ if (current === null) return null;
30
+ const applied = apply(current);
31
+ outcome = applied;
32
+ return applied.catalog === current ? null : applied.catalog;
33
+ }, {
34
+ locale: options.locale,
35
+ layout: project.layout,
36
+ isSourceLocale: false,
37
+ version: VERSION
38
+ });
39
+ return outcome;
40
+ }
41
+ export { sourceContext as n, applyToCatalog as t };
@@ -1,5 +1,5 @@
1
- import { o as isValidManualId } from "./validate-BxpupdAf.js";
2
- //#region src/core/config/exclude.ts
1
+ import { _ as isValidManualId } from "./validate-CgzaDdOY.js";
2
+ // src/core/config/exclude.ts
3
3
  /**
4
4
  * `ai.exclude` entries (PLAN §4.6, §5.20; INTERNALS §2.15): a configured
5
5
  * locale, a message id, or a glob over the config-relative files that use a
@@ -22,5 +22,4 @@ function excludeKind(entry, locales, idLength) {
22
22
  if (isValidManualId(entry)) return "id";
23
23
  return entry.length === idLength && BASE64URL.test(entry) ? "id" : null;
24
24
  }
25
- //#endregion
26
25
  export { excludeKind as t };
@@ -1,11 +1,12 @@
1
- import { c as sourceIndexOf, n as EXTENSIONS, o as exchangeUnits } from "./units-CQ5Q99RC.js";
2
- import { r as plural } from "./output-DWE3SIUA.js";
3
- import { t as EXIT } from "./types-wSh9fCBY.js";
4
- import { a as loadCatalogs, o as loadProject, t as CliExit, u as readErrors } from "./project-C4GpaN-n.js";
5
- import { n as serializeExport } from "./formats-9unqj_fZ.js";
1
+ import { n as serializeExport } from "./formats-BaryZ2N7.js";
2
+ import { a as exchangeUnits, n as EXTENSIONS } from "./units-DPPfCN7j.js";
3
+ import { n as sourceContext } from "./exchange-DjwlrTQ9.js";
4
+ import { r as plural } from "./output-D5tAt8q4.js";
5
+ import { f as readErrors, g as usageError, h as targetLocales, o as loadCatalogs, s as loadProject, t as CliExit } from "./project-B5KIO0q-.js";
6
+ import { r as EXIT } from "./types-xR2exI3L.js";
6
7
  import { join, resolve } from "node:path";
7
8
  import { mkdir, writeFile } from "node:fs/promises";
8
- //#region src/cli/commands/export.ts
9
+ // src/cli/commands/export.ts
9
10
  /**
10
11
  * `vi18n export --format po|json|flat|xliff2|arb|csv [--locale l]… [--out dir]
11
12
  * [--missing] [--ai]` (PLAN §4.4, §5.19, INTERNALS §2.6.9): each locale's
@@ -15,22 +16,14 @@ import { mkdir, writeFile } from "node:fs/promises";
15
16
  */
16
17
  async function exportCatalogs(ctx, flags) {
17
18
  const project = await loadProject(ctx);
18
- const { config, codec, layout } = project;
19
+ const { config } = project;
19
20
  const filtered = flags.missing || flags.ai;
20
- const locales = exportLocales(config.locales, config.sourceLocale, flags.locales, filtered);
21
- if (flags.out === null && locales.length !== 1) throw new CliExit(EXIT.usage, [{
22
- code: "config-invalid",
23
- severity: "error",
24
- message: `${plural(locales.length, "locale")} to export: pass --out <dir> for one file per locale, or one --locale for stdout`
25
- }]);
21
+ const locales = flags.locales.length === 0 && !filtered ? [...config.locales] : targetLocales(config, flags.locales, { includeSource: true });
22
+ if (flags.out === null && locales.length !== 1) throw usageError(`${plural(locales.length, "locale")} to export: pass --out <dir> for one file per locale, or one --locale for stdout`);
26
23
  const loaded = await loadCatalogs(project);
27
24
  const unreadable = readErrors(loaded.diagnostics);
28
25
  if (unreadable.length > 0) throw new CliExit(EXIT.failure, unreadable);
29
- const context = {
30
- layout,
31
- codec,
32
- sourceIndex: sourceIndexOf(loaded.set.get(config.sourceLocale)?.catalog ?? null, layout, codec)
33
- };
26
+ const context = sourceContext(project, loaded.set);
34
27
  const data = {
35
28
  format: flags.format,
36
29
  files: []
@@ -46,16 +39,16 @@ async function exportCatalogs(ctx, flags) {
46
39
  locale,
47
40
  isSourceLocale
48
41
  });
49
- const selected = filtered && !isSourceLocale ? units.filter((unit) => flags.missing && !unit.usable || flags.ai && unit.state === "ai" && unit.translation !== "") : units;
42
+ const selected = filtered && !isSourceLocale ? units.filter((u) => isSelected(u, flags)) : units;
50
43
  if (unsourced > 0) ctx.out.note(`vi18n export: ${locale}: left out ${plural(unsourced, "entry", "entries")} whose source text the catalogs do not hold; run vi18n extract`);
51
44
  const text = serializeExport(flags.format, selected, {
52
45
  locale,
53
46
  sourceLocale: config.sourceLocale,
54
47
  isSourceLocale,
55
- idScheme: codec.idScheme,
48
+ idScheme: project.codec.idScheme,
56
49
  msgid: config.catalogs.msgid,
57
50
  header: catalog?.header ?? null,
58
- layout
51
+ layout: project.layout
59
52
  });
60
53
  if (out === null) {
61
54
  data.files.push({
@@ -82,22 +75,8 @@ async function exportCatalogs(ctx, flags) {
82
75
  data
83
76
  };
84
77
  }
85
- /**
86
- * `--locale` as given (each must be configured), else every locale; with a
87
- * filter, every target locale, since the source locale has nothing missing.
88
- */
89
- function exportLocales(configured, sourceLocale, requested, filtered) {
90
- if (requested.length === 0) return filtered ? configured.filter((locale) => locale !== sourceLocale) : [...configured];
91
- const out = [];
92
- for (const locale of requested) {
93
- if (!configured.includes(locale)) throw new CliExit(EXIT.usage, [{
94
- code: "config-invalid",
95
- severity: "error",
96
- message: `--locale ${locale} is not in locales (${configured.join(", ")})`
97
- }]);
98
- if (!out.includes(locale)) out.push(locale);
99
- }
100
- return out;
78
+ /** `--missing`: no translation that would ship; `--ai`: an AI translation (both: either). */
79
+ function isSelected(unit, flags) {
80
+ return flags.missing && !unit.usable || flags.ai && unit.state === "ai" && unit.translation !== "";
101
81
  }
102
- //#endregion
103
82
  export { exportCatalogs };
@@ -1,14 +1,23 @@
1
- import { m as updateCatalogFile, n as libraryCoverage, s as catalogPath } from "./library-D5CWvTQs.js";
2
- import { n as scanProject, r as isIncluded } from "./project-Bw9UPIdx.js";
3
- import { t as VERSION } from "./version-Byqi2guX.js";
4
- import { a as writeGenFile, n as syncCatalogs, t as syncCatalog } from "./sync-B9V2KFwb.js";
5
- import { t as findCollisions } from "./collisions-CfpZU8e4.js";
6
- import { a as table, i as rootRelative, r as plural } from "./output-DWE3SIUA.js";
7
- import { t as EXIT } from "./types-wSh9fCBY.js";
8
- import { a as loadCatalogs, f as scan, i as hasErrors, o as loadProject, r as computeDrift, s as occurrencesOf, u as readErrors } from "./project-C4GpaN-n.js";
9
- import { dirname, join, resolve } from "node:path";
1
+ import { t as findCollisions } from "./collisions-CDSdtLQm.js";
2
+ import { F as isIncluded, h as updateCatalogFile, n as libraryCoverage, s as catalogPath } from "./library-tp_xqzSY.js";
3
+ import { a as writeGenFile, n as syncCatalogs, s as scanProject, t as syncCatalog } from "./sync-BNIAwMhR.js";
4
+ import { t as VERSION } from "./version-DC-J5WvV.js";
5
+ import { a as table, i as rootRelative, o as errorMessage, r as plural } from "./output-D5tAt8q4.js";
6
+ import { a as hasErrors, c as occurrencesOf, f as readErrors, g as usageError, i as computeDrift, m as scan, o as loadCatalogs, s as loadProject } from "./project-B5KIO0q-.js";
7
+ import { r as EXIT } from "./types-xR2exI3L.js";
8
+ import { dirname, resolve } from "node:path";
10
9
  import { watch } from "node:fs";
11
10
  import { stat } from "node:fs/promises";
11
+ // src/cli/commands/extract.ts
12
+ /**
13
+ * `vi18n extract [--check] [--watch] [--yes]` (INTERNALS §2.14, PLAN §5.5):
14
+ * scan the whole include set, refuse on collisions or past the safety limit,
15
+ * then write every changed catalog through the compare-and-swap writer and
16
+ * refresh `vi18n.gen.ts`. `--check` writes nothing and fails on drift;
17
+ * `--watch` only ever adds, after each debounced change.
18
+ */
19
+ /** Quiet period after the last file event before `--watch` re-scans (INTERNALS §2.14). */
20
+ const WATCH_DEBOUNCE_MS = 200;
12
21
  async function extract(ctx, flags) {
13
22
  const project = await loadProject(ctx);
14
23
  if (flags.watch) return watchMode(ctx, project);
@@ -83,7 +92,7 @@ async function extract(ctx, flags) {
83
92
  };
84
93
  }
85
94
  data.wrote = await writeCatalogs(project, loaded.set, sync, scanned, options);
86
- if (await writeGenFile(config)) data.wrote.push(rootRelative(config.root, join(dirname(config.configFile), "vi18n.gen.ts")));
95
+ if (await writeGenFile(config)) data.wrote.push(rootRelative(config.root, project.genPath));
87
96
  printStats(ctx, data);
88
97
  return {
89
98
  exitCode: hasErrors(ctx.out.diagnostics) ? EXIT.failure : EXIT.ok,
@@ -168,16 +177,103 @@ function printStats(ctx, data) {
168
177
  * files alone could not do.
169
178
  */
170
179
  async function watchMode(ctx, project) {
171
- const { config } = project;
172
- const data = {
180
+ const signal = ctx.io.signal;
181
+ if (signal === void 0) throw usageError("vi18n extract --watch needs a stop signal to end");
182
+ await writeGenFile(project.config);
183
+ const watcher = new CatalogWatcher(ctx, project);
184
+ await watcher.start(await watchRoots(project.config.project.include));
185
+ await watcher.stopOn(signal);
186
+ watcher.data.wrote = [...watcher.wrote].sort();
187
+ return {
188
+ exitCode: EXIT.ok,
189
+ data: watcher.data
190
+ };
191
+ }
192
+ /** The watchers of `--watch`, a debounced pass after changes, and one way to stop both. */
193
+ var CatalogWatcher = class {
194
+ data = {
173
195
  check: false,
174
196
  wrote: [],
175
197
  stats: {},
176
198
  blocked: []
177
199
  };
178
- const signal = ctx.io.signal;
179
- const wrote = /* @__PURE__ */ new Set();
180
- const pass = async () => {
200
+ wrote = /* @__PURE__ */ new Set();
201
+ ctx;
202
+ project;
203
+ watchers = [];
204
+ timer = null;
205
+ running = Promise.resolve();
206
+ pending = false;
207
+ stopped = false;
208
+ /** Changes wait for the first pass: it may have read the file before the edit. */
209
+ first = true;
210
+ constructor(ctx, project) {
211
+ this.ctx = ctx;
212
+ this.project = project;
213
+ }
214
+ /**
215
+ * Watching starts before the first pass, so an edit made while it runs is
216
+ * seen; so is one made just after, which on macOS could otherwise land
217
+ * before FSEvents, started a moment after `watch()` returns, was listening.
218
+ */
219
+ async start(roots) {
220
+ for (const root of roots) this.watchRoot(root);
221
+ try {
222
+ await this.pass();
223
+ } catch (error) {
224
+ this.stop();
225
+ throw error;
226
+ }
227
+ this.first = false;
228
+ if (this.pending) this.schedule();
229
+ }
230
+ /** Resolves once the signal aborted and the running pass settled. */
231
+ stopOn(signal) {
232
+ return new Promise((done) => {
233
+ const stop = () => {
234
+ if (this.stopped) return;
235
+ this.stop();
236
+ this.running.then(done, done);
237
+ };
238
+ if (signal.aborted) stop();
239
+ else signal.addEventListener("abort", stop, { once: true });
240
+ });
241
+ }
242
+ stop() {
243
+ this.stopped = true;
244
+ if (this.timer !== null) clearTimeout(this.timer);
245
+ for (const watcher of this.watchers) watcher.close();
246
+ }
247
+ watchRoot(root) {
248
+ const { config } = this.project;
249
+ const onEvent = (_event, name) => {
250
+ if (this.stopped) return;
251
+ const file = name === null ? null : resolve(root.dir, name.toString());
252
+ if (file !== null && root.file !== null && file !== root.file) return;
253
+ if (file !== null && !isIncluded(file, config)) return;
254
+ this.pending = true;
255
+ if (!this.first) this.schedule();
256
+ };
257
+ try {
258
+ this.watchers.push(root.file === null ? watch(root.dir, { recursive: true }, onEvent) : watch(root.dir, {}, onEvent));
259
+ } catch (error) {
260
+ this.ctx.out.note(`vi18n: cannot watch ${root.dir}: ${errorMessage(error)}`);
261
+ }
262
+ }
263
+ schedule() {
264
+ if (this.timer !== null) clearTimeout(this.timer);
265
+ this.timer = setTimeout(() => {
266
+ this.timer = null;
267
+ if (!this.pending) return;
268
+ this.pending = false;
269
+ this.running = this.running.then(() => this.pass()).catch((error) => {
270
+ this.ctx.out.note(`vi18n: ${errorMessage(error)}`);
271
+ });
272
+ }, WATCH_DEBOUNCE_MS);
273
+ }
274
+ async pass() {
275
+ const { ctx, project } = this;
276
+ const { config } = project;
181
277
  const scanned = await scanProject(config, project.codec, { cache: true });
182
278
  ctx.out.report(scanned.diagnostics);
183
279
  if (scanned.aborted) return;
@@ -201,7 +297,7 @@ async function watchMode(ctx, project) {
201
297
  if (hasErrors(collided)) return;
202
298
  const files = await writeCatalogs(project, loaded.set, sync, scanned, options);
203
299
  for (const [locale, stats] of Object.entries(sync.stats)) {
204
- const total = data.stats[locale] ??= {
300
+ const total = this.data.stats[locale] ??= {
205
301
  added: 0,
206
302
  deleted: 0,
207
303
  cleared: 0,
@@ -212,73 +308,10 @@ async function watchMode(ctx, project) {
212
308
  total.added += stats.added;
213
309
  total.updated += stats.updated;
214
310
  }
215
- for (const file of files) wrote.add(file);
311
+ for (const file of files) this.wrote.add(file);
216
312
  if (files.length > 0) ctx.out.note(`vi18n: updated ${files.join(", ")}`);
217
- };
218
- await writeGenFile(config);
219
- const roots = await watchRoots(config.project.include);
220
- const watchers = [];
221
- let timer = null;
222
- let running = Promise.resolve();
223
- let pending = false;
224
- let stopped = false;
225
- let first = true;
226
- const schedule = () => {
227
- if (timer !== null) clearTimeout(timer);
228
- timer = setTimeout(() => {
229
- timer = null;
230
- if (!pending) return;
231
- pending = false;
232
- running = running.then(pass).catch((error) => {
233
- ctx.out.note(`vi18n: ${error instanceof Error ? error.message : String(error)}`);
234
- });
235
- }, 200);
236
- };
237
- for (const root of roots) {
238
- const onEvent = (_event, name) => {
239
- if (stopped) return;
240
- const file = name === null ? null : resolve(root.dir, name.toString());
241
- if (file !== null && root.file !== null && file !== root.file) return;
242
- if (file !== null && !isIncluded(file, config)) return;
243
- pending = true;
244
- if (!first) schedule();
245
- };
246
- try {
247
- watchers.push(root.file === null ? watch(root.dir, { recursive: true }, onEvent) : watch(root.dir, {}, onEvent));
248
- } catch (error) {
249
- ctx.out.note(`vi18n: cannot watch ${root.dir}: ${error instanceof Error ? error.message : String(error)}`);
250
- }
251
313
  }
252
- try {
253
- await pass();
254
- } catch (error) {
255
- stopped = true;
256
- for (const watcher of watchers) watcher.close();
257
- throw error;
258
- }
259
- first = false;
260
- if (pending) schedule();
261
- await new Promise((done) => {
262
- const stop = () => {
263
- if (stopped) return;
264
- stopped = true;
265
- if (timer !== null) clearTimeout(timer);
266
- for (const watcher of watchers) watcher.close();
267
- running.then(done, done);
268
- };
269
- if (signal === void 0) return;
270
- if (signal.aborted) stop();
271
- else signal.addEventListener("abort", stop, { once: true });
272
- });
273
- return finishWatch(data, wrote);
274
- }
275
- function finishWatch(data, wrote) {
276
- data.wrote = [...wrote].sort();
277
- return {
278
- exitCode: EXIT.ok,
279
- data
280
- };
281
- }
314
+ };
282
315
  /** Directories to watch: each include directory recursively, a file's parent for a file entry. */
283
316
  async function watchRoots(include) {
284
317
  const roots = [];
@@ -294,5 +327,4 @@ async function watchRoots(include) {
294
327
  } catch {}
295
328
  return roots;
296
329
  }
297
- //#endregion
298
330
  export { extract };