init.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import fs from "fs";
  2. import generate from "./generate";
  3. import { Holiday } from '../src/holidays/arrangement';
  4. const data = generate();
  5. // 反向映射 Holiday 枚举
  6. const holidayMap: Record<string, keyof typeof Holiday> = Object.fromEntries(
  7. Object.entries(Holiday).map(([key, value]) => [value, key as keyof typeof Holiday])
  8. );
  9. const mapToEnum = (obj: Record<string, string>): Record<string, any> => {
  10. const result: Record<string, any> = {};
  11. for (const [key, value] of Object.entries(obj)) {
  12. if (holidayMap[value]) {
  13. result[key] = `Holiday.${holidayMap[value]}`;
  14. } else {
  15. result[key] = `${value}`;
  16. }
  17. }
  18. return result;
  19. };
  20. // 转换数据
  21. const holidays = mapToEnum(data.holidays);
  22. const workdays = mapToEnum(data.workdays);
  23. const inLieuDays = mapToEnum(data.inLieuDays);
  24. const formatObjectString = (obj: Record<string, any>): string => {
  25. const entries = Object.entries(obj).map(([key, value]) => ` "${key}": ${value}`);
  26. return `{\n${entries.join(",\n")}\n}`;
  27. };
  28. const tsContent = `import { Holiday } from "./arrangement";
  29. export const holidays: Record<string, Holiday> = ${formatObjectString(holidays)};
  30. export const workdays: Record<string, Holiday> = ${formatObjectString(workdays)};
  31. export const inLieuDays: Record<string, Holiday> = ${formatObjectString(inLieuDays)};
  32. `;
  33. // 保存到 constants.ts 文件
  34. fs.writeFile('./src/holidays/constants.ts', tsContent, (err) => {
  35. if (err) throw err;
  36. console.log('The file has been saved!');
  37. });