#!/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 TEMPLATES_BASE_DIR = join(import.meta.dirname!, "..", "src", "templates"); const OUTPUT_FILE = join(import.meta.dirname!, "..", "src", "templates", "embedded.ts"); const TEMPLATE_NAMES = ["deno-ssr"]; interface TemplateFile { template: string; // Template name (e.g., "deno-ssr", "deno-graphql") path: string; content: string; } async function embedTemplates() { console.log("Embedding templates from:", TEMPLATES_BASE_DIR); const templates: TemplateFile[] = []; // Walk through each template directory for (const templateName of TEMPLATE_NAMES) { const templateDir = join(TEMPLATES_BASE_DIR, templateName); console.log(`\nProcessing template: ${templateName}`); // Walk through all files in the template directory for await (const entry of walk(templateDir, { includeFiles: true, includeDirs: false })) { const relativePath = relative(templateDir, entry.path); const content = await Deno.readFile(entry.path); const base64Content = encodeBase64(content); templates.push({ template: templateName, 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 { template: string; // Template name (e.g., "deno-ssr", "deno-graphql") path: string; content: string; // Base64 encoded } export const EMBEDDED_TEMPLATES: EmbeddedTemplate[] = ${JSON.stringify(templates, null, 2)}; export const AVAILABLE_TEMPLATES = ${JSON.stringify(TEMPLATE_NAMES)}; export function getTemplateContent(templateName: string, path: string): Uint8Array | undefined { const template = EMBEDDED_TEMPLATES.find(t => t.template === templateName && 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; } export function getTemplatesForName(templateName: string): Map { const result = new Map(); for (const template of EMBEDDED_TEMPLATES) { if (template.template !== templateName) continue; 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(); }