configGenerator.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // 可替换素材:优先用用户上传的,否则用模板默认
  41. const replaceableAssets = [
  42. { key: "logo", file: "logo.png", var: "logoUrl", defaultPath: "/assets/img/logo.png" },
  43. { key: "logoTxt", file: "logo-txt.png", var: "logoTxtUrl", defaultPath: "/assets/img/logo-txt.png" },
  44. { key: "slogon", file: "slogon.png", var: "slogonUrl", defaultPath: "/assets/img/slogon.png" },
  45. { key: "coloringPages", file: "coloring-pages.png", var: "coloringPagesUrl", defaultPath: "/assets/img/coloring-pages.png" },
  46. ];
  47. for (const a of replaceableAssets) {
  48. const userFile = files[a.key];
  49. if (userFile) {
  50. lines.push(`import ${a.var} from "/assets/user/${userFile}?url";`);
  51. }
  52. else {
  53. lines.push(`import ${a.var} from "${a.defaultPath}?url";`);
  54. }
  55. }
  56. // == adAssets 导出 ==
  57. lines.push("");
  58. lines.push("export const adAssets = {");
  59. lines.push(" configRaw,");
  60. lines.push(" pageUrl,");
  61. lines.push(" mapUrl,");
  62. if (hasSpecial) {
  63. lines.push(" specialUrl,");
  64. }
  65. lines.push(" numberFontUrl,");
  66. lines.push(" fingerUrl,");
  67. lines.push(" logoUrl,");
  68. lines.push(" logoTxtUrl,");
  69. lines.push(" coloringPagesUrl,");
  70. lines.push(" slogonUrl,");
  71. lines.push("};");
  72. // == adTheme 导出 ==
  73. lines.push("");
  74. lines.push("export const adTheme = {");
  75. lines.push(` bgGradient: ${JSON.stringify(input.theme.bgGradient || "linear-gradient(160deg, #fff9f2 0%, #ffeedd 100%)")},`);
  76. lines.push(` ctaGradient: ${JSON.stringify(input.theme.ctaGradient || "linear-gradient(135deg, #ff5f1f 0%, #ffb300 100%)")},`);
  77. lines.push(` ctaGlowColor: ${JSON.stringify(input.theme.ctaGlowColor || "#ff5f1f")},`);
  78. lines.push(` ctaText: ${JSON.stringify(input.theme.ctaText || "PLAY NOW")},`);
  79. lines.push(` storeUrl: ${JSON.stringify(input.theme.storeUrl || "https://play.google.com/store/apps/details?id=com.pcoloring.art.puzzle.color.by.number")},`);
  80. lines.push(` frameColor: ${JSON.stringify(input.theme.frameColor || "#edce9b")},`);
  81. lines.push(` frameLineWidth: ${JSON.stringify(Number(input.theme.frameLineWidth) || 16)},`);
  82. lines.push("};");
  83. return lines.join("\n") + "\n";
  84. }
  85. /**
  86. * 创建 symlink:templates/coloring/assets/user/ → storage/creatives/<id>/assets/
  87. */
  88. function createAssetsSymlink(creativeId, storageDir) {
  89. const symlinkPath = path_1.default.join(TEMPLATE_DIR, "assets", "user");
  90. const targetPath = path_1.default.join(storageDir, "creatives", creativeId, "assets");
  91. // 清理旧的 symlink(用 try-lstat 而非 existsSync,因为悬空 symlink 的 existsSync 返回 false)
  92. try {
  93. const stat = fs_1.default.lstatSync(symlinkPath);
  94. if (stat.isSymbolicLink()) {
  95. fs_1.default.unlinkSync(symlinkPath);
  96. }
  97. else {
  98. fs_1.default.rmSync(symlinkPath, { recursive: true, force: true });
  99. }
  100. }
  101. catch {
  102. // 路径不存在,无需清理
  103. }
  104. fs_1.default.symlinkSync(targetPath, symlinkPath, "dir");
  105. console.log(`[config] Symlink created: ${symlinkPath} → ${targetPath}`);
  106. }
  107. /**
  108. * 清理构建产生的临时文件
  109. */
  110. function cleanupBuildArtifacts() {
  111. const adConfigPath = path_1.default.join(TEMPLATE_DIR, "src", "filler", "_ad_config_.ts");
  112. if (fs_1.default.existsSync(adConfigPath)) {
  113. fs_1.default.unlinkSync(adConfigPath);
  114. }
  115. // symlink 保留,下次构建复用
  116. }
  117. //# sourceMappingURL=configGenerator.js.map