|
| 1 | +/** |
| 2 | + * `zn comment ...` (#738): list, add, reply to and resolve the comments on a |
| 3 | + * note, the same operations the MCP tools expose, so a script or an agent |
| 4 | + * without MCP can join a review thread. |
| 5 | + */ |
| 6 | + |
| 7 | +import type { VaultBackend } from '../backend.js' |
| 8 | +import { getBool, getString, type ParsedArgs } from '../args.js' |
| 9 | +import { emitJson, emitLine, emitOk } from '../format.js' |
| 10 | +import { |
| 11 | + addComment, |
| 12 | + listCommentThreads, |
| 13 | + replyToComment, |
| 14 | + resolveComment, |
| 15 | + type CommentThreadView |
| 16 | +} from '../../mcp/comment-ops.js' |
| 17 | + |
| 18 | +function requirePath(args: ParsedArgs, usage: string): string { |
| 19 | + const rel = getString(args, 'path') ?? args.positionals[0] |
| 20 | + if (!rel) throw new Error(`Usage: ${usage}`) |
| 21 | + return rel |
| 22 | +} |
| 23 | + |
| 24 | +function requireBody(args: ParsedArgs, positionalIndex: number, usage: string): string { |
| 25 | + const body = getString(args, 'body') ?? args.positionals[positionalIndex] |
| 26 | + if (!body || !body.trim()) throw new Error(`Usage: ${usage}`) |
| 27 | + return body |
| 28 | +} |
| 29 | + |
| 30 | +function when(ms: number): string { |
| 31 | + return new Date(ms).toISOString().replace('T', ' ').slice(0, 16) |
| 32 | +} |
| 33 | + |
| 34 | +function printThread(thread: CommentThreadView): void { |
| 35 | + const who = thread.author ?? 'You' |
| 36 | + const state = thread.resolved ? ' (resolved)' : '' |
| 37 | + emitLine(`${thread.id} ${who} ${when(thread.createdAt)} line ${thread.line}${state}`) |
| 38 | + if (thread.anchorText) emitLine(` > ${thread.anchorText}`) |
| 39 | + emitLine(` ${thread.body.replace(/\n/g, '\n ')}`) |
| 40 | + for (const reply of thread.replies) { |
| 41 | + emitLine(` ${reply.id} ${reply.author ?? 'You'} ${when(reply.createdAt)}`) |
| 42 | + emitLine(` ${reply.body.replace(/\n/g, '\n ')}`) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +export async function cmdCommentList(vault: VaultBackend, args: ParsedArgs): Promise<void> { |
| 47 | + const rel = requirePath(args, 'zn comment list <path> [--all] [--json]') |
| 48 | + const threads = await listCommentThreads(vault, rel, { includeResolved: getBool(args, 'all') }) |
| 49 | + if (getBool(args, 'json')) { |
| 50 | + emitJson(threads) |
| 51 | + return |
| 52 | + } |
| 53 | + if (threads.length === 0) { |
| 54 | + emitLine(getBool(args, 'all') ? 'No comments.' : 'No open comments. Pass --all to include resolved ones.') |
| 55 | + return |
| 56 | + } |
| 57 | + threads.forEach((thread, index) => { |
| 58 | + if (index > 0) emitLine('') |
| 59 | + printThread(thread) |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +export async function cmdCommentAdd(vault: VaultBackend, args: ParsedArgs): Promise<void> { |
| 64 | + const usage = 'zn comment add <path> "<body>" [--anchor "<text from the note>"] [--author <name>]' |
| 65 | + const rel = requirePath(args, usage) |
| 66 | + const body = requireBody(args, 1, usage) |
| 67 | + const thread = await addComment(vault, { |
| 68 | + path: rel, |
| 69 | + body, |
| 70 | + anchorText: getString(args, 'anchor'), |
| 71 | + author: getString(args, 'author') |
| 72 | + }) |
| 73 | + if (getBool(args, 'json')) { |
| 74 | + emitJson(thread) |
| 75 | + return |
| 76 | + } |
| 77 | + emitOk(`Commented on ${rel} (${thread.id}${thread.anchorText ? `, line ${thread.line}` : ''})`) |
| 78 | +} |
| 79 | + |
| 80 | +export async function cmdCommentReply(vault: VaultBackend, args: ParsedArgs): Promise<void> { |
| 81 | + const usage = 'zn comment reply <path> <id> "<body>" [--author <name>]' |
| 82 | + const rel = requirePath(args, usage) |
| 83 | + const id = getString(args, 'id') ?? args.positionals[1] |
| 84 | + if (!id) throw new Error(`Usage: ${usage}`) |
| 85 | + const body = requireBody(args, 2, usage) |
| 86 | + const thread = await replyToComment(vault, { path: rel, id, body, author: getString(args, 'author') }) |
| 87 | + if (getBool(args, 'json')) { |
| 88 | + emitJson(thread) |
| 89 | + return |
| 90 | + } |
| 91 | + emitOk(`Replied in ${thread.id} on ${rel} (${thread.replies.length} ${thread.replies.length === 1 ? 'reply' : 'replies'})`) |
| 92 | +} |
| 93 | + |
| 94 | +export async function cmdCommentResolve(vault: VaultBackend, args: ParsedArgs): Promise<void> { |
| 95 | + const usage = 'zn comment resolve <path> <id> [--reopen]' |
| 96 | + const rel = requirePath(args, usage) |
| 97 | + const id = getString(args, 'id') ?? args.positionals[1] |
| 98 | + if (!id) throw new Error(`Usage: ${usage}`) |
| 99 | + const reopen = getBool(args, 'reopen') |
| 100 | + const thread = await resolveComment(vault, { path: rel, id, resolved: !reopen }) |
| 101 | + if (getBool(args, 'json')) { |
| 102 | + emitJson(thread) |
| 103 | + return |
| 104 | + } |
| 105 | + emitOk(`${reopen ? 'Reopened' : 'Resolved'} ${thread.id} on ${rel}`) |
| 106 | +} |
0 commit comments