|
| 1 | +#!/usr/bin/env tsx |
| 2 | +/** |
| 3 | + * Pika marketing asset generator. |
| 4 | + * |
| 5 | + * Usage: |
| 6 | + * npx tsx build/marketing/generate.ts --version <version> [--capture] [--web] [--figma] [--all] |
| 7 | + * |
| 8 | + * Flags: |
| 9 | + * --version Required. Version string, e.g. 1.3.0 or 1.2.0-beta1. |
| 10 | + * --capture Run capture loop via capture.sh (requires Pika running). |
| 11 | + * --web Composite source PNGs into website JPGs. |
| 12 | + * --figma Copy shadow-trimmed PNGs to figma/ output folder. |
| 13 | + * --all Run capture, web, and figma in sequence. |
| 14 | + * |
| 15 | + * Output: pika-releases/Marketing/v<version>/ |
| 16 | + */ |
| 17 | + |
| 18 | +import { execSync } from "child_process"; |
| 19 | +import { copyFileSync, existsSync, mkdirSync, readFileSync } from "fs"; |
| 20 | +import { dirname, join, resolve } from "path"; |
| 21 | +import sharp from "sharp"; |
| 22 | + |
| 23 | +// --------------------------------------------------------------------------- |
| 24 | +// Paths |
| 25 | +// --------------------------------------------------------------------------- |
| 26 | + |
| 27 | +const SCRIPT_DIR = dirname(new URL(import.meta.url).pathname); |
| 28 | +const REPO_ROOT = resolve(SCRIPT_DIR, "../.."); |
| 29 | +const WORKSPACE_ROOT = resolve(REPO_ROOT, ".."); |
| 30 | +const SOURCE_DIR = join(SCRIPT_DIR, "source"); |
| 31 | +const RELEASES_DIR = join(WORKSPACE_ROOT, "pika-releases", "Marketing"); |
| 32 | + |
| 33 | +// --------------------------------------------------------------------------- |
| 34 | +// Manifest |
| 35 | +// --------------------------------------------------------------------------- |
| 36 | + |
| 37 | +interface Shot { |
| 38 | + file: string; |
| 39 | + fg: string; |
| 40 | + bg: string; |
| 41 | + appearance: "light" | "dark"; |
| 42 | + history: "show" | "hide"; |
| 43 | +} |
| 44 | + |
| 45 | +interface Manifest { |
| 46 | + shots: Shot[]; |
| 47 | + web: { dark: string[]; light: string[] }; |
| 48 | + figma: Record<string, string>; |
| 49 | +} |
| 50 | + |
| 51 | +const manifest: Manifest = JSON.parse( |
| 52 | + readFileSync(join(SCRIPT_DIR, "manifest.json"), "utf8") |
| 53 | +); |
| 54 | + |
| 55 | +// --------------------------------------------------------------------------- |
| 56 | +// Args |
| 57 | +// --------------------------------------------------------------------------- |
| 58 | + |
| 59 | +const args = process.argv.slice(2); |
| 60 | + |
| 61 | +function flag(name: string): boolean { |
| 62 | + return args.includes(name); |
| 63 | +} |
| 64 | + |
| 65 | +function arg(name: string): string | undefined { |
| 66 | + const idx = args.indexOf(name); |
| 67 | + return idx !== -1 ? args[idx + 1] : undefined; |
| 68 | +} |
| 69 | + |
| 70 | +const version = arg("--version"); |
| 71 | +if (!version) { |
| 72 | + console.error("Error: --version is required"); |
| 73 | + process.exit(1); |
| 74 | +} |
| 75 | + |
| 76 | +const runCapture = flag("--capture") || flag("--all"); |
| 77 | +const runWeb = flag("--web") || flag("--all"); |
| 78 | +const runFigma = flag("--figma") || flag("--all"); |
| 79 | + |
| 80 | +if (!runCapture && !runWeb && !runFigma) { |
| 81 | + console.error("Error: specify at least one of --capture, --web, --figma, or --all"); |
| 82 | + process.exit(1); |
| 83 | +} |
| 84 | + |
| 85 | +const OUTPUT_DIR = join(RELEASES_DIR, `v${version}`); |
| 86 | +const FIGMA_DIR = join(OUTPUT_DIR, "figma"); |
| 87 | + |
| 88 | +// --------------------------------------------------------------------------- |
| 89 | +// Capture |
| 90 | +// --------------------------------------------------------------------------- |
| 91 | + |
| 92 | +async function runCaptureStep(): Promise<void> { |
| 93 | + console.log("\n── Capture ──────────────────────────────────────"); |
| 94 | + mkdirSync(SOURCE_DIR, { recursive: true }); |
| 95 | + |
| 96 | + const captureScript = join(SCRIPT_DIR, "capture.sh"); |
| 97 | + |
| 98 | + for (const shot of manifest.shots) { |
| 99 | + const outputPath = join(SOURCE_DIR, shot.file); |
| 100 | + const cmd = `"${captureScript}" "${outputPath}" "${shot.fg}" "${shot.bg}" "${shot.appearance}" "${shot.history}"`; |
| 101 | + console.log(` ${shot.file}`); |
| 102 | + execSync(cmd, { stdio: "inherit" }); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// --------------------------------------------------------------------------- |
| 107 | +// Web composite |
| 108 | +// --------------------------------------------------------------------------- |
| 109 | + |
| 110 | +const CANVAS_WIDTH = 2380; |
| 111 | +const CANVAS_HEIGHT = 838; |
| 112 | + |
| 113 | +// Layout: left (x=-30, y=54), center (x=893, y=0), right (x=1820, y=54) |
| 114 | +const WINDOW_POSITIONS = [ |
| 115 | + { left: -30, top: 54 }, |
| 116 | + { left: 893, top: 0 }, |
| 117 | + { left: 1820, top: 54 }, |
| 118 | +]; |
| 119 | + |
| 120 | +const BACKGROUNDS: Record<"dark" | "light", { r: number; g: number; b: number }> = { |
| 121 | + dark: { r: 26, g: 26, b: 26 }, |
| 122 | + light: { r: 255, g: 255, b: 255 }, |
| 123 | +}; |
| 124 | + |
| 125 | +async function compositeWeb(mode: "dark" | "light"): Promise<void> { |
| 126 | + const files = manifest.web[mode]; |
| 127 | + const bg = BACKGROUNDS[mode]; |
| 128 | + |
| 129 | + const trimmed = await Promise.all( |
| 130 | + files.map((f) => |
| 131 | + sharp(join(SOURCE_DIR, f)) |
| 132 | + .trim() |
| 133 | + .toBuffer({ resolveWithObject: true }) |
| 134 | + ) |
| 135 | + ); |
| 136 | + |
| 137 | + const composites = trimmed.map(({ data, info }, i) => ({ |
| 138 | + input: data, |
| 139 | + left: Math.max(0, WINDOW_POSITIONS[i].left), |
| 140 | + top: WINDOW_POSITIONS[i].top, |
| 141 | + })); |
| 142 | + |
| 143 | + mkdirSync(OUTPUT_DIR, { recursive: true }); |
| 144 | + |
| 145 | + const base2x = sharp({ |
| 146 | + create: { |
| 147 | + width: CANVAS_WIDTH, |
| 148 | + height: CANVAS_HEIGHT, |
| 149 | + channels: 3, |
| 150 | + background: bg, |
| 151 | + }, |
| 152 | + }); |
| 153 | + |
| 154 | + const jpg2x = join(OUTPUT_DIR, `pika-screenshot-${mode}@2x.jpg`); |
| 155 | + const jpg1x = join(OUTPUT_DIR, `pika-screenshot-${mode}.jpg`); |
| 156 | + |
| 157 | + const composited = await base2x |
| 158 | + .composite(composites) |
| 159 | + .jpeg({ quality: 95 }) |
| 160 | + .toBuffer(); |
| 161 | + |
| 162 | + await sharp(composited).toFile(jpg2x); |
| 163 | + console.log(` → ${jpg2x}`); |
| 164 | + |
| 165 | + await sharp(composited) |
| 166 | + .resize(Math.round(CANVAS_WIDTH / 2), Math.round(CANVAS_HEIGHT / 2)) |
| 167 | + .jpeg({ quality: 95 }) |
| 168 | + .toFile(jpg1x); |
| 169 | + console.log(` → ${jpg1x}`); |
| 170 | +} |
| 171 | + |
| 172 | +async function runWebStep(): Promise<void> { |
| 173 | + console.log("\n── Web composites ───────────────────────────────"); |
| 174 | + await compositeWeb("dark"); |
| 175 | + await compositeWeb("light"); |
| 176 | +} |
| 177 | + |
| 178 | +// --------------------------------------------------------------------------- |
| 179 | +// Figma export |
| 180 | +// --------------------------------------------------------------------------- |
| 181 | + |
| 182 | +async function runFigmaStep(): Promise<void> { |
| 183 | + console.log("\n── Figma exports ────────────────────────────────"); |
| 184 | + mkdirSync(FIGMA_DIR, { recursive: true }); |
| 185 | + |
| 186 | + for (const [key, file] of Object.entries(manifest.figma)) { |
| 187 | + const src = join(SOURCE_DIR, file); |
| 188 | + const dest = join(FIGMA_DIR, `${key}.png`); |
| 189 | + |
| 190 | + await sharp(src).trim().toFile(dest); |
| 191 | + console.log(` ${key}.png ← ${file}`); |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +// --------------------------------------------------------------------------- |
| 196 | +// Main |
| 197 | +// --------------------------------------------------------------------------- |
| 198 | + |
| 199 | +(async () => { |
| 200 | + console.log(`Pika marketing assets v${version}`); |
| 201 | + console.log(`Output: ${OUTPUT_DIR}`); |
| 202 | + |
| 203 | + if (runCapture) await runCaptureStep(); |
| 204 | + if (runWeb) await runWebStep(); |
| 205 | + if (runFigma) await runFigmaStep(); |
| 206 | + |
| 207 | + console.log("\nDone."); |
| 208 | +})(); |
0 commit comments