@verbaly/compiler 0.56.1 → 0.58.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/dist/cli.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import { basename, dirname, join, relative, resolve } from "node:path";
3
3
  import { parseArgs } from "node:util";
4
4
  import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, watch, writeFileSync } from "node:fs";
5
- import { CATALOG_SCRIPT, LOCALE_STORAGE_KEY, RICH_TAGS, VOID_TAGS, alternateLinks, createVerbaly, flatten, localeDirection, normalizeLink, parse, parseTags, safeAttribute } from "verbaly";
5
+ import { CATALOG_SCRIPT, LOCALE_STORAGE_KEY, RICH_TAGS, VOID_TAGS, alternateLinks, createVerbaly, flatten, localeDirection, normalizeLink, parse, parseIcu, parseTags, safeAttribute } from "verbaly";
6
6
  import { pathToFileURL } from "node:url";
7
7
  import { glob } from "tinyglobby";
8
8
  import { parse as parse$1 } from "@babel/parser";
@@ -79,8 +79,31 @@ function sortTree(tree) {
79
79
  }
80
80
  return sorted;
81
81
  }
82
- function serializeCatalog(catalog, nested = false) {
83
- return JSON.stringify(sortTree(nested ? nest(catalog) : catalog), null, 2) + "\n";
82
+ function isSorted(keys) {
83
+ for (let i = 1; i < keys.length; i++) if (keys[i - 1] > keys[i]) return false;
84
+ return true;
85
+ }
86
+ function orderLike(tree, previous) {
87
+ const had = Object.keys(previous);
88
+ if (isSorted(had)) return sortTree(tree);
89
+ const out = {};
90
+ const take = (key) => {
91
+ const value = tree[key];
92
+ if (typeof value === "string") {
93
+ out[key] = value;
94
+ return;
95
+ }
96
+ if (value === void 0) return;
97
+ const before = previous[key];
98
+ out[key] = typeof before === "object" && before !== null ? orderLike(value, before) : sortTree(value);
99
+ };
100
+ for (const key of had) if (key in tree) take(key);
101
+ for (const key of Object.keys(tree).sort()) if (!(key in previous)) take(key);
102
+ return out;
103
+ }
104
+ function serializeCatalog(catalog, nested = false, previous) {
105
+ const tree = nested ? nest(catalog) : catalog;
106
+ return JSON.stringify(previous ? orderLike(tree, previous) : sortTree(tree), null, 2) + "\n";
84
107
  }
85
108
  function writeCatalog(cfg, locale, catalog) {
86
109
  const path = catalogPath(cfg, locale);
@@ -90,16 +113,14 @@ function writeCatalog(cfg, locale, catalog) {
90
113
  } catch {
91
114
  mkdirSync(cfg.dir, { recursive: true });
92
115
  }
93
- const serialized = serializeCatalog(catalog, wantsNesting(cfg, locale, existing));
116
+ const previous = existing === void 0 ? void 0 : parseTree(existing);
117
+ const serialized = serializeCatalog(catalog, wantsNesting(cfg, locale, existing, previous), previous);
94
118
  if (existing === serialized) return serialized;
95
119
  writeFileSync(path, serialized);
96
120
  return serialized;
97
121
  }
98
- function wantsNesting(cfg, locale, existing) {
99
- if (existing !== void 0) {
100
- const tree = parseTree(existing);
101
- return tree !== void 0 && isNested(tree);
102
- }
122
+ function wantsNesting(cfg, locale, existing, previous) {
123
+ if (existing !== void 0) return previous !== void 0 && isNested(previous);
103
124
  if (locale === cfg.sourceLocale) return false;
104
125
  return isNested(readTree(cfg, cfg.sourceLocale));
105
126
  }
@@ -429,6 +450,7 @@ async function init(options = {}) {
429
450
  }
430
451
  //#endregion
431
452
  //#region src/validate.ts
453
+ const read = (message) => parse(message, parseIcu);
432
454
  const PLURAL_CATEGORIES = /* @__PURE__ */ new Set([
433
455
  "zero",
434
456
  "one",
@@ -439,7 +461,7 @@ const PLURAL_CATEGORIES = /* @__PURE__ */ new Set([
439
461
  const EXACT = /^=\d+$/;
440
462
  function shapes(message) {
441
463
  const out = /* @__PURE__ */ new Map();
442
- walk$1(parse(message), out);
464
+ walk$1(read(message), out);
443
465
  return out;
444
466
  }
445
467
  function walk$1(nodes, out) {
@@ -456,7 +478,7 @@ function walk$1(nodes, out) {
456
478
  }
457
479
  function paramNames(message) {
458
480
  const names = /* @__PURE__ */ new Set();
459
- collect(parse(message), names);
481
+ collect(read(message), names);
460
482
  return [...names].sort();
461
483
  }
462
484
  function collect(nodes, out) {