|
| 1 | +import { promises as fs } from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import { getTaskProgressForChange, type TaskProgress } from '../../utils/task-progress.js'; |
| 4 | +import { MarkdownParser } from '../parsers/markdown-parser.js'; |
| 5 | +import { renderMarkdown } from './markdown.js'; |
| 6 | + |
| 7 | +export interface ChangeArtifacts { |
| 8 | + proposal: boolean; |
| 9 | + specs: boolean; |
| 10 | + design: boolean; |
| 11 | + tasks: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +export interface ChangeEntry { |
| 15 | + name: string; |
| 16 | + status: 'draft' | 'active' | 'completed'; |
| 17 | + artifacts: ChangeArtifacts; |
| 18 | + progress: TaskProgress; |
| 19 | +} |
| 20 | + |
| 21 | +export interface SpecEntry { |
| 22 | + name: string; |
| 23 | + requirementCount: number; |
| 24 | +} |
| 25 | + |
| 26 | +export interface SpecGroup { |
| 27 | + domain: string; |
| 28 | + specs: SpecEntry[]; |
| 29 | +} |
| 30 | + |
| 31 | +export interface ArchiveEntry { |
| 32 | + name: string; |
| 33 | + date: string; |
| 34 | + changeName: string; |
| 35 | +} |
| 36 | + |
| 37 | +export interface DashboardSummary { |
| 38 | + changes: { draft: number; active: number; completed: number; total: number }; |
| 39 | + specs: { total: number; totalRequirements: number }; |
| 40 | + archive: { total: number }; |
| 41 | +} |
| 42 | + |
| 43 | +async function dirExists(dirPath: string): Promise<boolean> { |
| 44 | + try { |
| 45 | + const stat = await fs.stat(dirPath); |
| 46 | + return stat.isDirectory(); |
| 47 | + } catch { |
| 48 | + return false; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +async function fileExists(filePath: string): Promise<boolean> { |
| 53 | + try { |
| 54 | + await fs.access(filePath); |
| 55 | + return true; |
| 56 | + } catch { |
| 57 | + return false; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +async function getArtifacts(changeDir: string): Promise<ChangeArtifacts> { |
| 62 | + const [proposal, specs, design, tasks] = await Promise.all([ |
| 63 | + fileExists(path.join(changeDir, 'proposal.md')), |
| 64 | + dirExists(path.join(changeDir, 'specs')), |
| 65 | + fileExists(path.join(changeDir, 'design.md')), |
| 66 | + fileExists(path.join(changeDir, 'tasks.md')), |
| 67 | + ]); |
| 68 | + return { proposal, specs, design, tasks }; |
| 69 | +} |
| 70 | + |
| 71 | +export async function getChangesData(openspecDir: string): Promise<ChangeEntry[]> { |
| 72 | + const changesDir = path.join(openspecDir, 'changes'); |
| 73 | + if (!(await dirExists(changesDir))) { |
| 74 | + return []; |
| 75 | + } |
| 76 | + |
| 77 | + const entries = await fs.readdir(changesDir, { withFileTypes: true }); |
| 78 | + const changes: ChangeEntry[] = []; |
| 79 | + |
| 80 | + for (const entry of entries) { |
| 81 | + if (!entry.isDirectory() || entry.name === 'archive' || entry.name.startsWith('.')) continue; |
| 82 | + |
| 83 | + const changeDir = path.join(changesDir, entry.name); |
| 84 | + const [progress, artifacts] = await Promise.all([ |
| 85 | + getTaskProgressForChange(changesDir, entry.name), |
| 86 | + getArtifacts(changeDir), |
| 87 | + ]); |
| 88 | + |
| 89 | + let status: ChangeEntry['status']; |
| 90 | + if (progress.total === 0) { |
| 91 | + status = 'draft'; |
| 92 | + } else if (progress.completed === progress.total) { |
| 93 | + status = 'completed'; |
| 94 | + } else { |
| 95 | + status = 'active'; |
| 96 | + } |
| 97 | + |
| 98 | + changes.push({ name: entry.name, status, artifacts, progress }); |
| 99 | + } |
| 100 | + |
| 101 | + changes.sort((a, b) => a.name.localeCompare(b.name)); |
| 102 | + return changes; |
| 103 | +} |
| 104 | + |
| 105 | +export async function getSpecsData(openspecDir: string): Promise<SpecGroup[]> { |
| 106 | + const specsDir = path.join(openspecDir, 'specs'); |
| 107 | + if (!(await dirExists(specsDir))) { |
| 108 | + return []; |
| 109 | + } |
| 110 | + |
| 111 | + const entries = await fs.readdir(specsDir, { withFileTypes: true }); |
| 112 | + const specs: SpecEntry[] = []; |
| 113 | + |
| 114 | + for (const entry of entries) { |
| 115 | + if (!entry.isDirectory() || entry.name.startsWith('.')) continue; |
| 116 | + |
| 117 | + const specFile = path.join(specsDir, entry.name, 'spec.md'); |
| 118 | + if (!(await fileExists(specFile))) continue; |
| 119 | + |
| 120 | + let requirementCount = 0; |
| 121 | + try { |
| 122 | + const content = await fs.readFile(specFile, 'utf-8'); |
| 123 | + const parser = new MarkdownParser(content); |
| 124 | + const spec = parser.parseSpec(entry.name); |
| 125 | + requirementCount = spec.requirements.length; |
| 126 | + } catch { |
| 127 | + // If spec can't be parsed, include with 0 count |
| 128 | + } |
| 129 | + |
| 130 | + specs.push({ name: entry.name, requirementCount }); |
| 131 | + } |
| 132 | + |
| 133 | + specs.sort((a, b) => a.name.localeCompare(b.name)); |
| 134 | + |
| 135 | + // Group by domain prefix (text before first hyphen) |
| 136 | + const groupMap = new Map<string, SpecEntry[]>(); |
| 137 | + for (const spec of specs) { |
| 138 | + const hyphenIdx = spec.name.indexOf('-'); |
| 139 | + const domain = hyphenIdx > 0 ? spec.name.substring(0, hyphenIdx) : spec.name; |
| 140 | + const group = groupMap.get(domain); |
| 141 | + if (group) { |
| 142 | + group.push(spec); |
| 143 | + } else { |
| 144 | + groupMap.set(domain, [spec]); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + const groups: SpecGroup[] = []; |
| 149 | + for (const [domain, domainSpecs] of groupMap) { |
| 150 | + groups.push({ domain, specs: domainSpecs }); |
| 151 | + } |
| 152 | + groups.sort((a, b) => a.domain.localeCompare(b.domain)); |
| 153 | + |
| 154 | + return groups; |
| 155 | +} |
| 156 | + |
| 157 | +export async function getArchiveData( |
| 158 | + openspecDir: string, |
| 159 | + limit = 50, |
| 160 | + offset = 0 |
| 161 | +): Promise<{ entries: ArchiveEntry[]; total: number }> { |
| 162 | + const archiveDir = path.join(openspecDir, 'changes', 'archive'); |
| 163 | + if (!(await dirExists(archiveDir))) { |
| 164 | + return { entries: [], total: 0 }; |
| 165 | + } |
| 166 | + |
| 167 | + const dirEntries = await fs.readdir(archiveDir, { withFileTypes: true }); |
| 168 | + const allEntries: ArchiveEntry[] = []; |
| 169 | + |
| 170 | + for (const entry of dirEntries) { |
| 171 | + if (!entry.isDirectory() || entry.name.startsWith('.')) continue; |
| 172 | + |
| 173 | + // Parse date from YYYY-MM-DD-<name> format |
| 174 | + const dateMatch = entry.name.match(/^(\d{4}-\d{2}-\d{2})-(.+)$/); |
| 175 | + if (dateMatch) { |
| 176 | + allEntries.push({ |
| 177 | + name: entry.name, |
| 178 | + date: dateMatch[1], |
| 179 | + changeName: dateMatch[2], |
| 180 | + }); |
| 181 | + } else { |
| 182 | + allEntries.push({ |
| 183 | + name: entry.name, |
| 184 | + date: '', |
| 185 | + changeName: entry.name, |
| 186 | + }); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + // Sort reverse chronologically (most recent first) |
| 191 | + allEntries.sort((a, b) => b.name.localeCompare(a.name)); |
| 192 | + |
| 193 | + const total = allEntries.length; |
| 194 | + const entries = allEntries.slice(offset, offset + limit); |
| 195 | + |
| 196 | + return { entries, total }; |
| 197 | +} |
| 198 | + |
| 199 | +export async function getSummary(openspecDir: string): Promise<DashboardSummary> { |
| 200 | + const changes = await getChangesData(openspecDir); |
| 201 | + const specGroups = await getSpecsData(openspecDir); |
| 202 | + const archive = await getArchiveData(openspecDir, 1, 0); |
| 203 | + |
| 204 | + const draft = changes.filter((c) => c.status === 'draft').length; |
| 205 | + const active = changes.filter((c) => c.status === 'active').length; |
| 206 | + const completed = changes.filter((c) => c.status === 'completed').length; |
| 207 | + |
| 208 | + let totalSpecs = 0; |
| 209 | + let totalRequirements = 0; |
| 210 | + for (const group of specGroups) { |
| 211 | + totalSpecs += group.specs.length; |
| 212 | + totalRequirements += group.specs.reduce((sum, s) => sum + s.requirementCount, 0); |
| 213 | + } |
| 214 | + |
| 215 | + return { |
| 216 | + changes: { draft, active, completed, total: draft + active + completed }, |
| 217 | + specs: { total: totalSpecs, totalRequirements }, |
| 218 | + archive: { total: archive.total }, |
| 219 | + }; |
| 220 | +} |
| 221 | + |
| 222 | +export async function getArtifactContent( |
| 223 | + openspecDir: string, |
| 224 | + relativePath: string |
| 225 | +): Promise<{ html: string } | { error: string; status: number }> { |
| 226 | + // Resolve and verify path stays within openspec directory |
| 227 | + const resolvedOpenspec = path.resolve(openspecDir); |
| 228 | + const resolvedPath = path.resolve(openspecDir, relativePath); |
| 229 | + |
| 230 | + if (!resolvedPath.startsWith(resolvedOpenspec + path.sep) && resolvedPath !== resolvedOpenspec) { |
| 231 | + return { error: 'Forbidden: path outside openspec directory', status: 403 }; |
| 232 | + } |
| 233 | + |
| 234 | + // Only allow .md and .yaml files |
| 235 | + const ext = path.extname(resolvedPath).toLowerCase(); |
| 236 | + if (ext !== '.md' && ext !== '.yaml' && ext !== '.yml') { |
| 237 | + return { error: 'Forbidden: only .md and .yaml files are allowed', status: 403 }; |
| 238 | + } |
| 239 | + |
| 240 | + try { |
| 241 | + const content = await fs.readFile(resolvedPath, 'utf-8'); |
| 242 | + if (ext === '.md') { |
| 243 | + return { html: renderMarkdown(content) }; |
| 244 | + } |
| 245 | + // YAML files: render as code block |
| 246 | + return { html: `<pre><code>${content.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')}</code></pre>` }; |
| 247 | + } catch { |
| 248 | + return { error: 'File not found', status: 404 }; |
| 249 | + } |
| 250 | +} |
0 commit comments