import fs from 'fs/promises' import path from 'path' export default defineEventHandler(async (event) => { try { const pageData = await readBody(event) if (!pageData.name || !pageData.path) { throw createError({ statusCode: 400, statusMessage: 'Page name and path are required' }) } // Generate Vue file content const vueTemplate = ` ` // Determine file path const fileName = `${pageData.name.toLowerCase().replace(/\s+/g, '-')}.vue` const pagesDir = path.join(process.cwd(), 'matdash', 'pages') const filePath = path.join(pagesDir, fileName) // Ensure pages directory exists await fs.mkdir(pagesDir, { recursive: true }) // Write Vue file await fs.writeFile(filePath, vueTemplate) // Save page data to JSON const dataPath = path.join(process.cwd(), 'matdash', 'data', 'pages.json') let pagesData try { const fileContent = await fs.readFile(dataPath, 'utf-8') pagesData = JSON.parse(fileContent) } catch { pagesData = { pages: [] } } // Add or update page data const existingIndex = pagesData.pages.findIndex((page: any) => page.path === pageData.path) const pageRecord = { ...pageData, id: pageData.id || `page-${Date.now()}`, fileName, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() } if (existingIndex >= 0) { pagesData.pages[existingIndex] = pageRecord } else { pagesData.pages.push(pageRecord) } await fs.writeFile(dataPath, JSON.stringify(pagesData, null, 2)) return { success: true, data: { fileName, filePath: filePath.replace(process.cwd(), ''), pageData: pageRecord } } } catch (error) { console.error('Error generating page:', error) throw createError({ statusCode: 500, statusMessage: 'Failed to generate page' }) } })