#!/usr/bin/env -S deno run --allow-read --allow-write import { walk } from "@std/fs"; import { relative, join } from "@std/path"; import { encodeBase64 } from "@std/encoding/base64"; const TEMPLATE_DIR = join(import.meta.dirname!, "..", "src", "templates", "deno-ssr"); const OUTPUT_FILE = join(import.meta.dirname!, "..", "src", "templates", "embedded.ts"); interface TemplateFile { path: string; content: string; } async function embedTemplates() { console.log("Embedding templates from:", TEMPLATE_DIR); const templates: TemplateFile[] = []; // Walk through all files in the template directory for await (const entry of walk(TEMPLATE_DIR, { includeFiles: true, includeDirs: false })) { const relativePath = relative(TEMPLATE_DIR, entry.path); const content = await Deno.readFile(entry.path); const base64Content = encodeBase64(content); templates.push({ path: relativePath, content: base64Content, }); console.log(` Embedded: ${relativePath}`); } // Generate TypeScript file with embedded templates const output = `// Auto-generated file - DO NOT EDIT // Generated by scripts/embed-templates.ts export interface EmbeddedTemplate { path: string; content: string; // Base64 encoded } export const EMBEDDED_TEMPLATES: EmbeddedTemplate[] = ${JSON.stringify(templates, null, 2)}; export function getTemplateContent(path: string): Uint8Array | undefined { const template = EMBEDDED_TEMPLATES.find(t => t.path === path); if (!template) return undefined; const binaryString = atob(template.content); const bytes = new Uint8Array(binaryString.length); for (let i = 0; i < binaryString.length; i++) { bytes[i] = binaryString.charCodeAt(i); } return bytes; } export function getAllTemplates(): Map { const result = new Map(); for (const template of EMBEDDED_TEMPLATES) { const binaryString = atob(template.content); const bytes = new Uint8Array(binaryString.length); for (let i = 0; i < binaryString.length; i++) { bytes[i] = binaryString.charCodeAt(i); } result.set(template.path, bytes); } return result; } `; await Deno.writeTextFile(OUTPUT_FILE, output); console.log(`\nGenerated: ${OUTPUT_FILE}`); console.log(`Total templates embedded: ${templates.length}`); } if (import.meta.main) { await embedTemplates(); }