| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- "use strict";
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.generateAdConfig = generateAdConfig;
- exports.createAssetsSymlink = createAssetsSymlink;
- exports.cleanupBuildArtifacts = cleanupBuildArtifacts;
- const fs_1 = __importDefault(require("fs"));
- const path_1 = __importDefault(require("path"));
- const storageService_1 = require("./storageService");
- const TEMPLATE_DIR = path_1.default.resolve(__dirname, "../../../../templates/coloring");
- /**
- * 扫描用户素材目录,生成 _ad_config_.ts 内容。
- *
- * 关键逻辑:
- * - 必填素材 (config/page/map) → 必定 import
- * - optional 素材 (special) → 存在才 import,不存在则不生成对应字段
- * - 模板自有素材 → import 指向 templates/coloring/assets/
- */
- function generateAdConfig(input) {
- const assetsDir = path_1.default.join(input.storageDir, "creatives", input.creativeId, "assets");
- const files = (0, storageService_1.scanAssetFiles)(assetsDir);
- const lines = [];
- // == 用户素材 ==
- lines.push("// ==== 用户上传素材 ====");
- lines.push(`import configRaw from "/assets/user/config.json?raw";`);
- lines.push(`import pageUrl from "/assets/user/page.png?url";`);
- lines.push(`import mapUrl from "/assets/user/map.png?url";`);
- let hasSpecial = false;
- if (files.special) {
- hasSpecial = true;
- lines.push(`import specialUrl from "/assets/user/${files.special}?url";`);
- }
- // == 模板自有素材(用户上传则优先使用)==
- lines.push("");
- lines.push("// ==== 模板自有素材 ====");
- lines.push(`import numberFontUrl from "/assets/fonts/numbers_roboto_500.png?url";`);
- lines.push(`import fingerUrl from "/assets/img/finger.png?url";`);
- // 可替换素材:优先用用户上传的,否则用模板默认
- const replaceableAssets = [
- { key: "logo", file: "logo.png", var: "logoUrl", defaultPath: "/assets/img/logo.png" },
- { key: "logoTxt", file: "logo-txt.png", var: "logoTxtUrl", defaultPath: "/assets/img/logo-txt.png" },
- { key: "slogon", file: "slogon.png", var: "slogonUrl", defaultPath: "/assets/img/slogon.png" },
- { key: "coloringPages", file: "coloring-pages.png", var: "coloringPagesUrl", defaultPath: "/assets/img/coloring-pages.png" },
- ];
- for (const a of replaceableAssets) {
- const userFile = files[a.key];
- if (userFile) {
- lines.push(`import ${a.var} from "/assets/user/${userFile}?url";`);
- }
- else {
- lines.push(`import ${a.var} from "${a.defaultPath}?url";`);
- }
- }
- // == adAssets 导出 ==
- lines.push("");
- lines.push("export const adAssets = {");
- lines.push(" configRaw,");
- lines.push(" pageUrl,");
- lines.push(" mapUrl,");
- if (hasSpecial) {
- lines.push(" specialUrl,");
- }
- lines.push(" numberFontUrl,");
- lines.push(" fingerUrl,");
- lines.push(" logoUrl,");
- lines.push(" logoTxtUrl,");
- lines.push(" coloringPagesUrl,");
- lines.push(" slogonUrl,");
- lines.push("};");
- // == adTheme 导出 ==
- lines.push("");
- lines.push("export const adTheme = {");
- lines.push(` bgGradient: ${JSON.stringify(input.theme.bgGradient || "linear-gradient(160deg, #fff9f2 0%, #ffeedd 100%)")},`);
- lines.push(` ctaGradient: ${JSON.stringify(input.theme.ctaGradient || "linear-gradient(135deg, #ff5f1f 0%, #ffb300 100%)")},`);
- lines.push(` ctaGlowColor: ${JSON.stringify(input.theme.ctaGlowColor || "#ff5f1f")},`);
- lines.push(` ctaText: ${JSON.stringify(input.theme.ctaText || "PLAY NOW")},`);
- lines.push(` storeUrl: ${JSON.stringify(input.theme.storeUrl || "https://play.google.com/store/apps/details?id=com.pcoloring.art.puzzle.color.by.number")},`);
- lines.push(` frameColor: ${JSON.stringify(input.theme.frameColor || "#edce9b")},`);
- lines.push(` frameLineWidth: ${JSON.stringify(Number(input.theme.frameLineWidth) || 16)},`);
- lines.push("};");
- return lines.join("\n") + "\n";
- }
- /**
- * 创建 symlink:templates/coloring/assets/user/ → storage/creatives/<id>/assets/
- */
- function createAssetsSymlink(creativeId, storageDir) {
- const symlinkPath = path_1.default.join(TEMPLATE_DIR, "assets", "user");
- const targetPath = path_1.default.join(storageDir, "creatives", creativeId, "assets");
- // 清理旧的 symlink(用 try-lstat 而非 existsSync,因为悬空 symlink 的 existsSync 返回 false)
- try {
- const stat = fs_1.default.lstatSync(symlinkPath);
- if (stat.isSymbolicLink()) {
- fs_1.default.unlinkSync(symlinkPath);
- }
- else {
- fs_1.default.rmSync(symlinkPath, { recursive: true, force: true });
- }
- }
- catch {
- // 路径不存在,无需清理
- }
- fs_1.default.symlinkSync(targetPath, symlinkPath, "dir");
- console.log(`[config] Symlink created: ${symlinkPath} → ${targetPath}`);
- }
- /**
- * 清理构建产生的临时文件
- */
- function cleanupBuildArtifacts() {
- const adConfigPath = path_1.default.join(TEMPLATE_DIR, "src", "filler", "_ad_config_.ts");
- if (fs_1.default.existsSync(adConfigPath)) {
- fs_1.default.unlinkSync(adConfigPath);
- }
- // symlink 保留,下次构建复用
- }
- //# sourceMappingURL=configGenerator.js.map
|