feat(dacli): P4 v3 batch 2 — apis/{search,tags} 完整 wrapCommand 迁移 + printRawTable

- apis/search.ts: 双表 (Schema + API) → printRawTable. Schema 4 列, API 4 列.
  maxColWidth=80 容纳长 path. 整块 .action 套 wrapCommand (ctx.success/fail).
- apis/tags.ts: 单表 (NAME/API_COUNT/DESCRIPTION) → printRawTable. 移除
  本地 --json (改 addOutputOptions 统一注入 --verbose --json).

每文件:
  - import { setupOutputFilter, addOutputOptions, wrapCommand }
  - 删本地 --json 选项, 加 addOutputOptions(cmd)
  - Opts interface 加 verbose?: boolean
  - .action 开头 setupOutputFilter(opts)
  - 整块业务逻辑 wrapCommand 包 + try/catch → ctx.success/fail
  - summary 对象 (供 L3 CommandLog 落盘)
  - 原 log.warn 后 return → ctx.success(summary) (L3 落 success 状态)
  - console.log table → log.printRawTable({ maxColWidth: 80 })

验证:
  - TS compile 0 新错误 (baseline 57 不变)
  - 73/73 单元测试 PASS

剩 20 REAL_TABLE 文件. 下批: app/list + bigscreen/list + portal/list (3 普通表).
This commit is contained in:
ssw committed 2026-07-21 18:40:02 +08:00
1 parent 15b3361f15
commit a772647d64
2 files changed
+125 -63

No files matched your search

+68 -32
View File
@@ -13,12 +13,15 @@ import { JnpfClient } from '../../core/client.ts'
import { installHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { loadOpenApi, type OpenApiDoc } from './_cache.ts'
import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c
interface Opts {
field: string
json?: boolean
limit?: string
exact?: boolean
/** v2.1+ (2026-07-21): --verbose, addOutputOptions 注入 */
verbose?: boolean
}
interface SchemaRef {
@@ -92,43 +95,76 @@ export function buildApisSearchCommand(): Command {
.requiredOption('--field <f>', '字段名 (如 f_user_name, 默认模糊匹配)')
.option('--exact', '精确匹配字段名', false)
.option('--limit <n>', 'API 列表最大条数 (默认 50)', '50')
.option('--json', '原始 JSON 输出', false)
addOutputOptions(cmd)
.action(async (opts: Opts) => {
setupOutputFilter(opts)
const c = await JnpfClient.fromToken()
const openapi = await loadOpenApi(c)
const schemas = searchInSchemas(openapi, opts.field, opts.exact || false)
const schemaNames = new Set(schemas.map((s) => s.name))
const apis = searchInPaths(openapi, schemaNames).slice(0, parseInt(opts.limit || '50', 10))
const result: SearchResult = {
field: opts.field,
schemas: [...new Map(schemas.map((s) => [`${s.name}.${s.type}`, s])).values()],
apis,
}
await wrapCommand({
command: 'apis search',
appCode: c.appCode,
startTime: Date.now(),
verbose: opts.verbose,
json: opts.json,
fixes: [],
}, async (ctx) => {
try {
const openapi = await loadOpenApi(c)
const schemas = searchInSchemas(openapi, opts.field, opts.exact || false)
const schemaNames = new Set(schemas.map((s) => s.name))
const apis = searchInPaths(openapi, schemaNames).slice(0, parseInt(opts.limit || '50', 10))
if (opts.json) {
printJson(result)
return
}
const result: SearchResult = {
field: opts.field,
schemas: [...new Map(schemas.map((s) => [`${s.name}.${s.type}`, s])).values()],
apis,
}
const summary = {
field: opts.field,
schemaMatches: result.schemas.length,
apiMatches: result.apis.length,
exact: !!opts.exact,
limit: Number(opts.limit || '50'),
}
log.info(`字段 "${opts.field}" 匹配 ${result.schemas.length} 个 schema, ${result.apis.length} 个 API`)
if (result.schemas.length === 0) {
log.warn('无 schema 匹配. 提示: 字段名通常是 f_xxx 形式 (snake_case), 可省略前缀试试')
return
}
console.log('\n# 包含该字段的 Schema:')
console.log('SCHEMA'.padEnd(40) + 'TYPE'.padEnd(20) + 'REQUIRED DESCRIPTION')
console.log('-'.repeat(95))
for (const s of result.schemas) {
console.log(`${s.name.padEnd(40)}${(s.type || '-').padEnd(20)}${s.required ? 'YES' : 'no'.padEnd(9)} ${(s.description || '').slice(0, 40)}`)
}
console.log('\n# 引用这些 Schema 的 API:')
console.log('METHOD'.padEnd(8) + 'PATH'.padEnd(60) + 'DIRECTION SUMMARY')
console.log('-'.repeat(115))
for (const a of result.apis) {
console.log(`${a.method.padEnd(8)}${a.path.padEnd(60)}${a.direction.padEnd(10)} ${(a.summary || '').slice(0, 40)}`)
}
log.info(`\n💡 用法: dacli apis info "${result.apis[0]?.method} ${result.apis[0]?.path}" (看 schema 详情)`)
if (opts.json) {
printJson(result)
return ctx.success(summary)
}
log.info(`字段 "${opts.field}" 匹配 ${result.schemas.length} 个 schema, ${result.apis.length} 个 API`)
if (result.schemas.length === 0) {
log.warn('无 schema 匹配. 提示: 字段名通常是 f_xxx 形式 (snake_case), 可省略前缀试试')
return ctx.success(summary)
}
log.raw('\n# 包含该字段的 Schema:\n')
log.printRawTable(
['SCHEMA', 'TYPE', 'REQUIRED', 'DESCRIPTION'],
result.schemas.map((s) => [
s.name,
String(s.type || '-'),
s.required ? 'YES' : 'no',
String(s.description || '').slice(0, 60),
]),
{ maxColWidth: 80 },
)
log.raw('\n# 引用这些 Schema 的 API:\n')
log.printRawTable(
['METHOD', 'PATH', 'DIRECTION', 'SUMMARY'],
result.apis.map((a) => [
a.method,
a.path,
a.direction,
(a.summary || '').slice(0, 60),
]),
{ maxColWidth: 80 },
)
log.info(`\n💡 用法: dacli apis info "${result.apis[0]?.method} ${result.apis[0]?.path}" (看 schema 详情)`)
ctx.success(summary)
} catch (e) {
ctx.fail(`apis search 失败: ${(e as Error).message?.slice(0, 200)}`)
}
})
})
installHelpHook(cmd, {
+57 -31
View File
@@ -10,46 +10,72 @@ import { JnpfClient } from '../../core/client.ts'
import { installHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { loadOpenApi, type OpenApiDoc } from './_cache.ts'
import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c
interface Opts { json?: boolean }
interface Opts {
json?: boolean
/** v2.1+ (2026-07-21): --verbose, addOutputOptions 注入 */
verbose?: boolean
}
export function buildApisTagsCommand(): Command {
const cmd = new Command('tags')
.description('列 OpenAPI 所有 tag (P1 AI 引导, 不知道有哪些业务 tag 时用)')
.option('--json', '原始 JSON 输出', false)
addOutputOptions(cmd)
.action(async (opts: Opts) => {
setupOutputFilter(opts)
const c = await JnpfClient.fromToken()
const openapi = (await loadOpenApi(c)) as OpenApiDoc
const tags = openapi.tags || []
// 统计每个 tag 下的 API 数量
const counts = new Map<string, number>()
for (const pathItem of Object.values(openapi.paths || {})) {
for (const op of Object.values(pathItem || {})) {
if (!op || typeof op !== 'object') continue
const opObj = op as Record<string, unknown>
if (!Array.isArray(opObj.tags)) continue
for (const t of opObj.tags) {
counts.set(String(t), (counts.get(String(t)) || 0) + 1)
await wrapCommand({
command: 'apis tags',
appCode: c.appCode,
startTime: Date.now(),
verbose: opts.verbose,
json: opts.json,
fixes: [],
}, async (ctx) => {
try {
const openapi = (await loadOpenApi(c)) as OpenApiDoc
const tags = openapi.tags || []
// 统计每个 tag 下的 API 数量
const counts = new Map<string, number>()
for (const pathItem of Object.values(openapi.paths || {})) {
for (const op of Object.values(pathItem || {})) {
if (!op || typeof op !== 'object') continue
const opObj = op as Record<string, unknown>
if (!Array.isArray(opObj.tags)) continue
for (const t of opObj.tags) {
counts.set(String(t), (counts.get(String(t)) || 0) + 1)
}
}
}
const summary = {
tagCount: tags.length,
totalApis: [...counts.values()].reduce((a, b) => a + b, 0),
}
if (opts.json) {
printJson({
tagCount: tags.length,
tags: tags.map((t) => ({ name: t.name, description: t.description, apiCount: counts.get(t.name) || 0 })),
})
return ctx.success(summary)
}
log.info(`OpenAPI 共有 ${tags.length} 个 tag`)
log.printRawTable(
['NAME', 'API_COUNT', 'DESCRIPTION'],
tags.map((t) => [
String(t.name || ''),
String(counts.get(t.name) || 0),
String(t.description || '').slice(0, 60),
]),
{ maxColWidth: 80 },
)
log.info(`\n💡 用法: dacli apis list --tag=<name>`)
ctx.success(summary)
} catch (e) {
ctx.fail(`apis tags 失败: ${(e as Error).message?.slice(0, 200)}`)
}
}
if (opts.json) {
printJson({
tagCount: tags.length,
tags: tags.map((t) => ({ name: t.name, description: t.description, apiCount: counts.get(t.name) || 0 })),
})
return
}
log.info(`OpenAPI 共有 ${tags.length} 个 tag`)
console.log('NAME'.padEnd(30) + 'API_COUNT'.padEnd(12) + 'DESCRIPTION')
console.log('-'.repeat(95))
for (const t of tags) {
const name = String(t.name || '').padEnd(30)
const cnt = String(counts.get(t.name) || 0).padEnd(12)
const desc = String(t.description || '').slice(0, 50)
console.log(`${name}${cnt}${desc}`)
}
log.info(`\n💡 用法: dacli apis list --tag=<name>`)
})
})
installHelpHook(cmd, {