configGenerator.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.generateAdConfig = generateAdConfig;
  7. exports.createAssetsSymlink = createAssetsSymlink;
  8. exports.cleanupBuildArtifacts = cleanupBuildArtifacts;
  9. const fs_1 = __importDefault(require("fs"));
  10. const path_1 = __importDefault(require("path"));
  11. const storageService_1 = require("./storageService");
  12. const TEMPLATE_DIR = path_1.default.resolve(__dirname, "../../../../templates/coloring");
  13. /**
  14. * 扫描用户素材目录,生成 _ad_config_.ts 内容。
  15. *
  16. * 关键逻辑:
  17. * - 必填素材 (config/page/map) → 必定 import
  18. * - optional 素材 (special) → 存在才 import,不存在则不生成对应字段
  19. * - 模板自有素材 → import 指向 templates/coloring/assets/
  20. */
  21. function generateAdConfig(input) {
  22. const assetsDir = path_1.default.join(input.storageDir, "creatives", input.creativeId, "assets");
  23. const files = (0, storageService_1.scanAssetFiles)(assetsDir);
  24. const lines = [];
  25. // == 用户素材 ==
  26. lines.push("// ==== 用户上传素材 ====");
  27. lines.push(`import configRaw from "/assets/user/config.json?raw";`);
  28. lines.push(`import pageUrl from "/assets/user/page.png?url";`);
  29. lines.push(`import mapUrl from "/assets/user/map.png?url";`);
  30. let hasSpecial = false;
  31. if (files.special) {
  32. hasSpecial = true;
  33. lines.push(`import specialUrl from "/assets/user/${files.special}?url";`);
  34. }
  35. // == 模板自有素材 ==
  36. lines.push("");
  37. lines.push("// ==== 模板自有素材 ====");
  38. lines.push(`import numberFontUrl from "/assets/fonts/numbers_roboto_500.png?url";`);
  39. lines.push(`import fingerUrl from "/assets/img/finger.png?url";`);
  40. lines.push(`import logoUrl from "/assets/img/logo.png?url";`);
  41. lines.push(`import logoTxtUrl from "/assets/img/logo-txt.png?url";`);
  42. lines.push(`import coloringPagesUrl from "/assets/img/coloring-pages.png?url";`);
  43. lines.push(`import slogonUrl from "/assets/img/slogon.png?url";`);
  44. // == adAssets 导出 ==
  45. lines.push("");
  46. lines.push("export const adAssets = {");
  47. lines.push(" configRaw,");
  48. lines.push(" pageUrl,");
  49. lines.push(" mapUrl,");
  50. if (hasSpecial) {
  51. lines.push(" specialUrl,");
  52. }
  53. lines.push(" numberFontUrl,");
  54. lines.push(" fingerUrl,");
  55. lines.push(" logoUrl,");
  56. lines.push(" logoTxtUrl,");
  57. lines.push(" coloringPagesUrl,");
  58. lines.push(" slogonUrl,");
  59. lines.push("};");
  60. // == adTheme 导出 ==
  61. lines.push("");
  62. lines.push("export const adTheme = {");
  63. lines.push(` bgGradient: ${JSON.stringify(input.theme.bgGradient || "linear-gradient(160deg, #fff9f2 0%, #ffeedd 100%)")},`);
  64. lines.push(` ctaGradient: ${JSON.stringify(input.theme.ctaGradient || "linear-gradient(135deg, #ff5f1f 0%, #ffb300 100%)")},`);
  65. lines.push(` ctaText: ${JSON.stringify(input.theme.ctaText || "PLAY NOW")},`);
  66. lines.push(` frameColor: ${JSON.stringify(input.theme.frameColor || "#edce9b")},`);
  67. lines.push(` frameLineWidth: ${JSON.stringify(Number(input.theme.frameLineWidth) || 16)},`);
  68. lines.push("};");
  69. return lines.join("\n") + "\n";
  70. }
  71. /**
  72. * 创建 symlink:templates/coloring/assets/user/ → storage/creatives/<id>/assets/
  73. */
  74. function createAssetsSymlink(creativeId, storageDir) {
  75. const symlinkPath = path_1.default.join(TEMPLATE_DIR, "assets", "user");
  76. const targetPath = path_1.default.join(storageDir, "creatives", creativeId, "assets");
  77. // 清理旧的 symlink(用 try-lstat 而非 existsSync,因为悬空 symlink 的 existsSync 返回 false)
  78. try {
  79. const stat = fs_1.default.lstatSync(symlinkPath);
  80. if (stat.isSymbolicLink()) {
  81. fs_1.default.unlinkSync(symlinkPath);
  82. }
  83. else {
  84. fs_1.default.rmSync(symlinkPath, { recursive: true, force: true });
  85. }
  86. }
  87. catch {
  88. // 路径不存在,无需清理
  89. }
  90. fs_1.default.symlinkSync(targetPath, symlinkPath, "dir");
  91. console.log(`[config] Symlink created: ${symlinkPath} → ${targetPath}`);
  92. }
  93. /**
  94. * 清理构建产生的临时文件
  95. */
  96. function cleanupBuildArtifacts() {
  97. const adConfigPath = path_1.default.join(TEMPLATE_DIR, "src", "filler", "_ad_config_.ts");
  98. if (fs_1.default.existsSync(adConfigPath)) {
  99. fs_1.default.unlinkSync(adConfigPath);
  100. }
  101. // symlink 保留,下次构建复用
  102. }
  103. //# sourceMappingURL=configGenerator.js.map