i18n-sharpen 0.2.2 → 0.2.3

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,20 @@ 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
+ ---
136
+
123
137
  ## Locale Layouts
124
138
 
125
139
  Two layouts are supported under `localesDir`:
package/dist/cli.js CHANGED
@@ -419,8 +419,16 @@ function readLocaleFile(filePath) {
419
419
  const parsedJson = JSON.parse(trimmed);
420
420
  return parsedJson && typeof parsedJson === "object" && !Array.isArray(parsedJson) ? parsedJson : {};
421
421
  }
422
+ var JS_TS_EXTENSIONS = /* @__PURE__ */ new Set([".js", ".cjs", ".mjs", ".ts", ".tsx"]);
422
423
  function writeLocaleFile(filePath, obj) {
423
424
  const ext = path3.extname(filePath).toLowerCase();
425
+ if (JS_TS_EXTENSIONS.has(ext)) {
426
+ throw new Error(
427
+ `Refusing to write JS/TS locale file '${path3.basename(filePath)}'.
428
+ 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.
429
+ Fix: convert this locale to .json or .yaml so extract/prune can update it safely, or add the missing keys manually.`
430
+ );
431
+ }
424
432
  let content = "";
425
433
  if (ext === ".yaml" || ext === ".yml") {
426
434
  content = YAML.stringify(obj, { indent: 2 });