Spaces:
Running
Running
| import { NextRequest, NextResponse } from 'next/server' | |
| import fs from 'fs' | |
| import path from 'path' | |
| const DATA_DIR = process.env.NODE_ENV === 'production' && fs.existsSync('/data') | |
| ? '/data' | |
| : path.join(process.cwd(), 'data') | |
| const FLUTTER_APPS_DIR = path.join(DATA_DIR, 'documents', 'flutter_apps') | |
| export async function PUT(request: NextRequest) { | |
| try { | |
| const body = await request.json() | |
| const { name, dartCode, dependencies, pubspecYaml } = body | |
| if (!name) { | |
| return NextResponse.json( | |
| { success: false, error: 'App name is required' }, | |
| { status: 400 } | |
| ) | |
| } | |
| // Sanitize name | |
| const safeName = name.replace(/[^a-zA-Z0-9_-]/g, '_').toLowerCase() | |
| const fileName = `${safeName}.flutter.json` | |
| const filePath = path.join(FLUTTER_APPS_DIR, fileName) | |
| if (!fs.existsSync(filePath)) { | |
| return NextResponse.json( | |
| { success: false, error: `Flutter app '${safeName}' not found` }, | |
| { status: 404 } | |
| ) | |
| } | |
| // Read existing app | |
| const fileContent = fs.readFileSync(filePath, 'utf-8') | |
| const appData = JSON.parse(fileContent) | |
| // Update fields if provided | |
| if (dartCode !== undefined) { | |
| appData.dartCode = dartCode | |
| } | |
| if (dependencies !== undefined) { | |
| appData.dependencies = dependencies | |
| } | |
| if (pubspecYaml !== undefined) { | |
| appData.pubspecYaml = pubspecYaml | |
| } | |
| // Update metadata | |
| appData.metadata.modified = new Date().toISOString() | |
| // Save updated app | |
| fs.writeFileSync(filePath, JSON.stringify(appData, null, 2), 'utf-8') | |
| return NextResponse.json({ | |
| success: true, | |
| appName: safeName, | |
| message: 'Flutter app updated successfully' | |
| }) | |
| } catch (error) { | |
| console.error('Error updating Flutter app:', error) | |
| return NextResponse.json( | |
| { success: false, error: 'Failed to update Flutter app' }, | |
| { status: 500 } | |
| ) | |
| } | |
| } | |