// Dumps a project's goals (epics) and their tasks with descriptions into Markdown. // Usage: tsx packages/db/scripts/export-epics.ts [KEY=PEP] [out=PEPPE_EPICS.md] import { writeFileSync } from "node:fs"; import { asc, eq } from "drizzle-orm"; import { createDb, goals, projects, tasks } from "../src/index.ts"; const [key = "PEP", out = "PEPPE_EPICS.md"] = process.argv.slice(2); const db = createDb(); const [project] = await db.select().from(projects).where(eq(projects.key, key)); if (!project) throw new Error(`no project ${key}`); const gs = await db.select().from(goals).where(eq(goals.projectId, project.id)).orderBy(asc(goals.id)); const ts = await db.select().from(tasks).where(eq(tasks.projectId, project.id)).orderBy(asc(tasks.number)); const STATUS: Record = { backlog: "Бэклог", proposing: "Proposal", awaiting_approval: "Ждёт одобрения", in_progress: "В работе", review: "Ревью", done: "Готово", blocked: "Заблокирована", }; function section(title: string, desc: string | null, items: typeof ts) { const done = items.filter((t) => t.status === "done").length; const lines = [`## ${title}`, "", `Задач: ${items.length}, готово: ${done}`, ""]; if (desc?.trim()) lines.push(desc.trim(), ""); for (const t of items) { lines.push(`### ${key}-${t.number}: ${t.title}`, ""); const meta = [`Статус: ${STATUS[t.status] ?? t.status}`]; if (t.openspecChange) meta.push(`OpenSpec: \`${t.openspecChange}\``); if (t.prUrl) meta.push(`PR: ${t.prUrl}`); lines.push(meta.join(" · "), ""); if (t.description?.trim()) lines.push(t.description.trim(), ""); } return lines.join("\n"); } const parts = [ `# ${project.name} (${key}) — эпики и задачи`, "", `Выгружено ${new Date().toISOString().slice(0, 16).replace("T", " ")}: целей ${gs.length}, задач ${ts.length}.`, "", ...gs.map((g) => section(`${g.title}${g.status === "open" ? "" : ` (${g.status})`}`, g.description, ts.filter((t) => t.goalId === g.id))), ]; const orphans = ts.filter((t) => !t.goalId || !gs.some((g) => g.id === t.goalId)); if (orphans.length) parts.push(section("Без цели", null, orphans)); writeFileSync(out, parts.join("\n").trimEnd() + "\n"); console.log(`${out}: ${gs.length} goals, ${ts.length} tasks`); process.exit(0);