File size: 2,649 Bytes
33d698c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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')

// Ensure directory exists
if (!fs.existsSync(FLUTTER_APPS_DIR)) {
  fs.mkdirSync(FLUTTER_APPS_DIR, { recursive: true })
}

export async function POST(request: NextRequest) {
  try {
    const body = await request.json()
    const { name, dartCode, dependencies = [], pubspecYaml } = body

    if (!name || !dartCode) {
      return NextResponse.json(
        { success: false, error: 'Name and dartCode are required' },
        { status: 400 }
      )
    }

    // Sanitize name
    const safeName = name.replace(/[^a-zA-Z0-9_-]/g, '_').toLowerCase()
    if (!safeName) {
      return NextResponse.json(
        { success: false, error: 'Invalid app name' },
        { status: 400 }
      )
    }

    // Generate default pubspec.yaml if not provided
    let finalPubspecYaml = pubspecYaml
    if (!finalPubspecYaml) {
      const depLines = dependencies.map((dep: string) => `  ${dep}`).join('\n')
      finalPubspecYaml = `name: ${safeName}
description: A Flutter application created via Reuben OS
version: 1.0.0

environment:
  sdk: '>=3.0.0 <4.0.0'

dependencies:
  flutter:
    sdk: flutter
${depLines}

flutter:
  uses-material-design: true
`
    }

    // Create Flutter app object
    const flutterApp = {
      type: 'flutter_app',
      name: safeName,
      dartCode,
      dependencies,
      pubspecYaml: finalPubspecYaml,
      metadata: {
        created: new Date().toISOString(),
        modified: new Date().toISOString(),
        author: 'claude'
      }
    }

    // Save to file
    const fileName = `${safeName}.flutter.json`
    const filePath = path.join(FLUTTER_APPS_DIR, fileName)

    // Check if file exists
    if (fs.existsSync(filePath)) {
      return NextResponse.json(
        { success: false, error: `Flutter app '${safeName}' already exists. Use update instead.` },
        { status: 400 }
      )
    }

    fs.writeFileSync(filePath, JSON.stringify(flutterApp, null, 2), 'utf-8')

    return NextResponse.json({
      success: true,
      appName: safeName,
      fileName,
      filePath: `flutter_apps/${fileName}`,
      dependencies,
      message: 'Flutter app created successfully'
    })
  } catch (error) {
    console.error('Error creating Flutter app:', error)
    return NextResponse.json(
      { success: false, error: 'Failed to create Flutter app' },
      { status: 500 }
    )
  }
}