Highly ambitious ATProtocol AppView service and sdks
at fix-postgres 106 lines 3.5 kB view raw
1#!/usr/bin/env -S deno run --allow-read --allow-write 2 3import { walk } from "@std/fs"; 4import { relative, join } from "@std/path"; 5import { encodeBase64 } from "@std/encoding/base64"; 6 7const TEMPLATES_BASE_DIR = join(import.meta.dirname!, "..", "src", "templates"); 8const OUTPUT_FILE = join(import.meta.dirname!, "..", "src", "templates", "embedded.ts"); 9 10const TEMPLATE_NAMES = ["deno-ssr"]; 11 12interface TemplateFile { 13 template: string; // Template name (e.g., "deno-ssr", "deno-graphql") 14 path: string; 15 content: string; 16} 17 18async function embedTemplates() { 19 console.log("Embedding templates from:", TEMPLATES_BASE_DIR); 20 21 const templates: TemplateFile[] = []; 22 23 // Walk through each template directory 24 for (const templateName of TEMPLATE_NAMES) { 25 const templateDir = join(TEMPLATES_BASE_DIR, templateName); 26 console.log(`\nProcessing template: ${templateName}`); 27 28 // Walk through all files in the template directory 29 for await (const entry of walk(templateDir, { includeFiles: true, includeDirs: false })) { 30 const relativePath = relative(templateDir, entry.path); 31 const content = await Deno.readFile(entry.path); 32 const base64Content = encodeBase64(content); 33 34 templates.push({ 35 template: templateName, 36 path: relativePath, 37 content: base64Content, 38 }); 39 40 console.log(` Embedded: ${relativePath}`); 41 } 42 } 43 44 // Generate TypeScript file with embedded templates 45 const output = `// Auto-generated file - DO NOT EDIT 46// Generated by scripts/embed-templates.ts 47 48export interface EmbeddedTemplate { 49 template: string; // Template name (e.g., "deno-ssr", "deno-graphql") 50 path: string; 51 content: string; // Base64 encoded 52} 53 54export const EMBEDDED_TEMPLATES: EmbeddedTemplate[] = ${JSON.stringify(templates, null, 2)}; 55 56export const AVAILABLE_TEMPLATES = ${JSON.stringify(TEMPLATE_NAMES)}; 57 58export function getTemplateContent(templateName: string, path: string): Uint8Array | undefined { 59 const template = EMBEDDED_TEMPLATES.find(t => t.template === templateName && t.path === path); 60 if (!template) return undefined; 61 62 const binaryString = atob(template.content); 63 const bytes = new Uint8Array(binaryString.length); 64 for (let i = 0; i < binaryString.length; i++) { 65 bytes[i] = binaryString.charCodeAt(i); 66 } 67 return bytes; 68} 69 70export function getAllTemplates(): Map<string, Uint8Array> { 71 const result = new Map<string, Uint8Array>(); 72 for (const template of EMBEDDED_TEMPLATES) { 73 const binaryString = atob(template.content); 74 const bytes = new Uint8Array(binaryString.length); 75 for (let i = 0; i < binaryString.length; i++) { 76 bytes[i] = binaryString.charCodeAt(i); 77 } 78 result.set(template.path, bytes); 79 } 80 return result; 81} 82 83export function getTemplatesForName(templateName: string): Map<string, Uint8Array> { 84 const result = new Map<string, Uint8Array>(); 85 for (const template of EMBEDDED_TEMPLATES) { 86 if (template.template !== templateName) continue; 87 88 const binaryString = atob(template.content); 89 const bytes = new Uint8Array(binaryString.length); 90 for (let i = 0; i < binaryString.length; i++) { 91 bytes[i] = binaryString.charCodeAt(i); 92 } 93 result.set(template.path, bytes); 94 } 95 return result; 96} 97`; 98 99 await Deno.writeTextFile(OUTPUT_FILE, output); 100 console.log(`\nGenerated: ${OUTPUT_FILE}`); 101 console.log(`Total templates embedded: ${templates.length}`); 102} 103 104if (import.meta.main) { 105 await embedTemplates(); 106}