|
|
import { NextRequest, NextResponse } from 'next/server' |
|
|
import { templates } from '@/components/template-data' |
|
|
|
|
|
export async function POST(request: NextRequest) { |
|
|
try { |
|
|
const { templateId, repoName, description, isPrivate } = await request.json() |
|
|
|
|
|
const template = templates.find(t => t.id === templateId) |
|
|
|
|
|
if (!template) { |
|
|
return NextResponse.json( |
|
|
{ error: 'Template not found' }, |
|
|
{ status: 404 } |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
const githubToken = request.headers.get('X-GitHub-Token') || process.env.GITHUB_TOKEN |
|
|
|
|
|
if (!githubToken) { |
|
|
return NextResponse.json( |
|
|
{ error: 'GitHub authentication required' }, |
|
|
{ status: 401 } |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
const createRepoResponse = await fetch('https://api.github.com/user/repos', { |
|
|
method: 'POST', |
|
|
headers: { |
|
|
'Authorization': `Bearer ${githubToken}`, |
|
|
'Accept': 'application/vnd.github.v3+json', |
|
|
'Content-Type': 'application/json' |
|
|
}, |
|
|
body: JSON.stringify({ |
|
|
name: repoName || `${template.id}-project`, |
|
|
description: description || template.description, |
|
|
private: isPrivate || false, |
|
|
auto_init: true, |
|
|
has_issues: true, |
|
|
has_projects: false, |
|
|
has_wiki: false |
|
|
}) |
|
|
}) |
|
|
|
|
|
if (!createRepoResponse.ok) { |
|
|
const error = await createRepoResponse.json() |
|
|
return NextResponse.json( |
|
|
{ error: error.message || 'Failed to create repository' }, |
|
|
{ status: createRepoResponse.status } |
|
|
) |
|
|
} |
|
|
|
|
|
const repo = await createRepoResponse.json() |
|
|
|
|
|
|
|
|
if (template.githubUrl) { |
|
|
|
|
|
const readmeContent = `# ${repoName || template.name} |
|
|
|
|
|
${description || template.description} |
|
|
|
|
|
## Getting Started |
|
|
|
|
|
This project was created from the ${template.name} template. |
|
|
|
|
|
### Quick Setup |
|
|
|
|
|
\`\`\`bash |
|
|
# Clone this repository |
|
|
git clone ${repo.clone_url} |
|
|
|
|
|
# Navigate to the project |
|
|
cd ${repo.name} |
|
|
|
|
|
# Install dependencies |
|
|
npm install |
|
|
|
|
|
# Start development server |
|
|
npm run dev |
|
|
\`\`\` |
|
|
|
|
|
### Template Information |
|
|
|
|
|
- **Template**: ${template.name} |
|
|
- **Category**: ${template.category} |
|
|
- **Technologies**: ${template.tech.join(', ')} |
|
|
|
|
|
### Features |
|
|
|
|
|
${template.features.map(f => `- ${f}`).join('\n')} |
|
|
|
|
|
### Deploy |
|
|
|
|
|
${template.deployUrls?.vercel ? `[](${template.deployUrls.vercel})` : ''} |
|
|
${template.deployUrls?.netlify ? `[](${template.deployUrls.netlify})` : ''} |
|
|
${template.deployUrls?.railway ? `[](${template.deployUrls.railway})` : ''} |
|
|
|
|
|
## License |
|
|
|
|
|
MIT |
|
|
|
|
|
--- |
|
|
|
|
|
Created with [Hanzo AI Templates](https://hanzo.ai/templates) |
|
|
` |
|
|
|
|
|
|
|
|
const createFileResponse = await fetch( |
|
|
`https://api.github.com/repos/${repo.owner.login}/${repo.name}/contents/README.md`, |
|
|
{ |
|
|
method: 'PUT', |
|
|
headers: { |
|
|
'Authorization': `Bearer ${githubToken}`, |
|
|
'Accept': 'application/vnd.github.v3+json', |
|
|
'Content-Type': 'application/json' |
|
|
}, |
|
|
body: JSON.stringify({ |
|
|
message: 'Initial commit with template information', |
|
|
content: Buffer.from(readmeContent).toString('base64') |
|
|
}) |
|
|
} |
|
|
) |
|
|
|
|
|
if (!createFileResponse.ok) { |
|
|
console.error('Failed to create README:', await createFileResponse.text()) |
|
|
} |
|
|
} |
|
|
|
|
|
return NextResponse.json({ |
|
|
success: true, |
|
|
repository: { |
|
|
name: repo.name, |
|
|
url: repo.html_url, |
|
|
cloneUrl: repo.clone_url, |
|
|
description: repo.description |
|
|
}, |
|
|
message: 'Repository created successfully', |
|
|
nextSteps: [ |
|
|
`Clone your repository: git clone ${repo.clone_url}`, |
|
|
'Copy template files to your repository', |
|
|
'Commit and push your changes' |
|
|
] |
|
|
}) |
|
|
} catch (error) { |
|
|
console.error('Error creating GitHub repository:', error) |
|
|
return NextResponse.json( |
|
|
{ error: 'Failed to create GitHub repository' }, |
|
|
{ status: 500 } |
|
|
) |
|
|
} |
|
|
} |