From 556709eea3b799a8647c496bdc467006570e0143 Mon Sep 17 00:00:00 2001 From: ssw Date: Tue, 21 Jul 2026 18:11:18 +0800 Subject: [PATCH] =?UTF-8?q?feat(dacli):=20P4=20v3=20sample=204=20=E6=96=87?= =?UTF-8?q?=E4=BB=B6=20console.log=20=E8=A1=A8=20=E2=86=92=20printRawTable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 3 wrapCommand 迁移 + table swap 同步: - dict/datalist.ts (just swap console.log → printRawTable 内 已迁 wrapCommand) - di/list.ts (新 wrapCommand + printRawTable, 之前无 L3 接入) - apis/list.ts (新 wrapCommand + printRawTable, JNPF OpenAPI 接口列表) 每个文件: - setupOutputFilter + addOutputOptions(--verbose --json) - wrapCommand + ctx.success/fail - log.printRawTable 收口 console.log 表格输出 验证: - TS compile 0 新错误 (baseline 57 不变) - 73/73 单元测试 PASS (含 printRawTable 5 单测) 剩 ~310 处 console.log 表格散布 (含 dict/typelist printTree 递归, user/info 菜单树等非表格格式, 用户/角色/权限 dump, 不适 printRawTable). 后续批量迁移 可按以下优先级: - 真表格 (padEnd + '-' separator): 已迁 4 文件, 剩 ~10 - 菜单树状 dump: 建议保留 console.log 或写 printTree 单独 helper - 用户/角色 dump: 建议保留 key:value 格式, 不动 --- src/commands/apis/list.ts | 62 ++++++++++++++++++++++----------- src/commands/di/list.ts | 65 ++++++++++++++++++++++++----------- src/commands/dict/datalist.ts | 13 ++++--- 3 files changed, 93 insertions(+), 47 deletions(-) diff --git a/src/commands/apis/list.ts b/src/commands/apis/list.ts index 26a49fc..8f62256 100644 --- a/src/commands/apis/list.ts +++ b/src/commands/apis/list.ts @@ -8,8 +8,9 @@ import { JnpfClient } from '../../core/client.ts' import { installHelpHook } from '../../core/help.ts' import { log, printJson } from '../../core/log.ts' import { loadOpenApi } from './_cache.ts' +import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c -interface Opts { tag?: string; method?: string; keyword?: string; json?: boolean } +interface Opts { tag?: string; method?: string; keyword?: string; json?: boolean; verbose?: boolean } export function buildApisListCommand(): Command { const cmd = new Command('list') @@ -17,29 +18,48 @@ export function buildApisListCommand(): Command { .option('--tag ', '按 tag 过滤 (e.g. visualdev / system / workflow)') .option('--method ', '按 HTTP method 过滤 (GET/POST/...)') .option('--keyword ', '按 path 或 summary 搜索') - .option('--json', '原始 JSON 输出', false) + addOutputOptions(cmd) .action(async (opts: Opts) => { + setupOutputFilter(opts) const c = await JnpfClient.fromToken() - const doc = await loadOpenApi(c) - const rows: { method: string; path: string; summary: string; tags: string[] }[] = [] - for (const [path, methods] of Object.entries(doc.paths || {})) { - for (const [method, op] of Object.entries(methods)) { - if (opts.method && method.toUpperCase() !== opts.method.toUpperCase()) continue - // v3.0+ (2026-06-29 regression audit): 对齐 Java 版 ApiService.java:147 OR 匹配 - // 旧实现: 纯 AND (仅匹配 op.tags), 漏掉 path 含 tag 但 tags 不含的接口 - // 新实现: OR (op.tags.includes OR path.includes), 与 Java 版行为一致 - if (opts.tag && !(op.tags || []).includes(opts.tag) && !path.includes(opts.tag)) continue - if (opts.keyword && !`${path} ${op.summary || ''}`.toLowerCase().includes(opts.keyword.toLowerCase())) continue - rows.push({ method: method.toUpperCase(), path, summary: op.summary || '', tags: op.tags || [] }) + + await wrapCommand({ + command: 'apis list', + appCode: c.appCode, + startTime: Date.now(), + verbose: opts.verbose, + json: opts.json, + fixes: [], + }, async (ctx) => { + try { + const doc = await loadOpenApi(c) + const rows: { method: string; path: string; summary: string; tags: string[] }[] = [] + for (const [path, methods] of Object.entries(doc.paths || {})) { + for (const [method, op] of Object.entries(methods)) { + if (opts.method && method.toUpperCase() !== opts.method.toUpperCase()) continue + // v3.0+ (2026-06-29 regression audit): 对齐 Java 版 ApiService.java:147 OR 匹配 + // 旧实现: 纯 AND (仅匹配 op.tags), 漏掉 path 含 tag 但 tags 不含的接口 + // 新实现: OR (op.tags.includes OR path.includes), 与 Java 版行为一致 + if (opts.tag && !(op.tags || []).includes(opts.tag) && !path.includes(opts.tag)) continue + if (opts.keyword && !`${path} ${op.summary || ''}`.toLowerCase().includes(opts.keyword.toLowerCase())) continue + rows.push({ method: method.toUpperCase(), path, summary: op.summary || '', tags: op.tags || [] }) + } + } + const summary = { total: rows.length } + if (opts.json) { + printJson(rows) + return ctx.success(summary) + } + log.info(`共 ${rows.length} 个 API`) + log.printRawTable( + ['METHOD', 'PATH', 'TAGS'], + rows.map((r) => [r.method, r.path, r.tags.join(',')]), + ) + ctx.success(summary) + } catch (e) { + ctx.fail(`apis list 失败: ${(e as Error).message?.slice(0, 200)}`) } - } - if (opts.json) { printJson(rows); return } - log.info(`共 ${rows.length} 个 API`) - console.log('METHOD PATH TAGS') - console.log('-'.repeat(100)) - for (const r of rows) { - console.log(`${r.method.padEnd(7)} ${r.path.padEnd(60)} ${r.tags.join(',')}`) - } + }) }) installHelpHook(cmd, { diff --git a/src/commands/di/list.ts b/src/commands/di/list.ts index bc83119..e34642c 100644 --- a/src/commands/di/list.ts +++ b/src/commands/di/list.ts @@ -9,8 +9,9 @@ import { JnpfClient } from '../../core/client.ts' import { installHelpHook } from '../../core/help.ts' import { log, printJson } from '../../core/log.ts' import { initWebapi, loadDataInterface } from '../../webapi/di.ts' +import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c -interface Opts { json?: boolean; keyword?: string; currentPage?: string; pageSize?: string } +interface Opts { json?: boolean; keyword?: string; currentPage?: string; pageSize?: string; verbose?: boolean } export function buildDiListCommand(): Command { const cmd = new Command('list') @@ -18,28 +19,50 @@ export function buildDiListCommand(): Command { .option('--keyword ', '搜索') .option('--currentPage

', '页码', '1') .option('--pageSize ', '每页', '50') - .option('--json', '原始 JSON 输出', false) + addOutputOptions(cmd) .action(async (opts: Opts) => { + setupOutputFilter(opts) const c = await JnpfClient.fromToken() - await initWebapi(c) - // 镜像 web getDataInterfaceList: 传 { keyword, currentPage, pageSize, category? } - // web 端默认不传 category (后端 mapper 默认 eq(null) → 0 行, web 端从 store 拿选中分类) - // dacli 简化: 不传 category, 拉全部 (pageSize 限制) - log.info(`loadDataInterface.getDataInterfaceList`) - // defHttp shim 已解外层 envelope, 这里 r 已是 data 部分: { list, pagination } - const r = (await loadDataInterface.getDataInterfaceList({ - keyword: opts.keyword, - currentPage: Number(opts.currentPage || '1'), - pageSize: Number(opts.pageSize || '50'), - })) as { list?: { id: string; fullName: string; enCode: string; categoryId?: string }[] } - const list = r.list || [] - if (opts.json) { printJson(r); return } - log.info(`共 ${list.length} 个数据接口`) - console.log('ID FULL_NAME ENCODE') - console.log('-'.repeat(80)) - for (const d of list) { - console.log(`${String(d.id).padEnd(36)}${(d.fullName || '').padEnd(22)}${d.enCode || ''}`) - } + + await wrapCommand({ + command: 'di list', + appCode: c.appCode, + startTime: Date.now(), + verbose: opts.verbose, + json: opts.json, + fixes: [], + }, async (ctx) => { + try { + await initWebapi(c) + // 镜像 web getDataInterfaceList: 传 { keyword, currentPage, pageSize, category? } + // web 端默认不传 category (后端 mapper 默认 eq(null) → 0 行, web 端从 store 拿选中分类) + // dacli 简化: 不传 category, 拉全部 (pageSize 限制) + log.info(`loadDataInterface.getDataInterfaceList`) + // defHttp shim 已解外层 envelope, 这里 r 已是 data 部分: { list, pagination } + const r = (await loadDataInterface.getDataInterfaceList({ + keyword: opts.keyword, + currentPage: Number(opts.currentPage || '1'), + pageSize: Number(opts.pageSize || '50'), + })) as { list?: { id: string; fullName: string; enCode: string; categoryId?: string }[] } + const list = r.list || [] + const summary = { + total: list.length, + keyword: opts.keyword, + } + if (opts.json) { + printJson(r) + return ctx.success(summary) + } + log.info(`共 ${list.length} 个数据接口`) + log.printRawTable( + ['ID', 'FULL_NAME', 'ENCODE'], + list.map((d) => [String(d.id), d.fullName || '', d.enCode || '']), + ) + ctx.success(summary) + } catch (e) { + ctx.fail(`di list 失败: ${(e as Error).message?.slice(0, 200)}`) + } + }) }) installHelpHook(cmd, { diff --git a/src/commands/dict/datalist.ts b/src/commands/dict/datalist.ts index d5b4984..5e5d4d3 100644 --- a/src/commands/dict/datalist.ts +++ b/src/commands/dict/datalist.ts @@ -62,11 +62,14 @@ export function buildDictDataListCommand(): Command { return ctx.success(summary) } log.info(`${path} 共 ${list.length} 条`) - console.log('ENCODE'.padEnd(21) + 'FULL_NAME'.padEnd(22) + 'SORT') - console.log('-'.repeat(60)) - for (const d of list as Record[]) { - console.log(`${String(d.enCode || d.id || '').padEnd(21)}${String(d.fullName || '').padEnd(22)}${d.sortCode || ''}`) - } + log.printRawTable( + ['ENCODE', 'FULL_NAME', 'SORT'], + (list as Record[]).map((d) => [ + String(d.enCode || d.id || ''), + String(d.fullName || ''), + String(d.sortCode || ''), + ]), + ) ctx.success(summary) } catch (e) { ctx.fail(`dict datalist 失败: ${(e as Error).message?.slice(0, 200)}`)