diff --git a/src/commands/bigscreen/create.ts b/src/commands/bigscreen/create.ts index b416522..6bbea89 100644 --- a/src/commands/bigscreen/create.ts +++ b/src/commands/bigscreen/create.ts @@ -16,7 +16,7 @@ import { JnpfClient } from '../../core/client.ts' import { installHelpHook } from '../../core/help.ts' import { log, printJson } from '../../core/log.ts' import { initWebapi, loadVisualData } from '../../webapi/visualdata.ts' -import { setupOutputFilter, printCommandEnd } from '../_output_filter.ts' // v2.1+ (2026-07-21): 输出过滤 + CommandLog 落盘 +import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤 + CommandLog; v2.2+ wrapCommand 收口 5c import { runCommandAutoFix, fetchExistingLists } from '../_auto_fix.ts' // v2.1+ (2026-07-21): enCode/fullName 跨命令 auto-fix interface CreateOpts { @@ -41,8 +41,7 @@ export function buildBigscreenCreateCommand(): Command { .option('--status ', '状态 0/1 (镜像 web visual.js addObj: visual.status)', '1') .option('--width ', '宽度 (镜像 web visual.js addObj: config.detail.width)', '1920') .option('--height ', '高度 (镜像 web visual.js addObj: config.detail.height)', '1080') - .option('--verbose', '完整输出 (含 step machine、API 调用等过程日志)', false) - .option('--json', '原始 JSON 输出', false) + addOutputOptions(cmd) .action(async (opts: CreateOpts) => { // 输出过滤: 默认静默 (quiet mode), --verbose 走完整日志, --json 走原始 JSON setupOutputFilter(opts) @@ -56,64 +55,48 @@ export function buildBigscreenCreateCommand(): Command { Object.assign(opts, cmdFix.opts) } - // v0.14+ (2026-07-01): 走 webapi/visualdata.addObj (跟 list/info/release/update/delete 一致) - // - 镜像 web 端 jnpf-web-datascreen-vue3/src/api/visual.js: addObj - // - 端点: POST /api/blade-visual/visual/save (jnpf-visualdata 微服务) - // - 之前 v3.0+ 走 webapi/datascreen.addObj 是错的 (端点 /api/visual/save, 不是 web 真实) - await initWebapi(c) - - // 镜像 web 端 visual.js addObj body: - // { visual: { password, category, status, title }, config: { detail: JSON.stringify(...), component: '[]' } } - // web 端: visual.title = data.title = opts.fullName (中文名), visual.category = data.category = opts.category - const webAddObjParams = { - title: opts.fullName, - category: opts.category, - status: Number(opts.status) || 1, - password: opts.password || '', - width: Number(opts.width) || 1920, - height: Number(opts.height) || 1080, - // config 暂用 web 端默认 (detail = JSON.stringify(config + name/width/height), component = '[]') - } - - // 调用 web 端 addObj (走 /api/blade-visual/visual/save → jnpf-visualdata 微服务) - // shim 已 unwrap envelope + 包成 {data: }, r.data = 后端 data 字段 (id 字符串或对象) - const r = (await loadVisualData.addObj(webAddObjParams)) as { data?: string | { id?: string } } - - // 后端返纯字符串 id (后端 VisualCrform.save 返 .toString()), 也可能返 { id } - const data = r.data - let id: string | undefined - if (typeof data === 'string' && /^\d+$/.test(data.trim())) id = data.trim() - else if (data && typeof data === 'object') id = (data as { id?: string }).id - - if (!id) { - const errMsg = `bigscreen create 后端未返 id. 实际 raw: ${JSON.stringify(r).slice(0, 200)}` - // v2.1+ (2026-07-21): 错误路径也落 CommandLog (L3 兜底) - printCommandEnd({ - command: 'bigscreen create', - appCode: c.appCode, - result: 'failure', - error: errMsg, - duration_ms: Date.now() - startTime, - fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions], - verbose: opts.verbose, - json: opts.json, - }) - log.error(errMsg) - process.exit(1) - } - log.ok( - `✓ 大屏创建成功 id=${id} enCode=${opts.enCode} (镜像 web 端 datascreen-vue3 addObj)`, - ) - if (opts.json) printJson({ id, enCode: opts.enCode }) - // v2.1+ (2026-07-21): 成功路径落 CommandLog (L3) - printCommandEnd({ + await wrapCommand({ command: 'bigscreen create', appCode: c.appCode, - summary: { id, enCode: opts.enCode }, - fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions], - duration_ms: Date.now() - startTime, + startTime, verbose: opts.verbose, json: opts.json, + fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions], + }, async (ctx) => { + try { + // v0.14+ (2026-07-01): 走 webapi/visualdata.addObj (跟 list/info/release/update/delete 一致) + await initWebapi(c) + + // 镜像 web 端 visual.js addObj body + const webAddObjParams = { + title: opts.fullName, + category: opts.category, + status: Number(opts.status) || 1, + password: opts.password || '', + width: Number(opts.width) || 1920, + height: Number(opts.height) || 1080, + } + + // 调用 web 端 addObj (走 /api/blade-visual/visual/save → jnpf-visualdata 微服务) + const r = (await loadVisualData.addObj(webAddObjParams)) as { data?: string | { id?: string } } + + // 后端返纯字符串 id (后端 VisualCrform.save 返 .toString()), 也可能返 { id } + const data = r.data + let id: string | undefined + if (typeof data === 'string' && /^\d+$/.test(data.trim())) id = data.trim() + else if (data && typeof data === 'object') id = (data as { id?: string }).id + + if (!id) { + return ctx.fail(`bigscreen create 后端未返 id. 实际 raw: ${JSON.stringify(r).slice(0, 200)}`, { enCode: opts.enCode }) + } + log.ok( + `✓ 大屏创建成功 id=${id} enCode=${opts.enCode} (镜像 web 端 datascreen-vue3 addObj)`, + ) + if (opts.json) printJson({ id, enCode: opts.enCode }) + ctx.success({ id, enCode: opts.enCode }) + } catch (e) { + ctx.fail(`bigscreen create 失败: ${(e as Error).message?.slice(0, 200)}`) + } }) }) installHelpHook(cmd, { diff --git a/src/commands/bigscreen/delete.ts b/src/commands/bigscreen/delete.ts index 7a2aade..8f42e4a 100644 --- a/src/commands/bigscreen/delete.ts +++ b/src/commands/bigscreen/delete.ts @@ -10,26 +10,43 @@ import { JnpfClient } from '../../core/client.ts' import { installHelpHook } from '../../core/help.ts' import { log } from '../../core/log.ts' import { initWebapi, loadVisualData } from '../../webapi/visualdata.ts' +import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c -interface Opts { id: string; yes?: boolean } +interface Opts { id: string; yes?: boolean; json?: boolean; verbose?: boolean } export function buildBigscreenDeleteCommand(): Command { const cmd = new Command('delete') .description('大屏删除 (硬删, v0.11+ webapi)') .requiredOption('--id ', '大屏 id') .option('--yes', '跳过确认', false) + addOutputOptions(cmd) .action(async (opts: Opts) => { - if (!opts.yes) { - log.warn(`即将删除大屏 id=${opts.id}, 加 --yes 跳过确认`) - process.exit(2) - } + setupOutputFilter(opts) const c = await JnpfClient.fromToken() - await initWebapi(c) - log.info(`loadVisualData.delObj (id=${opts.id})`) - // shim 已 unwrap, r.data = 后端 data 字段 (null 或受影响行数) - // visual.js delObj 签名: delObj(id) → /visual/remove (POST, params={ids: id}) - await loadVisualData.delObj(opts.id) - log.ok(`✓ 大屏已删除 (id=${opts.id})`) + + await wrapCommand({ + command: 'bigscreen delete', + appCode: c.appCode, + startTime: Date.now(), + verbose: opts.verbose, + json: opts.json, + fixes: [], + }, async (ctx) => { + try { + if (!opts.yes) { + return ctx.fail(`即将删除大屏 id=${opts.id}, 加 --yes 跳过确认`, { id: opts.id }) + } + await initWebapi(c) + log.info(`loadVisualData.delObj (id=${opts.id})`) + // shim 已 unwrap, r.data = 后端 data 字段 (null 或受影响行数) + // visual.js delObj 签名: delObj(id) → /visual/remove (POST, params={ids: id}) + await loadVisualData.delObj(opts.id) + log.ok(`✓ 大屏已删除 (id=${opts.id})`) + ctx.success({ id: opts.id }) + } catch (e) { + ctx.fail(`bigscreen delete 失败: ${(e as Error).message?.slice(0, 200)}`) + } + }) }) installHelpHook(cmd, { purpose: '大屏删除', diff --git a/src/commands/bigscreen/info.ts b/src/commands/bigscreen/info.ts index 1ad4f17..6e89e72 100644 --- a/src/commands/bigscreen/info.ts +++ b/src/commands/bigscreen/info.ts @@ -14,37 +14,52 @@ import { installHelpHook } from '../../core/help.ts' import { log, printJson } from '../../core/log.ts' import { initWebapi, loadVisualData } from '../../webapi/visualdata.ts' import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts' +import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c interface InfoOpts { id: string json?: boolean + /** v2.1+ (2026-07-21): --verbose, addOutputOptions 注入 */ + verbose?: boolean } export function buildBigscreenInfoCommand(): Command { const cmd = new Command('info') .description('查看大屏详情') .requiredOption('--id ', '大屏 id') - .option('--json', '原始 JSON 输出', false) + addOutputOptions(cmd) .action(async (opts: InfoOpts) => { + setupOutputFilter(opts) const c = await JnpfClient.fromToken() - await initWebapi(c) - // v0.14+ 用 /api/blade-visual/visual/detail (走 jnpf-visualdata 微服务, 不是 32000 datareport-univer) - // shim 已 unwrap, r.data = { visual: {...}, config: {...} } (后端返复合对象) - // visual: { id, title/fullName, enCode, category, password, backgroundUrl, status } - // config: { detail: , component: , id, visualId } - const r = (await loadVisualData.getObj(opts.id)) as { - data?: { visual?: Record; config?: Record } - } - const visual = r.data?.visual - if (!visual) { - const enhanced = enhanceFromBackend('未找到大屏', 'bigscreen', { entity: 'bigscreen' }) - log.error(` ${formatErrorLine(enhanced, { step: 0, stepName: '后端API' })}`) - process.exit(1) - } - log.info(`大屏 id=${opts.id} 名称=${visual.fullName || visual.title} 编码=${visual.enCode}`) - console.log(`category: ${visual.category || '-'}`) - console.log(`status: ${visual.status === 1 ? '已发布' : '未发布'}`) - if (opts.json) printJson(r.data) + + await wrapCommand({ + command: 'bigscreen info', + appCode: c.appCode, + startTime: Date.now(), + verbose: opts.verbose, + json: opts.json, + fixes: [], + }, async (ctx) => { + try { + await initWebapi(c) + const r = (await loadVisualData.getObj(opts.id)) as { + data?: { visual?: Record; config?: Record } + } + const visual = r.data?.visual + if (!visual) { + const enhanced = enhanceFromBackend('未找到大屏', 'bigscreen', { entity: 'bigscreen' }) + log.error(` ${formatErrorLine(enhanced, { step: 0, stepName: '后端API' })}`) + return ctx.fail(`bigscreen info 失败: id=${opts.id} 未找到`, { id: opts.id }) + } + log.info(`大屏 id=${opts.id} 名称=${visual.fullName || visual.title} 编码=${visual.enCode}`) + console.log(`category: ${visual.category || '-'}`) + console.log(`status: ${visual.status === 1 ? '已发布' : '未发布'}`) + if (opts.json) printJson(r.data) + ctx.success({ id: opts.id, enCode: visual.enCode }) + } catch (e) { + ctx.fail(`bigscreen info 失败: ${(e as Error).message?.slice(0, 200)}`) + } + }) }) installHelpHook(cmd, { purpose: '查看大屏详情', diff --git a/src/commands/bigscreen/release.ts b/src/commands/bigscreen/release.ts index 5514fdc..0c23b8c 100644 --- a/src/commands/bigscreen/release.ts +++ b/src/commands/bigscreen/release.ts @@ -19,9 +19,13 @@ import { installHelpHook } from '../../core/help.ts' import { log } from '../../core/log.ts' import { initWebapi, loadVisualData } from '../../webapi/visualdata.ts' import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts' +import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c interface ReleaseOpts { id: string + json?: boolean + /** v2.1+ (2026-07-21): --verbose, addOutputOptions 注入 */ + verbose?: boolean } interface VisualObj { @@ -34,28 +38,41 @@ export function buildBigscreenReleaseCommand(): Command { const cmd = new Command('release') .description('发布大屏 (state 0→1)') .requiredOption('--id ', '大屏 id') + addOutputOptions(cmd) .action(async (opts: ReleaseOpts) => { + setupOutputFilter(opts) const c = await JnpfClient.fromToken() - await initWebapi(c) - // === step 1: 先 getObj 拿完整 visual 对象 (必须传全字段) === - // shim 已 unwrap, r.data = 大屏 visual 记录 - const info = (await loadVisualData.getObj(opts.id)) as { data?: VisualObj } - const visual = info.data - if (!visual) { - const enhanced = enhanceFromBackend('未找到大屏', 'bigscreen', { entity: 'bigscreen' }) - log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '读取大屏详情' })}`) - process.exit(1) - } - // === step 2: 改 status=1 (0=未发布, 1=已发布) === - visual.status = 1 + await wrapCommand({ + command: 'bigscreen release', + appCode: c.appCode, + startTime: Date.now(), + verbose: opts.verbose, + json: opts.json, + fixes: [], + }, async (ctx) => { + try { + await initWebapi(c) - // === step 3: updateObj (后端会读 visual.status 决定发布状态) === - // web 端 updateObj 签名: updateObj(data) → 内部包成 { visual: data } - // shim 已 unwrap, update 成功返 {data: null} 或 {data: 1} (后端返受影响的行数) - // 不检查 r.code (shim unwrap 后 code 字段不直接暴露, 通过 defHttp stub 处理 null data) - await loadVisualData.updateObj(visual) - log.info(`✓ 大屏 id=${opts.id} 已发布 (status=1)`) + // === step 1: 先 getObj 拿完整 visual 对象 (必须传全字段) === + const info = (await loadVisualData.getObj(opts.id)) as { data?: VisualObj } + const visual = info.data + if (!visual) { + const enhanced = enhanceFromBackend('未找到大屏', 'bigscreen', { entity: 'bigscreen' }) + log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '读取大屏详情' })}`) + return ctx.fail(`bigscreen release 失败: id=${opts.id} 未找到`, { id: opts.id }) + } + // === step 2: 改 status=1 (0=未发布, 1=已发布) === + visual.status = 1 + + // === step 3: updateObj (后端会读 visual.status 决定发布状态) === + await loadVisualData.updateObj(visual) + log.info(`✓ 大屏 id=${opts.id} 已发布 (status=1)`) + ctx.success({ id: opts.id, status: 1 }) + } catch (e) { + ctx.fail(`bigscreen release 失败: ${(e as Error).message?.slice(0, 200)}`) + } + }) }) installHelpHook(cmd, { purpose: '发布大屏 (state 0→1, 菜单可挂)', diff --git a/src/commands/bigscreen/update.ts b/src/commands/bigscreen/update.ts index 2bb8f88..4505ee4 100644 --- a/src/commands/bigscreen/update.ts +++ b/src/commands/bigscreen/update.ts @@ -10,8 +10,9 @@ import { JnpfClient } from '../../core/client.ts' import { installHelpHook } from '../../core/help.ts' import { log, printJson } from '../../core/log.ts' import { initWebapi, loadVisualData } from '../../webapi/visualdata.ts' -import { dirtyCheck, dirtyCheckFailAndExit } from '../_dirty_check.ts' +import { dirtyCheck, dirtyCheckFailAndExit, DirtyCheckError } from '../_dirty_check.ts' import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts' +import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c interface Opts { id: string @@ -21,6 +22,8 @@ interface Opts { expectedEnCode?: string force?: boolean json?: boolean + /** v2.1+ (2026-07-21): --verbose, addOutputOptions 注入 */ + verbose?: boolean } export function buildBigscreenUpdateCommand(): Command { @@ -32,50 +35,71 @@ export function buildBigscreenUpdateCommand(): Command { .option('--category ', '分类 (1=数据大屏 2=仪表盘)') .option('--expected-enCode ', 'v0.11e+ Phase A2: 校验 enCode 未变 (防 AI session 误覆盖)') .option('--force', 'v0.11e+ Phase A2: 跳过 dirty check', false) - .option('--json', '原始 JSON 输出', false) + addOutputOptions(cmd) .action(async (opts: Opts) => { - // === step 0 校验 --id + 拉原大屏 (loadVisualData.getVisualDataInfo) === + setupOutputFilter(opts) const c = await JnpfClient.fromToken() - await initWebapi(c) - // === step 1 dirty check (Phase A2 防 AI session 误覆盖) === - const wrapper = (await loadVisualData.getVisualDataInfo?.(opts.id) || - (await loadVisualData.getInfo?.(opts.id))) as { data?: Record } | Record - const orig = (wrapper as { data?: Record }).data || (wrapper as Record) - if (!orig) { - const enhanced = enhanceFromBackend('拉原大屏失败, id 不存在', 'id', { entity: 'bigscreen' }) - log.error(` ${formatErrorLine(enhanced, { step: 0, stepName: '配置拉取' })}`) - process.exit(1) - } - // v0.11e+ Phase A2: dirty check (防 AI session 误覆盖) - const dirtyResult = dirtyCheck({ - expectedEnCode: opts.expectedEnCode, - force: opts.force, - current: { - enCode: orig.enCode as string | undefined, - state: orig.enabledMark as string | number | undefined, - fullName: orig.fullName as string | undefined, - type: 'di', - }, - id: opts.id, + await wrapCommand({ + command: 'bigscreen update', + appCode: c.appCode, + startTime: Date.now(), + verbose: opts.verbose, + json: opts.json, + fixes: [], + }, async (ctx) => { + try { + // === step 0 校验 --id + 拉原大屏 === + await initWebapi(c) + const wrapper = (await loadVisualData.getVisualDataInfo?.(opts.id) || + (await loadVisualData.getInfo?.(opts.id))) as { data?: Record } | Record + const orig = (wrapper as { data?: Record }).data || (wrapper as Record) + if (!orig) { + const enhanced = enhanceFromBackend('拉原大屏失败, id 不存在', 'id', { entity: 'bigscreen' }) + log.error(` ${formatErrorLine(enhanced, { step: 0, stepName: '配置拉取' })}`) + return ctx.fail(`bigscreen update 失败: 拉原大屏 id=${opts.id} 失败`, { id: opts.id }) + } + + // v0.11e+ Phase A2: dirty check (防 AI session 误覆盖) + const dirtyResult = dirtyCheck({ + expectedEnCode: opts.expectedEnCode, + force: opts.force, + current: { + enCode: orig.enCode as string | undefined, + state: orig.enabledMark as string | number | undefined, + fullName: orig.fullName as string | undefined, + type: 'di', + }, + id: opts.id, + }) + if (!dirtyResult.ok) { + // v2.2+ (2026-07-21): dirtyCheckFailAndExit 改 throw (DirtyCheckError), 下方 catch → ctx.fail + try { + dirtyCheckFailAndExit(dirtyResult, { type: 'di', id: opts.id, current: { enCode: orig.enCode as string | undefined, fullName: orig.fullName as string | undefined } }, log.error) + } catch (e) { + if (e instanceof DirtyCheckError) { + return ctx.fail(e.message, { id: opts.id, type: 'bigscreen', errorCount: e.errorCount }) + } + throw e + } + } else if (dirtyResult.skipped) { + log.info(`⚠ dirty check 已 --force 跳过`) + } + + // === step 2 PUT 后端API (loadVisualData.updateObj) === + const body: Record = { ...orig, id: opts.id } + if (opts.fullName) body.fullName = opts.fullName + if (opts.enCode) body.enCode = opts.enCode + if (opts.category) body.category = opts.category + log.info(`loadVisualData.updateObj (visual.id=${body.id})`) + const r = (await loadVisualData.updateObj(body)) as { data?: unknown } + log.ok(`✓ 大屏更新成功 (id=${opts.id})`) + if (opts.json) printJson(r.data) + ctx.success({ id: opts.id }) + } catch (e) { + ctx.fail(`bigscreen update 失败: ${(e as Error).message?.slice(0, 200)}`) + } }) - if (!dirtyResult.ok) { - dirtyCheckFailAndExit(dirtyResult, { type: 'di', id: opts.id, current: { enCode: orig.enCode as string | undefined, fullName: orig.fullName as string | undefined } }, log.error) - } else if (dirtyResult.skipped) { - log.info(`⚠ dirty check 已 --force 跳过`) - } - - // === step 2 PUT 后端API (loadVisualData.updateVisualData) === - const body: Record = { ...orig, id: opts.id } - if (opts.fullName) body.fullName = opts.fullName - if (opts.enCode) body.enCode = opts.enCode - if (opts.category) body.category = opts.category - log.info(`loadVisualData.updateObj (visual.id=${body.id})`) - // web 端 updateObj(visual) → 内部包成 {visual: data} → /visual/update (POST) - // shim 已 unwrap, update 成功返 {data: null} 或 {data: 1} - const r = (await loadVisualData.updateObj(body)) as { data?: unknown } - log.ok(`✓ 大屏更新成功 (id=${opts.id})`) - if (opts.json) printJson(r.data) }) installHelpHook(cmd, { purpose: '大屏修改. 改 fullName/enCode/category, PUT 语义 + v0.11e+ dirty check.',