@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 180 lines 7.29 KB
Raw
1 /**
2 * Build-time brand asset generator. Runs before `astro build` and emits
3 * favicons + a fallback OG image into `website/public/`, all derived from
4 * the single yantra geometry source in `src/brand/yantra.ts`.
5 *
6 * The per-page OG images are rendered separately by the static endpoint at
7 * `src/pages/og/[...slug].webp.ts` during `astro build`; this script only
8 * produces brand artifacts that need to exist on disk before Astro starts
9 * (so they're picked up by the public/ asset pipeline).
10 */
11
12 import { mkdir, writeFile } from "node:fs/promises";
13 import path from "node:path";
14 import { fileURLToPath } from "node:url";
15 import { render, renderSvg } from "takumi-js";
16 import { brandFonts } from "../src/brand/fonts.ts";
17 import { OG_DEFAULT_H, OG_DEFAULT_W, OgDefault } from "../src/brand/OgDefault.tsx";
18 import { YANTRA_THEMES, type YantraTheme, yantraSvg } from "../src/brand/yantra.ts";
19
20 const here = path.dirname(fileURLToPath(import.meta.url));
21 const publicDir = path.resolve(here, "../public");
22 /** Rasterize the geometric icon artwork with Takumi. */
23 function rasterize(svg: string, size: number): Promise<Uint8Array> {
24 return render(
25 {
26 type: "image",
27 src: `data:image/svg+xml;base64,${Buffer.from(svg).toString("base64")}`,
28 style: { width: size, height: size },
29 },
30 { width: size, height: size, format: "png" },
31 );
32 }
33
34 /** Favicons use exactly the same geometry and margins as the logo SVG. */
35 function faviconMarkSvg(theme: YantraTheme): string {
36 return yantraSvg({ size: 64, theme });
37 }
38
39 /**
40 * The vector favicon carries both themes in one file: a `prefers-color-scheme`
41 * block re-paints the light mark, and CSS rules beat the presentation
42 * attributes underneath. Chrome, Firefox and Safari 16.4+ re-evaluate it live
43 * when the OS theme flips; anything that ignores the `<style>` (older browsers,
44 * rasterizers) still renders the light mark from the attributes.
45 *
46 * The bindu is the only element carrying a `fill` attribute, so `circle[fill]`
47 * targets it without also hitting the outer circle.
48 */
49 function faviconVectorSvg(): string {
50 const { stroke, dot } = YANTRA_THEMES.dark;
51 const style = `<style>@media (prefers-color-scheme: dark){svg{stroke:${stroke}}circle[fill]{fill:${dot}}}</style>`;
52 return faviconMarkSvg("light").replace(/(<svg[^>]*>)/, `$1${style}`);
53 }
54
55 /** Canonical standalone logo, also used by app icons and the OG fallback. */
56 function brandMarkSvg(theme: YantraTheme): string {
57 return yantraSvg({ size: 512, theme });
58 }
59
60 /** Opaque background treatment with a slightly smaller centered mark. */
61 function backgroundLogoSvg(theme: YantraTheme, background: string): string {
62 const size = 512;
63 const markSize = 448;
64 const offset = (size - markSize) / 2;
65 const mark = brandMarkSvg(theme).replace(
66 'width="512" height="512"',
67 `width="${markSize}" height="${markSize}"`,
68 );
69 return `<svg xmlns="http://www.w3.org/2000/svg" width="${size}" height="${size}" viewBox="0 0 ${size} ${size}">
70 <rect width="${size}" height="${size}" fill="${background}"/>
71 <g transform="translate(${offset} ${offset})">${mark}</g>
72 </svg>`;
73 }
74
75 /**
76 * apple-touch-icon: opaque and generously padded. The one asset that cannot be
77 * transparent — iOS composites the alpha channel against black when it masks
78 * the web clip into its squircle. Light only: iOS web clips have no dark
79 * variant and `<link rel="apple-touch-icon">` ignores `media`.
80 */
81 function appleTouchSvg(): string {
82 const { bg } = YANTRA_THEMES.light;
83 const inner = yantraSvg();
84 return `<svg xmlns="http://www.w3.org/2000/svg" width="180" height="180" viewBox="0 0 32 32">
85 <rect width="32" height="32" fill="${bg}"/>
86 <g transform="translate(4 4)">${inner}</g>
87 </svg>`;
88 }
89
90 const OG_PIXEL_RATIO = 2.5;
91
92 async function main() {
93 await mkdir(publicDir, { recursive: true });
94
95 // 1. Vector favicons: one per theme (picked by `media` on the <link>), plus
96 // a combined file for consumers that can only reference one URL.
97 await writeFile(path.join(publicDir, "favicon.svg"), faviconVectorSvg());
98 for (const theme of ["light", "dark"] as const) {
99 await writeFile(path.join(publicDir, `favicon-${theme}.svg`), faviconMarkSvg(theme));
100 }
101
102 // 2. Raster favicons, one pair per theme. Media queries don't survive
103 // rasterization, so each PNG comes from an explicit-color mark and the
104 // <link> tags pick between them with `media`.
105 const favLight = faviconMarkSvg("light");
106 const favDark = faviconMarkSvg("dark");
107 for (const size of [16, 32] as const) {
108 await writeFile(path.join(publicDir, `favicon-${size}.png`), await rasterize(favLight, size));
109 await writeFile(
110 path.join(publicDir, `favicon-${size}-dark.png`),
111 await rasterize(favDark, size),
112 );
113 }
114
115 // 3. apple-touch-icon (180×180, padded, opaque).
116 await writeFile(
117 path.join(publicDir, "apple-touch-icon.png"),
118 await rasterize(appleTouchSvg(), 180),
119 );
120
121 // 4. The brand mark at 512, per theme — emitted as a transparent standalone
122 // vector, a transparent PWA/share raster, and an opaque logo raster using
123 // the theme background. None of the variants adds a frame or border.
124 for (const theme of ["light", "dark"] as const) {
125 const mark = brandMarkSvg(theme);
126 await writeFile(path.join(publicDir, `alchemy-logo-${theme}.svg`), mark);
127 await writeFile(
128 path.join(publicDir, `icon-512${theme === "dark" ? "-dark" : ""}.png`),
129 await rasterize(mark, 512),
130 );
131 await writeFile(
132 path.join(publicDir, `alchemy-logo-${theme}-bg.png`),
133 await rasterize(backgroundLogoSvg(theme, YANTRA_THEMES[theme].bg), 2048),
134 );
135 }
136
137 // 5. Light brand mark on a true-white ground for consumers that cannot use
138 // the warmer theme background or composite the transparent asset.
139 await writeFile(
140 path.join(publicDir, "alchemy-logo-512-white-bg.png"),
141 await rasterize(backgroundLogoSvg("light", "#ffffff"), 2048),
142 );
143
144 // 6. Monochrome mark: white strokes, transparent vector + dark-ground raster.
145 await writeFile(path.join(publicDir, "alchemy-logo-mono.svg"), brandMarkSvg("mono"));
146 await writeFile(
147 path.join(publicDir, "alchemy-logo-mono-bg.png"),
148 await rasterize(backgroundLogoSvg("mono", YANTRA_THEMES.mono.bg), 2048),
149 );
150
151 // 7. Backwards-compat: keep the old /favicon.png reference (used by
152 // some cached nav code) pointing to the 32px raster.
153 await writeFile(path.join(publicDir, "favicon.png"), await rasterize(favLight, 32));
154
155 // 8. Fallback OG: Takumi emits both PNG and outlined SVG from one layout.
156 const card = OgDefault();
157 const options = {
158 width: OG_DEFAULT_W,
159 height: OG_DEFAULT_H,
160 fonts: brandFonts,
161 emoji: "from-font" as const,
162 };
163 await writeFile(path.join(publicDir, "og-default.svg"), await renderSvg(card, options));
164 await writeFile(
165 path.join(publicDir, "og-default.png"),
166 await render(card, {
167 ...options,
168 format: "png",
169 width: OG_DEFAULT_W * OG_PIXEL_RATIO,
170 height: OG_DEFAULT_H * OG_PIXEL_RATIO,
171 devicePixelRatio: OG_PIXEL_RATIO,
172 }),
173 );
174
175 console.log(
176 "[brand] wrote favicon[-{light,dark}].svg, favicon.png, favicon-{16,32}[-dark].png, apple-touch-icon.png, icon-512[-dark].png, alchemy-logo-{light,dark}.svg, alchemy-logo-{light,dark}-bg.png (2048px), alchemy-logo-512-white-bg.png (2048px), alchemy-logo-mono.svg, alchemy-logo-mono-bg.png (2048px), og-default.{svg,png}",
177 );
178 }
179
180 await main();