i18n-sharpen 0.2.2 → 0.2.4

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
@@ -120,6 +120,29 @@ npx i18n-sharpen prune --force
120
120
 
121
121
  ---
122
122
 
123
+ ## Supported File Formats
124
+
125
+ `i18n-sharpen` supports reading translation files in multiple formats:
126
+
127
+ * **JSON (`.json`)**: Out of the box. Fully writable (`extract`/`prune`).
128
+ * **YAML (`.yaml`, `.yml`)**: Out of the box. Fully writable (`extract`/`prune`).
129
+ * **CommonJS (`.js`, `.cjs`)**: Supported for **reading only**. Loaded synchronously via native Node `require`.
130
+ * **ESM / TypeScript (`.mjs`, `.ts`, `.tsx`)**: Supported for **reading only**. Requires the `jiti` package (install via `pnpm add -D jiti`). Supports ES modules syntax (`export default { ... }`).
131
+
132
+ > [!WARNING]
133
+ > **JS/TS locale files are Read-Only.** `extract` and `prune` will throw an error if asked to write to a `.js`, `.cjs`, `.mjs`, `.ts`, or `.tsx` file. This prevents the tool from destroying imports, JSDoc, type annotations, or custom wrapping code. If you want `i18n-sharpen` to automatically manage and mutate your locale files, convert them to `.json` or `.yaml`.
134
+
135
+ ### Format Trade-offs (`.json` vs. `.ts` / `.js`)
136
+
137
+ * **Using `.ts` / `.tsx` (or JS modules):**
138
+ * *Requirement:* You must install `jiti` as a `devDependency`.
139
+ * *Capability:* Supported for **`validate` only**. Automatic `extract` and `prune` are disabled.
140
+ * **Using `.json` / `.yaml`:**
141
+ * *Capability:* Full support for **all features** (`validate`, `extract`, and `prune`).
142
+ * *Note:* Vite and TypeScript natively resolve types for `.json` imports (by enabling `"resolveJsonModule": true` in your `tsconfig.json`), guaranteeing compile-time type safety with zero runtime overhead.
143
+
144
+ ---
145
+
123
146
  ## Locale Layouts
124
147
 
125
148
  Two layouts are supported under `localesDir`:
package/dist/cli.js CHANGED
@@ -23,6 +23,9 @@ function escapeRegex(input) {
23
23
  return input.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
24
24
  }
25
25
  function buildKeyRegex(matchFunctions) {
26
+ if (matchFunctions.length === 0) {
27
+ return /(?!)/g;
28
+ }
26
29
  const functionsJoined = matchFunctions.map(escapeRegex).join("|");
27
30
  return new RegExp(
28
31
  "\\b(?:" + functionsJoined + ")\\s*\\(\\s*(['\"`])([a-zA-Z0-9_\\-.:]+)\\1",
@@ -30,6 +33,9 @@ function buildKeyRegex(matchFunctions) {
30
33
  );
31
34
  }
32
35
  function buildAttrRegex(matchAttributes) {
36
+ if (matchAttributes.length === 0) {
37
+ return /(?!)/g;
38
+ }
33
39
  const attrsJoined = matchAttributes.map(escapeRegex).join("|");
34
40
  return new RegExp(
35
41
  "(?:^|[\\s/{(>])(?:" + attrsJoined + ")\\s*=\\s*(['\"`])([a-zA-Z0-9_\\-.:]+)\\1",
@@ -37,6 +43,9 @@ function buildAttrRegex(matchAttributes) {
37
43
  );
38
44
  }
39
45
  function buildDynamicCallRegex(matchFunctions) {
46
+ if (matchFunctions.length === 0) {
47
+ return /(?!)/g;
48
+ }
40
49
  const functionsJoined = matchFunctions.map(escapeRegex).join("|");
41
50
  return new RegExp("\\b(?:" + functionsJoined + ")\\s*\\(\\s*([^)]*)\\)", "g");
42
51
  }
@@ -419,8 +428,16 @@ function readLocaleFile(filePath) {
419
428
  const parsedJson = JSON.parse(trimmed);
420
429
  return parsedJson && typeof parsedJson === "object" && !Array.isArray(parsedJson) ? parsedJson : {};
421
430
  }
431
+ var JS_TS_EXTENSIONS = /* @__PURE__ */ new Set([".js", ".cjs", ".mjs", ".ts", ".tsx"]);
422
432
  function writeLocaleFile(filePath, obj) {
423
433
  const ext = path3.extname(filePath).toLowerCase();
434
+ if (JS_TS_EXTENSIONS.has(ext)) {
435
+ throw new Error(
436
+ `Refusing to write JS/TS locale file '${path3.basename(filePath)}'.
437
+ i18n-sharpen can read .js/.cjs/.mjs/.ts/.tsx locale files but will not overwrite them, because doing so would destroy any imports, type annotations, or custom code in the source.
438
+ Fix: convert this locale to .json or .yaml so extract/prune can update it safely, or add the missing keys manually.`
439
+ );
440
+ }
424
441
  let content = "";
425
442
  if (ext === ".yaml" || ext === ".yml") {
426
443
  content = YAML.stringify(obj, { indent: 2 });