|
| 1 | +import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs' |
| 2 | +import { dirname, join } from 'node:path' |
| 3 | + |
| 4 | +export const ap = (r, n) => join(r, 'shadow-docs', 'changes', n, 'brief.md') |
| 5 | +export const xp = (r, n) => join(r, 'shadow-docs', 'changes', 'archive', n, 'brief.md') |
| 6 | + |
| 7 | +export function brief(r, n, arch = false) { |
| 8 | + const path = arch ? xp(r, n) : ap(r, n) |
| 9 | + if (!existsSync(path)) throw Error('BRIEF_NOT_FOUND') |
| 10 | + const t = readFileSync(path, 'utf8'), e = t.indexOf('\n---\n', 4) |
| 11 | + if (e < 0) throw Error('BRIEF_FRONTMATTER_REQUIRED') |
| 12 | + return { path, data: JSON.parse(t.slice(4, e)), body: t.slice(e + 5) } |
| 13 | +} |
| 14 | + |
| 15 | +export function write(b) { |
| 16 | + mkdirSync(dirname(b.path), { recursive: true }) |
| 17 | + const t = b.path + `.tmp-${process.pid}` |
| 18 | + writeFileSync(t, `---\n${JSON.stringify(b.data, null, 2)}\n---\n${b.body}`) |
| 19 | + renameSync(t, b.path) |
| 20 | +} |
| 21 | + |
| 22 | +export function tasks(s) { |
| 23 | + return [...s.matchAll(/^\s*- \[([ xX])\]\s+(.+)$/gm)].map((m, i) => ({ id: `task-${i + 1}`, done: m[1].toLowerCase() === 'x', text: m[2] })) |
| 24 | +} |
0 commit comments