@4399ywkf/core 5.4.0 → 5.4.1

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/index.js CHANGED
@@ -631,7 +631,7 @@ function scaffoldSourceFiles(cwd, sourceDir, namespaces, logger) {
631
631
  }
632
632
  const indexPath = join3(dir, "index.ts");
633
633
  if (!existsSync3(indexPath)) {
634
- const imports = namespaces.map((ns) => `import ${ns} from "./${ns}";`).join("\n");
634
+ const imports = namespaces.map((ns) => `import ${ns} from './${ns}';`).join("\n");
635
635
  const entries = namespaces.join(",\n ");
636
636
  const content = `${imports}
637
637
 
@@ -648,19 +648,19 @@ export default resources;
648
648
  function buildDefaultNsFile(ns) {
649
649
  if (ns === "common") {
650
650
  return `export default {
651
- welcome: "\u6B22\u8FCE\u4F7F\u7528",
652
- loading: "\u52A0\u8F7D\u4E2D...",
653
- confirm: "\u786E\u5B9A",
654
- cancel: "\u53D6\u6D88",
655
- save: "\u4FDD\u5B58",
656
- delete: "\u5220\u9664",
657
- edit: "\u7F16\u8F91",
658
- back: "\u8FD4\u56DE",
651
+ welcome: '\u6B22\u8FCE\u4F7F\u7528',
652
+ loading: '\u52A0\u8F7D\u4E2D...',
653
+ confirm: '\u786E\u5B9A',
654
+ cancel: '\u53D6\u6D88',
655
+ save: '\u4FDD\u5B58',
656
+ delete: '\u5220\u9664',
657
+ edit: '\u7F16\u8F91',
658
+ back: '\u8FD4\u56DE',
659
659
  } as const;
660
660
  `;
661
661
  }
662
662
  return `export default {
663
- title: "${ns}",
663
+ title: '${ns}',
664
664
  } as const;
665
665
  `;
666
666
  }
@@ -668,11 +668,11 @@ function scaffoldResourcesFile(cwd, sourceDir, _namespaces, locales, defaultLoca
668
668
  const parentDir = join3(cwd, sourceDir, "..");
669
669
  const filePath = join3(parentDir, "resources.ts");
670
670
  if (existsSync3(filePath)) return;
671
- const content = `import resources from "./default";
671
+ const content = `import type resources from './default';
672
672
 
673
- export const DEFAULT_LOCALE = "${defaultLocale}";
673
+ export const DEFAULT_LOCALE = '${defaultLocale}';
674
674
 
675
- export const SUPPORTED_LOCALES = ${JSON.stringify(locales)} as const;
675
+ export const SUPPORTED_LOCALES = [${locales.map((l) => `'${l}'`).join(", ")}] as const;
676
676
 
677
677
  export type SupportedLocale = (typeof SUPPORTED_LOCALES)[number];
678
678
 
@@ -684,7 +684,7 @@ export const normalizeLocale = (locale?: string): SupportedLocale => {
684
684
  if (!locale) return DEFAULT_LOCALE as SupportedLocale;
685
685
 
686
686
  for (const l of SUPPORTED_LOCALES) {
687
- if (l.startsWith(locale) || locale.startsWith(l.split("-")[0])) {
687
+ if (l.startsWith(locale) || locale.startsWith(l.split('-')[0])) {
688
688
  return l;
689
689
  }
690
690
  }
@@ -692,11 +692,9 @@ export const normalizeLocale = (locale?: string): SupportedLocale => {
692
692
  return DEFAULT_LOCALE as SupportedLocale;
693
693
  };
694
694
 
695
- export const localeOptions = ${JSON.stringify(
696
- locales.map((l) => ({ value: l, label: getLocaleLabel(l) })),
697
- null,
698
- 2
699
- )} as const;
695
+ export const localeOptions = [
696
+ ${locales.map((l) => ` { value: '${l}', label: '${getLocaleLabel(l)}' },`).join("\n")}
697
+ ] as const;
700
698
  `;
701
699
  writeFileSync2(filePath, content, "utf-8");
702
700
  logger.info(`\u5DF2\u751F\u6210\u7C7B\u578B\u6587\u4EF6: ${sourceDir}/../resources.ts`);
@@ -721,29 +719,29 @@ function getLocaleLabel(locale) {
721
719
  }
722
720
  function scaffoldConvertScript(cwd, sourceDir, localesDir, defaultLocale, logger) {
723
721
  const scriptsDir = join3(cwd, "scripts");
724
- const scriptPath = join3(scriptsDir, "i18n-gen.js");
725
- if (existsSync3(scriptPath)) return;
722
+ const scriptPath = join3(scriptsDir, "i18n-gen.cjs");
723
+ if (existsSync3(scriptPath) || existsSync3(join3(scriptsDir, "i18n-gen.js"))) return;
726
724
  if (!existsSync3(scriptsDir)) {
727
725
  mkdirSync(scriptsDir, { recursive: true });
728
726
  }
729
- const content = `const fs = require("fs");
730
- const path = require("path");
727
+ const content = `const fs = require('node:fs');
728
+ const path = require('node:path');
731
729
 
732
- const sourceDir = path.join(__dirname, "../${sourceDir}");
733
- const targetDir = path.join(__dirname, "../${localesDir}/${defaultLocale}");
730
+ const sourceDir = path.join(__dirname, '../${sourceDir}');
731
+ const targetDir = path.join(__dirname, '../${localesDir}/${defaultLocale}');
734
732
 
735
733
  const files = fs
736
734
  .readdirSync(sourceDir)
737
- .filter((file) => file.endsWith(".ts") && file !== "index.ts")
738
- .map((file) => path.basename(file, ".ts"));
735
+ .filter((file) => file.endsWith('.ts') && file !== 'index.ts')
736
+ .map((file) => path.basename(file, '.ts'));
739
737
 
740
- require("ts-node/register");
738
+ require('ts-node/register');
741
739
 
742
740
  if (!fs.existsSync(targetDir)) {
743
741
  fs.mkdirSync(targetDir, { recursive: true });
744
742
  }
745
743
 
746
- console.log("\u5F00\u59CB\u8F6C\u6362 TypeScript \u6587\u4EF6\u5230 JSON...\\n");
744
+ console.log('\u5F00\u59CB\u8F6C\u6362 TypeScript \u6587\u4EF6\u5230 JSON...\\n');
747
745
 
748
746
  files.forEach((fileName) => {
749
747
  try {
@@ -752,28 +750,28 @@ files.forEach((fileName) => {
752
750
  const moduleContent = require(modulePath).default;
753
751
  const jsonContent = JSON.stringify(moduleContent, null, 2);
754
752
  const targetPath = path.join(targetDir, \`\${fileName}.json\`);
755
- fs.writeFileSync(targetPath, jsonContent, "utf8");
753
+ fs.writeFileSync(targetPath, jsonContent, 'utf8');
756
754
  console.log(\` \${fileName}.ts \u2192 \${fileName}.json\`);
757
755
  } catch (error) {
758
756
  console.error(\` \u8F6C\u6362 \${fileName}.ts \u5931\u8D25:\`, error.message);
759
757
  }
760
758
  });
761
759
 
762
- console.log("\\n\u8F6C\u6362\u5B8C\u6210\uFF01");
760
+ console.log('\\n\u8F6C\u6362\u5B8C\u6210\uFF01');
763
761
  `;
764
762
  writeFileSync2(scriptPath, content, "utf-8");
765
- logger.info("\u5DF2\u751F\u6210\u8F6C\u6362\u811A\u672C: scripts/i18n-gen.js");
763
+ logger.info("\u5DF2\u751F\u6210\u8F6C\u6362\u811A\u672C: scripts/i18n-gen.cjs");
766
764
  }
767
765
  function scaffoldI18nRc(cwd, localesDir, defaultLocale, locales, logger) {
768
766
  const filePath = join3(cwd, ".i18nrc.js");
769
767
  if (existsSync3(filePath)) return;
770
- const outputLocales = locales.filter((l) => l !== defaultLocale).map((l) => `"${l}"`).join(", ");
771
- const content = `const { defineConfig } = require("@4399ywkf/i18n");
768
+ const outputLocales = locales.filter((l) => l !== defaultLocale).map((l) => `'${l}'`).join(", ");
769
+ const content = `const { defineConfig } = require('@4399ywkf/i18n');
772
770
 
773
771
  module.exports = defineConfig({
774
- entry: "${localesDir}/${defaultLocale}",
775
- entryLocale: "${defaultLocale}",
776
- output: "${localesDir}",
772
+ entry: '${localesDir}/${defaultLocale}',
773
+ entryLocale: '${defaultLocale}',
774
+ output: '${localesDir}',
777
775
  outputLocales: [${outputLocales}],
778
776
  });
779
777
  `;
@@ -792,15 +790,16 @@ function scaffoldLocaleJsonDirs(cwd, localesDir, locales, defaultLocale, namespa
792
790
  const filePath = join3(localeDir, `${ns}.json`);
793
791
  if (!existsSync3(filePath)) {
794
792
  const placeholder = ns === "common" ? JSON.stringify(
793
+ // 键按字母序排列,与 @4399ywkf/lint 的 Prettier(prettier-plugin-sort-json)一致
795
794
  {
796
- welcome: "Welcome",
797
- loading: "Loading...",
798
- confirm: "OK",
795
+ back: "Back",
799
796
  cancel: "Cancel",
800
- save: "Save",
797
+ confirm: "OK",
801
798
  delete: "Delete",
802
799
  edit: "Edit",
803
- back: "Back"
800
+ loading: "Loading...",
801
+ save: "Save",
802
+ welcome: "Welcome"
804
803
  },
805
804
  null,
806
805
  2