← GPT-AI-LP
CODE · 2.0 KB

scripts/build-layered-local-preview.mjs

Workspace snapshot · 08/31 23:51

import { cp, mkdir, readFile, writeFile } from "node:fs/promises";
import { exportHtml, exportThanksHtml } from "../src/export-html.js";
import { buildExportBundle } from "../src/export-bundle.js";
import { buildVisualPlan } from "../src/visual-strategy.js";

const root = new URL("../output/real-e2e/", import.meta.url);
const layerRoot = new URL("layers/", root);
const previewRoot = new URL("layered-preview/", root);
const previewImages = new URL("images/", previewRoot);
const design = JSON.parse(await readFile(new URL("design.json", root), "utf8"));
const plan = buildVisualPlan({
  project: design.project,
  sections: design.sections,
  direction: design.tone,
  purpose: "implementation",
});

await mkdir(previewImages, { recursive: true });

const images = [];
const bundleImages = [];
for (const visual of plan) {
  const name = `${visual.sectionId}-${visual.layer}.png`;
  const source = new URL(name, layerRoot);
  await cp(source, new URL(name, previewImages));
  const record = {
    sectionId: visual.sectionId,
    visualId: visual.visualId,
    layer: visual.layer,
    status: "COMPLETED",
    src: `./images/${name}`,
    alt: visual.layer === "content" ? visual.copy?.main || "" : "",
    textIntegrated: visual.textIntegrated === true,
  };
  images.push(record);
  const bytes = await readFile(source);
  bundleImages.push({ ...record, src: `data:image/png;base64,${bytes.toString("base64")}` });
}

await writeFile(
  new URL("index.html", previewRoot),
  exportHtml(design.project, design.sections, design.tone, images),
  "utf8",
);
await writeFile(
  new URL("thanks.html", previewRoot),
  exportThanksHtml(design.project, design.tone),
  "utf8",
);
const bundle = await buildExportBundle({
  project: design.project,
  sections: design.sections,
  tone: design.tone,
  images: bundleImages,
});
await writeFile(
  new URL("landing-page-layered-package.zip", root),
  Buffer.from(await bundle.arrayBuffer()),
);

console.log(`built ${images.length} layered images into ${previewRoot.pathname}`);