/** * Finds and parses a repo's TODO file. Pure apart from the file-system lookups, so it's testable. */ import { closeSync, existsSync, openSync, readSync, statSync } from "node:fs"; import { dirname, join } from "node:path"; /** Most bytes read from a TODO file; a longer file is read up to here and the rest is ignored. */ export const MAX_TODO_BYTES = 256 * 1024; /** Most items parsed from a TODO file. */ export const MAX_TODO_ITEMS = 500; /** File names checked at the repo root, in order. */ export const TODO_FILES = ["TODO.md", "TODO", "TODO.txt", "todo.md", "TODO.markdown"]; /** The nearest directory at or above `cwd` that contains `.git`, else `cwd`. */ export function repoRoot(cwd: string): string { let dir = cwd; for (;;) { if (existsSync(join(dir, ".git"))) return dir; const up = dirname(dir); if (up === dir) return cwd; dir = up; } } /** Path of the repo's TODO file, or undefined. */ export function findTodoFile(cwd: string): string | undefined { const root = repoRoot(cwd); for (const name of TODO_FILES) { const p = join(root, name); try { if (statSync(p).isFile()) return p; } catch { // not there } } return undefined; } /** * Open top-level items: markdown list entries ("- x", "* x", "1. x", "- [ ] x") at the file's * shallowest list indent. Checked boxes ("- [x]") and nested sub-items are left out, as are code * blocks. A file with no list items falls back to its non-heading, non-blank lines. At most * `maxItems` list entries (or plain lines) are looked at; the rest of the text is ignored. */ export function parseTodos(content: string, maxItems = MAX_TODO_ITEMS): string[] { const items: { indent: number; text: string }[] = []; const plain: string[] = []; let fence = false; for (const raw of content.split(/\r?\n/)) { if (/^\s*(```|~~~)/.test(raw)) { fence = !fence; continue; } if (fence) continue; if (items.length >= maxItems || plain.length >= maxItems) break; const m = raw.match(/^(\s*)(?:[-*+]|\d+[.)])\s+(.*)$/); if (m) { const indent = m[1]!.replace(/\t/g, " ").length; let text = m[2]!.trim(); const box = text.match(/^\[([ xX~-])\]\s*(.*)$/); if (box) { if (box[1] !== " ") { items.push({ indent, text: "" }); // done: keeps its subitems out, then dropped continue; } text = box[2]!.trim(); } items.push({ indent, text }); } else if (raw.trim() && !/^\s*(#|