configGenerator.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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(` progressColor: ${JSON.stringify(input.theme.progressColor || "#07ce07")},`);
  67. lines.push("};");
  68. return lines.join("\n") + "\n";
  69. }
  70. /**
  71. * 创建 symlink:templates/coloring/assets/user/ → storage/creatives/<id>/assets/
  72. */
  73. function createAssetsSymlink(creativeId, storageDir) {
  74. const symlinkPath = path_1.default.join(TEMPLATE_DIR, "assets", "user");
  75. const targetPath = path_1.default.join(storageDir, "creatives", creativeId, "assets");
  76. // 清理旧的 symlink
  77. if (fs_1.default.existsSync(symlinkPath)) {
  78. const stat = fs_1.default.lstatSync(symlinkPath);
  79. if (stat.isSymbolicLink()) {
  80. fs_1.default.unlinkSync(symlinkPath);
  81. }
  82. else {
  83. fs_1.default.rmSync(symlinkPath, { recursive: true, force: true });
  84. }
  85. }
  86. fs_1.default.symlinkSync(targetPath, symlinkPath, "dir");
  87. console.log(`[config] Symlink created: ${symlinkPath} → ${targetPath}`);
  88. }
  89. /**
  90. * 清理构建产生的临时文件
  91. */
  92. function cleanupBuildArtifacts() {
  93. const adConfigPath = path_1.default.join(TEMPLATE_DIR, "src", "filler", "_ad_config_.ts");
  94. if (fs_1.default.existsSync(adConfigPath)) {
  95. fs_1.default.unlinkSync(adConfigPath);
  96. }
  97. // symlink 保留,下次构建复用
  98. }
  99. //# sourceMappingURL=configGenerator.js.map