import { readdirSync, copyFileSync, mkdirSync, readFileSync } from 'fs'; import { join, dirname } from 'path'; import { fileURLToPath } from 'url'; import { parse } from 'yaml'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); export interface TemplateInfo { name: string; displayName: string; description: string; } export function listTemplates(): TemplateInfo[] { try { const entries = readdirSync(__dirname, { withFileTypes: true }); return entries .filter(e => e.isDirectory()) .map(e => { try { const meta = parse(readFileSync(join(__dirname, e.name, 'template.yaml'), 'utf-8')) as { displayName?: string; description?: string; }; return { name: e.name, displayName: meta.displayName ?? e.name, description: meta.description ?? '', }; } catch { return { name: e.name, displayName: e.name, description: '' }; } }); } catch { return []; } } export function copyTemplate(name: string, targetDir: string): void { const srcDir = join(__dirname, name); let files: string[]; try { files = readdirSync(srcDir).filter(f => f !== 'template.yaml' && !f.startsWith('.')); } catch { throw new Error(`Template '${name}' not found`); } mkdirSync(targetDir, { recursive: true }); for (const file of files) { copyFileSync(join(srcDir, file), join(targetDir, file)); } } export function getTemplateYaml(name: string): string { const yamlPath = join(__dirname, name, 'template.yaml'); return readFileSync(yamlPath, 'utf-8'); }