|
| 1 | +import fs from "node:fs/promises" |
| 2 | +import path from "node:path" |
| 3 | +import crypto from "node:crypto" |
| 4 | +import * as yaml from "yaml" |
| 5 | + |
| 6 | +function sha256(text) { |
| 7 | + return crypto.createHash("sha256").update(text).digest("hex") |
| 8 | +} |
| 9 | + |
| 10 | +function parseSpecMarkdown(md) { |
| 11 | + // Extremely small “SpecKit-like” parser: extracts the 4 sections we need. |
| 12 | + // Sections are identified by headings: |
| 13 | + // - "## Intent" |
| 14 | + // - "## Scope (owned_scope)" |
| 15 | + // - "## Constraints" |
| 16 | + // - "## Acceptance Criteria" |
| 17 | + const getSection = (title) => { |
| 18 | + const re = new RegExp(`^##\\s+${title}\\s*$`, "m") |
| 19 | + const m = md.match(re) |
| 20 | + if (!m) return "" |
| 21 | + const start = m.index + m[0].length |
| 22 | + const rest = md.slice(start) |
| 23 | + const next = rest.search(/^##\s+/m) |
| 24 | + return (next === -1 ? rest : rest.slice(0, next)).trim() |
| 25 | + } |
| 26 | + |
| 27 | + const intent = getSection("Intent").trim() |
| 28 | + const scope = getSection("Scope \\(owned_scope\\)") |
| 29 | + .split("\n") |
| 30 | + .map((l) => l.trim()) |
| 31 | + .filter((l) => l.startsWith("- ")) |
| 32 | + .map((l) => l.slice(2).trim().replace(/^`|`$/g, "")) |
| 33 | + |
| 34 | + const constraints = getSection("Constraints") |
| 35 | + .split("\n") |
| 36 | + .map((l) => l.trim()) |
| 37 | + .filter((l) => l.startsWith("- ")) |
| 38 | + .map((l) => l.slice(2).trim()) |
| 39 | + |
| 40 | + const acceptance = getSection("Acceptance Criteria") |
| 41 | + .split("\n") |
| 42 | + .map((l) => l.trim()) |
| 43 | + .filter((l) => l.startsWith("- ")) |
| 44 | + .map((l) => l.slice(2).trim()) |
| 45 | + |
| 46 | + return { intent, scope, constraints, acceptance } |
| 47 | +} |
| 48 | + |
| 49 | +async function main() { |
| 50 | + const repoRoot = process.cwd() |
| 51 | + const specsDir = path.join(repoRoot, "specs") |
| 52 | + const orchestrationDir = path.join(repoRoot, ".orchestration") |
| 53 | + |
| 54 | + await fs.mkdir(specsDir, { recursive: true }) |
| 55 | + await fs.mkdir(orchestrationDir, { recursive: true }) |
| 56 | + |
| 57 | + const specFiles = (await fs.readdir(specsDir)).filter((f) => f.endsWith(".md")) |
| 58 | + if (specFiles.length === 0) { |
| 59 | + console.log("No spec files found in ./specs. Add at least one *.md spec and rerun.") |
| 60 | + process.exit(1) |
| 61 | + } |
| 62 | + |
| 63 | + const activeIntentsPath = path.join(orchestrationDir, "active_intents.yaml") |
| 64 | + const existingYaml = await fs.readFile(activeIntentsPath, "utf-8").catch(() => "active_intents: []\n") |
| 65 | + const existing = (yaml.parse(existingYaml) ?? {}) || {} |
| 66 | + const active_intents = Array.isArray(existing.active_intents) ? existing.active_intents : [] |
| 67 | + |
| 68 | + for (const file of specFiles) { |
| 69 | + const full = path.join(specsDir, file) |
| 70 | + const md = await fs.readFile(full, "utf-8") |
| 71 | + |
| 72 | + const idMatch = file.match(/^(INT-\d+)/i) |
| 73 | + const id = idMatch ? idMatch[1].toUpperCase() : `INT-${sha256(file).slice(0, 3).toUpperCase()}` |
| 74 | + const name = md.split("\n").find((l) => l.startsWith("# "))?.replace(/^#\s+/, "").trim() || file |
| 75 | + |
| 76 | + const parsed = parseSpecMarkdown(md) |
| 77 | + |
| 78 | + const intentEntry = { |
| 79 | + id, |
| 80 | + name, |
| 81 | + status: "IN_PROGRESS", |
| 82 | + owned_scope: parsed.scope, |
| 83 | + constraints: parsed.constraints, |
| 84 | + acceptance_criteria: parsed.acceptance, |
| 85 | + created_at: new Date().toISOString(), |
| 86 | + updated_at: new Date().toISOString(), |
| 87 | + spec_hash: `sha256:${sha256(md)}`, |
| 88 | + spec_file: `specs/${file}`, |
| 89 | + } |
| 90 | + |
| 91 | + const i = active_intents.findIndex((x) => x?.id === id) |
| 92 | + if (i >= 0) active_intents[i] = intentEntry |
| 93 | + else active_intents.push(intentEntry) |
| 94 | + } |
| 95 | + |
| 96 | + await fs.writeFile(activeIntentsPath, yaml.stringify({ active_intents }), "utf-8") |
| 97 | + console.log(`Updated .orchestration/active_intents.yaml with ${active_intents.length} intent(s).`) |
| 98 | +} |
| 99 | + |
| 100 | +main().catch((err) => { |
| 101 | + console.error(err) |
| 102 | + process.exit(1) |
| 103 | +}) |
| 104 | + |
| 105 | + |
0 commit comments