i18n-sharpen 0.2.1 → 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 +14 -0
- package/dist/cli.js +70 -2
- package/dist/cli.js.map +1 -1
- package/dist/index.js +70 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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
|
@@ -4,6 +4,7 @@ import * as path3 from 'path';
|
|
|
4
4
|
import { fileURLToPath } from 'url';
|
|
5
5
|
import { Command } from 'commander';
|
|
6
6
|
import pc5 from 'picocolors';
|
|
7
|
+
import { createRequire } from 'module';
|
|
7
8
|
import YAML from 'yaml';
|
|
8
9
|
import { z } from 'zod';
|
|
9
10
|
|
|
@@ -329,8 +330,19 @@ var unflattenObject = buildNestedObject;
|
|
|
329
330
|
function normalizeDisplayPath(p) {
|
|
330
331
|
return p.split(path3.sep).join("/");
|
|
331
332
|
}
|
|
333
|
+
var _require = createRequire(import.meta.url);
|
|
334
|
+
var LOCALE_EXTENSIONS = [
|
|
335
|
+
".json",
|
|
336
|
+
".yaml",
|
|
337
|
+
".yml",
|
|
338
|
+
".js",
|
|
339
|
+
".cjs",
|
|
340
|
+
".mjs",
|
|
341
|
+
".ts",
|
|
342
|
+
".tsx"
|
|
343
|
+
];
|
|
332
344
|
function findLocaleFile(localesDir, lang) {
|
|
333
|
-
const extensions =
|
|
345
|
+
const extensions = LOCALE_EXTENSIONS;
|
|
334
346
|
const found = extensions.map((ext) => path3.join(localesDir, `${lang}${ext}`)).filter((p) => fs8.existsSync(p));
|
|
335
347
|
if (found.length === 0) return null;
|
|
336
348
|
if (found.length > 1) {
|
|
@@ -342,8 +354,56 @@ function findLocaleFile(localesDir, lang) {
|
|
|
342
354
|
}
|
|
343
355
|
return found[0];
|
|
344
356
|
}
|
|
357
|
+
function loadWithJiti(filePath) {
|
|
358
|
+
let jiti;
|
|
359
|
+
try {
|
|
360
|
+
const jitiMod = _require("jiti");
|
|
361
|
+
const factory = typeof jitiMod === "function" ? jitiMod : jitiMod.default;
|
|
362
|
+
if (typeof factory === "function") {
|
|
363
|
+
jiti = factory(import.meta.url);
|
|
364
|
+
}
|
|
365
|
+
} catch {
|
|
366
|
+
}
|
|
367
|
+
if (!jiti) {
|
|
368
|
+
throw new Error(
|
|
369
|
+
`TypeScript/ESM locale file '${path3.basename(filePath)}' requires the 'jiti' package.
|
|
370
|
+
Install it as a dev-dependency: pnpm add -D jiti`
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
try {
|
|
374
|
+
const mod = jiti(filePath);
|
|
375
|
+
const result = mod !== null && typeof mod === "object" && "default" in mod ? mod.default : mod;
|
|
376
|
+
return result && typeof result === "object" && !Array.isArray(result) ? result : {};
|
|
377
|
+
} catch (err) {
|
|
378
|
+
throw new Error(
|
|
379
|
+
`Failed to parse TypeScript/ESM locale file '${path3.basename(filePath)}': ${err.message}`
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
345
383
|
function readLocaleFile(filePath) {
|
|
346
384
|
const ext = path3.extname(filePath).toLowerCase();
|
|
385
|
+
if (ext === ".js" || ext === ".cjs") {
|
|
386
|
+
try {
|
|
387
|
+
const resolved = _require.resolve(filePath);
|
|
388
|
+
Reflect.deleteProperty(_require.cache, resolved);
|
|
389
|
+
} catch {
|
|
390
|
+
}
|
|
391
|
+
try {
|
|
392
|
+
const mod = _require(filePath);
|
|
393
|
+
const result = mod !== null && typeof mod === "object" && "default" in mod ? mod.default : mod;
|
|
394
|
+
return result && typeof result === "object" && !Array.isArray(result) ? result : {};
|
|
395
|
+
} catch (err) {
|
|
396
|
+
if (ext === ".js") {
|
|
397
|
+
return loadWithJiti(filePath);
|
|
398
|
+
}
|
|
399
|
+
throw new Error(
|
|
400
|
+
`Failed to parse JS/CJS locale file '${path3.basename(filePath)}': ${err.message}`
|
|
401
|
+
);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
if (ext === ".mjs" || ext === ".ts" || ext === ".tsx") {
|
|
405
|
+
return loadWithJiti(filePath);
|
|
406
|
+
}
|
|
347
407
|
let content = fs8.readFileSync(filePath, "utf8");
|
|
348
408
|
if (content.charCodeAt(0) === 65279) {
|
|
349
409
|
content = content.slice(1);
|
|
@@ -359,8 +419,16 @@ function readLocaleFile(filePath) {
|
|
|
359
419
|
const parsedJson = JSON.parse(trimmed);
|
|
360
420
|
return parsedJson && typeof parsedJson === "object" && !Array.isArray(parsedJson) ? parsedJson : {};
|
|
361
421
|
}
|
|
422
|
+
var JS_TS_EXTENSIONS = /* @__PURE__ */ new Set([".js", ".cjs", ".mjs", ".ts", ".tsx"]);
|
|
362
423
|
function writeLocaleFile(filePath, obj) {
|
|
363
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
|
+
}
|
|
364
432
|
let content = "";
|
|
365
433
|
if (ext === ".yaml" || ext === ".yml") {
|
|
366
434
|
content = YAML.stringify(obj, { indent: 2 });
|
|
@@ -427,7 +495,7 @@ function loadNamespacedLocales(localesDir, supportedLanguages, onMissing = () =>
|
|
|
427
495
|
for (const entry of entries) {
|
|
428
496
|
if (!entry.isFile()) continue;
|
|
429
497
|
const ext = path3.extname(entry.name).toLowerCase();
|
|
430
|
-
if (
|
|
498
|
+
if (!LOCALE_EXTENSIONS.includes(ext)) continue;
|
|
431
499
|
const ns = path3.basename(entry.name, ext);
|
|
432
500
|
const filePath = path3.join(langDir, entry.name);
|
|
433
501
|
localeNamespaces[lang][ns] = filePath;
|