init.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. // 映射函数
  10. type RecordStringHoliday = Record<string, Holiday>;
  11. type RecordStringName = Record<string, { name: string }>;
  12. const mapToEnum = (obj: Record<string, string>): Record<string, any> => {
  13. const result: Record<string, any> = {};
  14. for (const [key, value] of Object.entries(obj)) {
  15. if (holidayMap[value]) {
  16. result[key] = `Holiday.${holidayMap[value]}`;
  17. } else {
  18. result[key] = `{ name: "${value}" }`;
  19. }
  20. }
  21. return result;
  22. };
  23. // 转换数据
  24. const holidays = mapToEnum(data.holidays);
  25. const workdays = mapToEnum(data.workdays);
  26. const inLieuDays = mapToEnum(data.inLieuDays);
  27. const formatObjectString = (obj: Record<string, any>): string => {
  28. const entries = Object.entries(obj).map(([key, value]) => ` "${key}": ${value}`);
  29. return `{\n${entries.join(",\n")}\n}`;
  30. };
  31. const tsContent = `import { Holiday } from "./arrangement";
  32. export const holidays: Record<string, Holiday> = ${formatObjectString(holidays)};
  33. export const workdays: Record<string, Holiday> = ${formatObjectString(workdays)};
  34. export const inLieuDays: Record<string, Holiday> = ${formatObjectString(inLieuDays)};
  35. `;
  36. // 保存到 arrangement.ts 文件
  37. fs.writeFile('./src/holidays/constants.ts', tsContent, (err) => {
  38. if (err) throw err;
  39. console.log('The file has been saved!');
  40. });