diff --git a/src/commands/report/publish.ts b/src/commands/report/publish.ts index 27a5805..2e06798 100644 --- a/src/commands/report/publish.ts +++ b/src/commands/report/publish.ts @@ -26,6 +26,7 @@ import { log, printJson } from '../../core/log.ts' import { initWebapi as initReportWebapi, loadReport } from '../../webapi/report.ts' import { validateReportCreate } from './_validate.ts' import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts' +import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c interface PublishOpts { id: string @@ -35,6 +36,8 @@ interface PublishOpts { queryList?: string versionId?: string json?: boolean + /** v2.1+ (2026-07-21): --verbose, addOutputOptions 注入 */ + verbose?: boolean } export function buildReportPublishCommand(): Command { @@ -48,72 +51,89 @@ export function buildReportPublishCommand(): Command { .option('--columnList ', '列定义 JSON 数组字符串') .option('--queryList ', '查询参数 JSON 数组字符串') .option('--versionId ', '版本 id (留空则创建新版本)') - .option('--json', '原始 JSON 输出', false) + addOutputOptions(cmd) .action(async (opts: PublishOpts) => { + setupOutputFilter(opts) const c = await JnpfClient.fromToken() - await initReportWebapi(c) - // 拉原报表 (跟 update 一致) - const current = (await loadReport.getReportInfo(opts.id)) as Record - if (!current || !current.id) { - log.error(`report publish 失败: 拉原报表 id=${opts.id} 失败`) - process.exit(1) - } + await wrapCommand({ + command: 'report publish', + appCode: c.appCode, + startTime: Date.now(), + verbose: opts.verbose, + json: opts.json, + fixes: [], + }, async (ctx) => { + try { + await initReportWebapi(c) - // step machine 校验 (validateReportCreate 返 {ok, steps: StepValidation[]}) - const vBasic = validateReportCreate({ - fullName: current.fullName as string, - enCode: current.enCode as string, - category: current.category as string, - }) - if (!vBasic.ok) { - for (const step of vBasic.steps) { - for (const err of step.errors) log.error(` [${step.stepName}] ${err.message}`) + // 拉原报表 (跟 update 一致) + const current = (await loadReport.getReportInfo(opts.id)) as Record + if (!current || !current.id) { + log.error(`report publish 失败: 拉原报表 id=${opts.id} 失败`) + return ctx.fail(`report id=${opts.id} 不存在`, { id: opts.id }) + } + + // step machine 校验 (validateReportCreate 返 {ok, steps: StepValidation[]}) + const vBasic = validateReportCreate({ + fullName: current.fullName as string, + enCode: current.enCode as string, + category: current.category as string, + }) + if (!vBasic.ok) { + for (const step of vBasic.steps) { + for (const err of step.errors) log.error(` [${step.stepName}] ${err.message}`) + } + return ctx.fail(`basic info 校验失败: ${vBasic.steps.reduce((n, s) => n + s.errors.length, 0)} 条`, { id: opts.id }) + } + + // v0.16+ (P2-2): --cells 必传 (type=1 必填) + JSON 预校验 + if (!opts.cells) { + log.error('--cells 必传 (publish 隐式 type=1, 后端要求 cells 非空)') + return ctx.fail(`--cells 必传`, { id: opts.id }) + } + try { + JSON.parse(opts.cells) + } catch (e) { + log.error(`--cells 不是合法 JSON 字符串: ${(e as Error).message?.slice(0, 100)}`) + return ctx.fail(`--cells 不是合法 JSON 字符串`, { id: opts.id, error: (e as Error).message?.slice(0, 100) }) + } + + // 构造 body (跟 release --type=1 一致, type 写死 1) + const body: Record = { + ...current, + id: opts.id, + type: 1, // v0.16+ publish 隐式 type=1 (真正发布+部署) + versionId: opts.versionId || current.versionId || null, + } + if (opts.cells !== undefined) body.cells = opts.cells + if (opts.dataSets !== undefined) body.dataSetList = opts.dataSets + if (opts.columnList !== undefined) body.columnList = opts.columnList + if (opts.queryList !== undefined) body.queryList = opts.queryList + + // 调 webapi saveVersion (走 web 端镜像, 端点 /api/Report/Save type=1) + const r = (await loadReport.saveVersion(body)) as unknown + let code: number | undefined + let msg: string | undefined + if (r && typeof r === 'object') { + const obj = r as { code?: number; msg?: string } + code = obj.code + msg = obj.msg + } + if (code !== undefined && code !== 200) { + log.error(`report publish 失败 (code=${code}): ${msg}`) + const enhanced = enhanceFromBackend(msg || '', 'report', { entity: 'report' }) + log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`) + return ctx.fail(`report publish 失败 (code=${code}): ${msg}`, { id: opts.id, code }) + } + log.ok(`✓ 报表已发布+部署 id=${opts.id} (v0.16+ 新 publish 命令, type=1)`) + const summary = { id: opts.id, code, msg, raw: r } + if (opts.json) printJson(summary) + ctx.success(summary) + } catch (e) { + ctx.fail(`report publish 失败: ${(e as Error).message?.slice(0, 200)}`) } - process.exit(1) - } - - // v0.16+ (P2-2): --cells 必传 (type=1 必填) + JSON 预校验 - if (!opts.cells) { - log.error('--cells 必传 (publish 隐式 type=1, 后端要求 cells 非空)') - process.exit(1) - } - try { - JSON.parse(opts.cells) - } catch (e) { - log.error(`--cells 不是合法 JSON 字符串: ${(e as Error).message?.slice(0, 100)}`) - process.exit(1) - } - - // 构造 body (跟 release --type=1 一致, type 写死 1) - const body: Record = { - ...current, - id: opts.id, - type: 1, // v0.16+ publish 隐式 type=1 (真正发布+部署) - versionId: opts.versionId || current.versionId || null, - } - if (opts.cells !== undefined) body.cells = opts.cells - if (opts.dataSets !== undefined) body.dataSetList = opts.dataSets - if (opts.columnList !== undefined) body.columnList = opts.columnList - if (opts.queryList !== undefined) body.queryList = opts.queryList - - // 调 webapi saveVersion (走 web 端镜像, 端点 /api/Report/Save type=1) - const r = (await loadReport.saveVersion(body)) as unknown - let code: number | undefined - let msg: string | undefined - if (r && typeof r === 'object') { - const obj = r as { code?: number; msg?: string } - code = obj.code - msg = obj.msg - } - if (code !== undefined && code !== 200) { - log.error(`report publish 失败 (code=${code}): ${msg}`) - const enhanced = enhanceFromBackend(msg || '', 'report', { entity: 'report' }) - log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`) - process.exit(1) - } - log.ok(`✓ 报表已发布+部署 id=${opts.id} (v0.16+ 新 publish 命令, type=1)`) - if (opts.json) printJson({ id: opts.id, code, msg, raw: r }) + }) }) installHelpHook(cmd, { purpose: '发布+部署报表 (v0.16+ 新增, 替代 release --type=1)', @@ -143,4 +163,4 @@ export function buildReportPublishCommand(): Command { ], }) return cmd -} +} \ No newline at end of file