i18n-sharpen 0.2.0 → 0.2.2

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
@@ -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 = [".json", ".yaml", ".yml"];
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);
@@ -427,7 +487,7 @@ function loadNamespacedLocales(localesDir, supportedLanguages, onMissing = () =>
427
487
  for (const entry of entries) {
428
488
  if (!entry.isFile()) continue;
429
489
  const ext = path3.extname(entry.name).toLowerCase();
430
- if (ext !== ".json" && ext !== ".yaml" && ext !== ".yml") continue;
490
+ if (!LOCALE_EXTENSIONS.includes(ext)) continue;
431
491
  const ns = path3.basename(entry.name, ext);
432
492
  const filePath = path3.join(langDir, entry.name);
433
493
  localeNamespaces[lang][ns] = filePath;