|
| 1 | +import { chromium } from "playwright"; |
| 2 | +import { readFileSync, writeFileSync, cpSync, mkdirSync, readdirSync } from "node:fs"; |
| 3 | +import { execSync } from "node:child_process"; |
| 4 | + |
| 5 | +const assetsDir = "./build/assets"; |
| 6 | +mkdirSync(assetsDir, { recursive: true }); |
| 7 | + |
| 8 | +// --------------------------------------------------------------------------- |
| 9 | +// 1. Determine version via semantic-release dry-run |
| 10 | +// --------------------------------------------------------------------------- |
| 11 | + |
| 12 | +let version = "unreleased"; |
| 13 | +try { |
| 14 | + const branch = execSync("git branch --show-current", { |
| 15 | + encoding: "utf-8", |
| 16 | + }).trim(); |
| 17 | + const output = execSync( |
| 18 | + `npx semantic-release --dry-run --no-ci --branches ${branch}`, |
| 19 | + { |
| 20 | + encoding: "utf-8", |
| 21 | + env: { ...process.env, GITHUB_ACTIONS: "" }, |
| 22 | + stdio: ["pipe", "pipe", "pipe"], |
| 23 | + } |
| 24 | + ); |
| 25 | + const match = output.match(/next release version is (\d+\.\d+\.\d+)/); |
| 26 | + if (match) version = match[1]; |
| 27 | +} catch { |
| 28 | + // no release pending or no git tags yet |
| 29 | +} |
| 30 | +console.log(`Version: ${version}`); |
| 31 | + |
| 32 | +const date = new Date().toISOString().split("T")[0]; |
| 33 | +const commit = execSync("git rev-parse HEAD", { encoding: "utf-8" }).trim(); |
| 34 | + |
| 35 | +writeFileSync( |
| 36 | + "./build/version.md", |
| 37 | + `# fink-brand\nversion: ${version}\nreleased: ${date}\ncommit: ${commit}\n` |
| 38 | +); |
| 39 | + |
| 40 | +// --------------------------------------------------------------------------- |
| 41 | +// 2. Render SVGs to PNGs |
| 42 | +// --------------------------------------------------------------------------- |
| 43 | + |
| 44 | +const svgFiles = readdirSync("assets").filter((f) => f.endsWith(".svg")); |
| 45 | +const browser = await chromium.launch(); |
| 46 | + |
| 47 | +for (const file of svgFiles) { |
| 48 | + cpSync(`assets/${file}`, `${assetsDir}/${file}`); |
| 49 | + |
| 50 | + const svg = readFileSync(`assets/${file}`, "utf-8"); |
| 51 | + |
| 52 | + const widthMatch = svg.match(/width="(\d+)"/); |
| 53 | + const heightMatch = svg.match(/height="(\d+)"/); |
| 54 | + const width = widthMatch ? parseInt(widthMatch[1]) : 512; |
| 55 | + const height = heightMatch ? parseInt(heightMatch[1]) : 512; |
| 56 | + |
| 57 | + const page = await browser.newPage({ viewport: { width, height } }); |
| 58 | + |
| 59 | + const html = `<!DOCTYPE html> |
| 60 | +<html><head><style> |
| 61 | + body { margin: 0; background: transparent; } |
| 62 | + img { display: block; width: ${width}px; height: ${height}px; } |
| 63 | +</style></head> |
| 64 | +<body><img src="data:image/svg+xml;base64,${Buffer.from(svg).toString("base64")}"></body></html>`; |
| 65 | + |
| 66 | + await page.setContent(html, { waitUntil: "load" }); |
| 67 | + |
| 68 | + const outName = file.replace(".svg", ".png"); |
| 69 | + await page.screenshot({ |
| 70 | + path: `${assetsDir}/${outName}`, |
| 71 | + omitBackground: true, |
| 72 | + }); |
| 73 | + |
| 74 | + console.log(` ${file} → ${outName} (${width}x${height})`); |
| 75 | + await page.close(); |
| 76 | +} |
| 77 | + |
| 78 | +await browser.close(); |
| 79 | + |
| 80 | +// --------------------------------------------------------------------------- |
| 81 | +// 3. Package tarball |
| 82 | +// --------------------------------------------------------------------------- |
| 83 | + |
| 84 | +execSync("tar -czf build/brand-assets.tar.gz -C build version.md assets"); |
| 85 | +console.log(`\nDone. brand-assets.tar.gz (v${version})`); |
0 commit comments