diff --git a/src/commands/doc/get.ts b/src/commands/doc/get.ts index f6eb1d6..37b9405 100644 --- a/src/commands/doc/get.ts +++ b/src/commands/doc/get.ts @@ -20,8 +20,18 @@ import { resolve } from 'node:path' import { installHelpHook } from '../../core/help.ts' import { log, printJson } from '../../core/log.ts' import { getSection, getRawLines, listSections } from './_parser.ts' +import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c -interface Opts { file: string; section?: string; from?: number; lines?: number; stripMermaid?: boolean; json?: boolean } +interface Opts { + file: string + section?: string + from?: number + lines?: number + stripMermaid?: boolean + json?: boolean + /** v2.1+ (2026-07-21): --verbose, addOutputOptions 注入 */ + verbose?: boolean +} function resolveFile(file: string): string { if (file.startsWith('/')) return file @@ -42,42 +52,62 @@ export function buildDocGetCommand(): Command { .option('--from ', '起始行号 (1-based, 需配合 --lines)', '1') .option('--lines ', '行数 (默认 -1 = 全文, 仅 --from/--lines 模式)', '-1') .option('--strip-mermaid', '去除 Mermaid 代码块 (默认 false)', false) - .option('--json', '原始 JSON 输出', false) + addOutputOptions(cmd) .action(async (opts: Opts) => { - const file = resolveFile(opts.file) - const stripMermaid = !!opts.stripMermaid - try { - // 模式 1: --section - if (opts.section) { - const sec = getSection(file, opts.section, stripMermaid) - if (!sec) { - const all = listSections(file) - log.error(`section 不存在: ${opts.section}`) - log.info(`可用 section (${all.length}):`) - for (const s of all) log.info(` ${s.id} [${s.lines} 行] ${s.title}`) - process.exit(1) + setupOutputFilter(opts) + + // doc 类无 appCode (本地文件操作, 跟 JNPF API 无关), wrapCommand 仍可用于统一 L1 stdout + await wrapCommand({ + command: 'doc get', + startTime: Date.now(), + verbose: opts.verbose, + json: opts.json, + fixes: [], + }, async (ctx) => { + const file = resolveFile(opts.file) + const stripMermaid = !!opts.stripMermaid + try { + // 模式 1: --section + if (opts.section) { + const sec = getSection(file, opts.section, stripMermaid) + if (!sec) { + const all = listSections(file) + log.error(`section 不存在: ${opts.section}`) + log.info(`可用 section (${all.length}):`) + for (const s of all) log.info(` ${s.id} [${s.lines} 行] ${s.title}`) + return ctx.fail(`section 不存在: ${opts.section}`, { file, section: opts.section, availableCount: all.length }) + } + if (opts.json) { + printJson({ file, section: sec.id, title: sec.title, lines: sec.lines, content: sec.text }) + return ctx.success({ file, section: sec.id }) + } + log.info(`${file} #${sec.id} (${sec.lines} 行) — ${sec.title}`) + log.raw(sec.text + '\n') + return ctx.success({ file, section: sec.id }) } - if (opts.json) { printJson({ file, section: sec.id, title: sec.title, lines: sec.lines, content: sec.text }); return } - log.info(`${file} #${sec.id} (${sec.lines} 行) — ${sec.title}`) - console.log(sec.text) - return + // 模式 2: --from/--lines 或全文 + const from = Number(opts.from) || 1 + const lines = Number(opts.lines) || -1 + const out = getRawLines(file, from, lines, stripMermaid) + if (!out) { + log.error(`起始行 ${from} 超出文件长度`) + return ctx.fail(`起始行 ${from} 超出文件长度`, { file, from }) + } + if (opts.json) { + printJson(out) + return ctx.success({ file: out.file, from: out.from, lines: out.lines }) + } + log.info(`${out.file} 第 ${out.from}-${out.from + out.lines - 1} 行 / 共 ${out.totalLines} 行${stripMermaid ? ' (Mermaid 已去除)' : ''}`) + log.raw(out.content + '\n') + ctx.success({ file: out.file, from: out.from, lines: out.lines }) + } catch (e: any) { + if (e.code === 'ENOENT') { + log.error(`文件不存在: ${file}`) + return ctx.fail(`文件不存在: ${file}`, { file }) + } + ctx.fail(`doc get 失败: ${e.message?.slice(0, 200)}`) } - // 模式 2: --from/--lines 或全文 - const from = Number(opts.from) || 1 - const lines = Number(opts.lines) || -1 - const out = getRawLines(file, from, lines, stripMermaid) - if (!out) { - log.error(`起始行 ${from} 超出文件长度`) - process.exit(1) - } - if (opts.json) { printJson(out); return } - log.info(`${out.file} 第 ${out.from}-${out.from + out.lines - 1} 行 / 共 ${out.totalLines} 行${stripMermaid ? ' (Mermaid 已去除)' : ''}`) - console.log(out.content) - } catch (e: any) { - if (e.code === 'ENOENT') { log.error(`文件不存在: ${file}`); process.exit(1) } - log.error(`doc get 失败: ${e.message}`) - process.exit(1) - } + }) }) installHelpHook(cmd, { @@ -104,4 +134,4 @@ export function buildDocGetCommand(): Command { ], }) return cmd -} +} \ No newline at end of file