- Fall back to CODEBASE_MAP.md parsing when no apps/ dir exists - Further fallback: scan top-level dirs for deployable app signals (package.json, Dockerfile, requirements.txt, next.config.*, etc.) - Skips docs, scripts, keys, and other non-app directories - Returns isImport flag to frontend for context Made-with: Cursor
146 lines
5.5 KiB
TypeScript
146 lines
5.5 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from '@/lib/auth/authOptions';
|
|
import { query } from '@/lib/db-postgres';
|
|
|
|
const GITEA_API_URL = process.env.GITEA_API_URL ?? 'https://git.vibnai.com';
|
|
const GITEA_API_TOKEN = process.env.GITEA_API_TOKEN ?? '';
|
|
|
|
async function giteaGet(path: string) {
|
|
const res = await fetch(`${GITEA_API_URL}/api/v1${path}`, {
|
|
headers: { Authorization: `token ${GITEA_API_TOKEN}` },
|
|
next: { revalidate: 30 },
|
|
});
|
|
if (!res.ok) throw new Error(`Gitea ${res.status}: ${path}`);
|
|
return res.json();
|
|
}
|
|
|
|
/**
|
|
* GET — returns the project's apps/ directories from Gitea + saved designPackages.
|
|
* Response: { apps: [{ name, path, type }], designPackages: { appName: packageId } }
|
|
*/
|
|
export async function GET(
|
|
_req: Request,
|
|
{ params }: { params: Promise<{ projectId: string }> }
|
|
) {
|
|
const { projectId } = await params;
|
|
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user?.email) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const rows = await query<{ data: Record<string, unknown> }>(
|
|
`SELECT p.data FROM fs_projects p
|
|
JOIN fs_users u ON u.id = p.user_id
|
|
WHERE p.id = $1 AND u.data->>'email' = $2 LIMIT 1`,
|
|
[projectId, session.user.email]
|
|
);
|
|
|
|
if (rows.length === 0) {
|
|
return NextResponse.json({ error: 'Project not found' }, { status: 404 });
|
|
}
|
|
|
|
const data = rows[0].data ?? {};
|
|
const giteaRepo = data.giteaRepo as string | undefined; // e.g. "mark/sportsy"
|
|
const designPackages = (data.designPackages ?? {}) as Record<string, string>;
|
|
|
|
let apps: { name: string; path: string }[] = [];
|
|
|
|
if (giteaRepo) {
|
|
// First: try the standard turborepo apps/ directory
|
|
try {
|
|
const contents: Array<{ name: string; path: string; type: string }> =
|
|
await giteaGet(`/repos/${giteaRepo}/contents/apps`);
|
|
apps = contents
|
|
.filter((item) => item.type === 'dir')
|
|
.map(({ name, path }) => ({ name, path }));
|
|
} catch {
|
|
// No apps/ dir — fall through to import detection below
|
|
}
|
|
|
|
// Fallback for imported (non-turborepo) projects:
|
|
// Detect deployable components from top-level dirs and CODEBASE_MAP.md
|
|
if (apps.length === 0 && (data.isImport || data.creationMode === 'migration')) {
|
|
try {
|
|
// Try to read CODEBASE_MAP.md first (written by ImportAnalyzer)
|
|
const mapFile = await giteaGet(`/repos/${giteaRepo}/contents/CODEBASE_MAP.md`).catch(() => null);
|
|
if (mapFile?.content) {
|
|
const decoded = Buffer.from(mapFile.content, 'base64').toString('utf-8');
|
|
// Extract component folder paths from the map (lines like "### [name] — folder/path")
|
|
const matches = [...decoded.matchAll(/###\s+.+?—\s+([^\n(]+)/g)];
|
|
const parsedApps = matches
|
|
.map(m => m[1].trim())
|
|
.filter(p => p && !p.includes(' ') && !p.startsWith('http'))
|
|
.map(p => ({ name: p.split('/').pop() ?? p, path: p }));
|
|
if (parsedApps.length > 0) {
|
|
apps = parsedApps;
|
|
}
|
|
}
|
|
} catch { /* CODEBASE_MAP not ready yet */ }
|
|
|
|
// If still empty, scan top-level dirs and pick ones that look like apps
|
|
if (apps.length === 0) {
|
|
try {
|
|
const SKIP = new Set(['docs', 'scripts', 'keys', '.github', 'node_modules', '.git', 'dist', 'build', 'coverage']);
|
|
const APP_SIGNALS = ['package.json', 'requirements.txt', 'pyproject.toml', 'Dockerfile', 'next.config.ts', 'next.config.js', 'vite.config.ts', 'main.py', 'app.py', 'index.js', 'server.ts'];
|
|
|
|
const root: Array<{ name: string; path: string; type: string }> =
|
|
await giteaGet(`/repos/${giteaRepo}/contents/`);
|
|
|
|
const dirs = root.filter(i => i.type === 'dir' && !SKIP.has(i.name));
|
|
|
|
const candidates = await Promise.all(
|
|
dirs.map(async (dir) => {
|
|
try {
|
|
const sub: Array<{ name: string }> = await giteaGet(`/repos/${giteaRepo}/contents/${dir.path}`);
|
|
const isApp = sub.some(f => APP_SIGNALS.includes(f.name));
|
|
return isApp ? { name: dir.name, path: dir.path } : null;
|
|
} catch { return null; }
|
|
})
|
|
);
|
|
|
|
apps = candidates.filter((a): a is { name: string; path: string } => a !== null);
|
|
} catch { /* scan failed */ }
|
|
}
|
|
}
|
|
}
|
|
|
|
return NextResponse.json({ apps, designPackages, giteaRepo, isImport: !!(data.isImport || data.creationMode === 'migration') });
|
|
}
|
|
|
|
/**
|
|
* PATCH — saves { appName, packageId } → stored in fs_projects.data.designPackages
|
|
*/
|
|
export async function PATCH(
|
|
req: Request,
|
|
{ params }: { params: Promise<{ projectId: string }> }
|
|
) {
|
|
const { projectId } = await params;
|
|
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user?.email) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const { appName, packageId } = await req.json() as { appName: string; packageId: string };
|
|
|
|
if (!appName || !packageId) {
|
|
return NextResponse.json({ error: 'appName and packageId are required' }, { status: 400 });
|
|
}
|
|
|
|
await query(
|
|
`UPDATE fs_projects p
|
|
SET data = data || jsonb_build_object(
|
|
'designPackages',
|
|
COALESCE(data->'designPackages', '{}'::jsonb) || jsonb_build_object($3, $4)
|
|
),
|
|
updated_at = NOW()
|
|
FROM fs_users u
|
|
WHERE p.id = $1 AND p.user_id = u.id AND u.data->>'email' = $2`,
|
|
[projectId, session.user.email, appName, packageId]
|
|
);
|
|
|
|
return NextResponse.json({ success: true });
|
|
}
|