diff --git a/app/api/github/create-repo/route.ts b/app/api/github/create-repo/route.ts index 42ae428..8d005d1 100644 --- a/app/api/github/create-repo/route.ts +++ b/app/api/github/create-repo/route.ts @@ -15,6 +15,14 @@ export async function POST(request: NextRequest) { return proAccess.response } + const { app, repoName, scaffoldFiles } = (await request.json()) as { + app: TemplateApp + repoName: string + scaffoldFiles?: Record + } + + if (!repoName || repoName.trim().length === 0) { + return NextResponse.json({ error: 'Repository name required' }, { status: 400 }) if (!accessToken) { return NextResponse.json({ error: 'Not authenticated' }, { status: 401 }) } @@ -25,6 +33,22 @@ export async function POST(request: NextRequest) { return NextResponse.json({ error: parsedBody.error.issues[0]?.message ?? 'Invalid repository request' }, { status: 400 }) } + const newRepo = await createRepoRes.json() + + const filesToCreate = scaffoldFiles + ? normalizeScaffoldFiles(scaffoldFiles) + : generateTemplateFiles(app) + + // Create files in the new repository + for (const [fileName, content] of Object.entries(filesToCreate)) { + await createFileInRepo( + githubUsername, + repoName, + fileName, + content, + accessToken + ) + } const result = await createGitHubRepositoryFromBlueprint({ accessToken, repoName: parsedBody.data.repoName, @@ -39,6 +63,99 @@ export async function POST(request: NextRequest) { }) } catch (error) { console.error('Error creating repository:', error) + return NextResponse.json({ error: 'Failed to create repository' }, { status: 500 }) + } +} + +async function createFileInRepo( + owner: string, + repo: string, + path: string, + content: string, + accessToken: string +): Promise { + const encodedContent = Buffer.from(content).toString('base64') + + await fetch( + `https://api.github.com/repos/${owner}/${repo}/contents/${path}`, + { + method: 'PUT', + headers: { + 'Authorization': `Bearer ${accessToken}`, + 'Accept': 'application/vnd.github+json', + }, + body: JSON.stringify({ + message: `Add ${path}`, + content: encodedContent, + }), + } + ) +} + +function normalizeScaffoldFiles(files: Record): Record { + return Object.fromEntries( + Object.entries(files).map(([path, content]) => [ + path, + typeof content === 'string' ? content : JSON.stringify(content, null, 2), + ]) + ) +} + +function generateTemplateFiles(app: TemplateApp): Record { + return { + 'README.md': `# ${app.app_name} + +${app.description} + +## Technologies +${app.technologies.map((t: string) => `- ${t}`).join('\n')} + +## Getting Started + +\`\`\`bash +npm install +npm run dev +\`\`\` + +## Difficulty Level +${app.difficulty_level} + +## Notes +${app.ai_explanation} + +${app.missing_files.length > 0 ? ` +## Missing Files to Add +${app.missing_files.map((f: string) => `- [ ] ${f}`).join('\n')} +` : ''} +`, + 'package.json': JSON.stringify( + { + name: app.app_name.toLowerCase().replace(/\s+/g, '-'), + version: '1.0.0', + description: app.description, + scripts: { + dev: 'next dev', + build: 'next build', + start: 'next start', + }, + dependencies: { + react: '^18.0.0', + 'next': '^14.0.0', + }, + }, + null, + 2 + ), + '.gitignore': `node_modules/ +.env +.env.local +.env.*.local +.next/ +dist/ +build/ +*.log +.DS_Store +`, return NextResponse.json( { error: error instanceof Error ? error.message : 'Failed to create repository' }, { status: 500 }, diff --git a/app/dashboard/results/page.tsx b/app/dashboard/results/page.tsx index 6232a5b..4371069 100644 --- a/app/dashboard/results/page.tsx +++ b/app/dashboard/results/page.tsx @@ -48,8 +48,30 @@ export default function ResultsPage() { const data = await res.json() if (data.success) { - alert(`Scaffold generated for ${app.name}! Creating repository...`) - // TODO: Create GitHub repo with scaffold + const repoName = app.name.toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '') + const repoRes = await fetch('/api/github/create-repo', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + app: { + app_name: app.name, + app_type: app.category, + description: app.description, + technologies: app.technologies, + difficulty_level: app.estimatedBuildTime, + ai_explanation: '', + missing_files: app.missingFiles, + }, + repoName, + scaffoldFiles: data.scaffold?.files, + }), + }) + const repoData = await repoRes.json() + if (repoData.success) { + alert(`Repository created: ${repoData.repository.url}`) + } else { + alert(`Scaffold generated but repo creation failed: ${repoData.error}`) + } } } catch (error) { console.error('Scaffold generation failed:', error)