import { writeFile } from 'fs/promises'; import path from 'path'; interface Event { title: string; date: string; description: string; imageUrl: string; } interface GalleryImage { imageUrl: string; altText: string; } interface ContentSection { [key: string]: any; } export class FileGeneratorService { escapeQuotes(str: string): string { return str.replace(/"/g, '\\"'); } escapeBackticks(str: string): string { return str.replace(/`/g, '\\`').replace(/\${/g, '\\${'); } generateIndexAstro(events: Event[], images: GalleryImage[]): string { const eventsCode = events.map(e => `\t{ \t\timage: "${e.imageUrl}", \t\ttitle: "${this.escapeQuotes(e.title)}", \t\tdate: "${e.date}", \t\tdescription: \` \t\t\t${this.escapeBackticks(e.description)} \t\t\`, \t}`).join(',\n'); const imagesCode = images.map(g => `\t{ src: "${g.imageUrl}", alt: "${this.escapeQuotes(g.altText)}" }` ).join(',\n'); return `--- import Layout from "../components/Layout.astro"; import Hero from "../components/Hero.astro"; import Welcome from "../components/Welcome.astro"; import EventsGrid from "../components/EventsGrid.astro"; import Drinks from "../components/Drinks.astro"; import ImageCarousel from "../components/ImageCarousel.astro"; import Contact from "../components/Contact.astro"; import About from "../components/About.astro"; const events = [ ${eventsCode} ]; const images = [ ${imagesCode} ]; --- \t \t \t \t \t `; } generateHeroComponent(content: ContentSection): string { return `--- // src/components/Hero.astro import "../styles/components/Hero.css" const { id } = Astro.props; ---
\t
\t\t
\t\t\t

${content.heading || 'Dein Irish Pub'}

\t\t\t

${content.subheading || 'Im Herzen von St.Gallen'}

\t\t\tAktuelles ↓ \t\t
\t
`; } generateWelcomeComponent(content: ContentSection): string { const highlightsList = (content.highlights || []).map((h: any) => `\t\t\t
  • \n\t\t\t\t${h.title}: ${h.description}\n\t\t\t
  • ` ).join('\n\n'); return `--- // src/components/Welcome.astro import "../styles/components/Welcome.css" const { id } = Astro.props; ---
    \t
    \t\t

    ${content.heading1 || 'Herzlich willkommen im'}

    \t\t

    ${content.heading2 || 'Gallus Pub!'}

    \t\t

    \t\t\t${content.introText || ''} \t\t

    \t\t

    Unsere Highlights:

    \t\t \t\t

    \t\t\t${content.closingText || ''} \t\t

    \t
    \t
    \t\tWelcome background image \t
    `; } generateDrinksComponent(content: ContentSection): string { return `--- import "../styles/components/Drinks.css" const { id } = Astro.props; ---

    Drinks

    ${content.introText || 'Ob ein frisch gezapftes Pint, ein edler Tropfen Whiskey oder ein gemütliches Glas Wein – hier kannst du in entspannter Atmosphäre das Leben genießen.'}

    Getränkekarte

    Monats Hit

    Monats Hit
    ${content.monthlySpecialName || 'Mate Vodka'}

    ${content.whiskeyText || 'Für Whisky-Liebhaber haben wir erlesene Sorten aus Schottland und Irland im Angebot.'}

    Whiskey 1
    Whiskey 2
    Whiskey 3
    `; } async writeFiles( workspaceDir: string, events: Event[], images: GalleryImage[], sections: Map ) { // Write index.astro const indexContent = this.generateIndexAstro(events, images); await writeFile( path.join(workspaceDir, 'src/pages/index.astro'), indexContent, 'utf-8' ); // Write Hero component if (sections.has('hero')) { const heroContent = this.generateHeroComponent(sections.get('hero')!); await writeFile( path.join(workspaceDir, 'src/components/Hero.astro'), heroContent, 'utf-8' ); } // Write Welcome component if (sections.has('welcome')) { const welcomeContent = this.generateWelcomeComponent(sections.get('welcome')!); await writeFile( path.join(workspaceDir, 'src/components/Welcome.astro'), welcomeContent, 'utf-8' ); } // Write Drinks component if (sections.has('drinks')) { const drinksContent = this.generateDrinksComponent(sections.get('drinks')!); await writeFile( path.join(workspaceDir, 'src/components/Drinks.astro'), drinksContent, 'utf-8' ); } } }