feat(bigscreen-update): v0.11e+ dirty check + enhanceFromBackend (Phase A2+A1)

bigscreen/update.ts:
- 加 --expected-enCode/--force 选项 (A2 dirty check)
- 配置拉取失败 catch + 后端更新失败 catch 都用 enhanceFromBackend (A1)
This commit is contained in:
ssw committed 2026-06-27 04:10:53 +08:00
1 parent cc61ac0cca
commit 58ea281b7d
1 file changed
+41 -8
+41 -8
View File
@@ -10,6 +10,7 @@ import { JnpfClient } from '../../core/client.ts'
import { installAiHelpHook } 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 { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
interface Opts {
@@ -17,35 +18,56 @@ interface Opts {
fullName?: string
enCode?: string
category?: string
expectedEnCode?: string
force?: boolean
json?: boolean
}
export function buildBigscreenUpdateCommand(): Command {
const cmd = new Command('update')
.description('大屏修改 (改 fullName/enCode/category, PUT 语义, v0.11+ webapi)')
.description('大屏修改 (改 fullName/enCode/category, PUT 语义, v0.11+ webapi + v0.11e+ dirty check)')
.requiredOption('--id <id>', '大屏 id')
.option('--fullName <n>', '大屏名')
.option('--enCode <e>', '大屏编码')
.option('--category <c>', '分类 (1=数据大屏 2=仪表盘)')
.option('--expected-enCode <e>', 'v0.11e+ Phase A2: 校验 enCode 未变 (防 AI session 误覆盖)')
.option('--force', 'v0.11e+ Phase A2: 跳过 dirty check', false)
.option('--json', '原始 JSON 输出', false)
.action(async (opts: Opts) => {
const c = await JnpfClient.fromToken()
await initWebapi(c)
// web 端 updateVisualData(id, data) 接受 (id, data)
// 走 getInfo 拿原数据
const wrapper = (await loadVisualData.getVisualDataInfo?.(opts.id) ||
(await loadVisualData.getInfo?.(opts.id))) as { data?: Record<string, unknown> } | Record<string, unknown>
const orig = (wrapper as { data?: Record<string, unknown> }).data || (wrapper as Record<string, unknown>)
if (!orig) {
log.error(`bigscreen update 失败: 拉原大屏 id=${opts.id} 失败`)
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,
})
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 跳过`)
}
const body: Record<string, unknown> = { ...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.updateVisualData (id=${opts.id})`)
// web 端 update(id, data) 调用
const r = (await (
loadVisualData.updateVisualData?.(opts.id, body) || loadVisualData.update?.(opts.id, body)
)) as { code?: number; msg?: string; data?: unknown }
@@ -59,12 +81,23 @@ export function buildBigscreenUpdateCommand(): Command {
if (opts.json) printJson(r.data)
})
installAiHelpHook(cmd, {
purpose: '大屏修改. 改 fullName/enCode/category, PUT 语义.',
purpose: '大屏修改. 改 fullName/enCode/category, PUT 语义 + v0.11e+ dirty check.',
required: { '--id': '大屏 id' },
optional: { '--fullName': '大屏名', '--enCode': '大屏编码', '--category': '分类' },
optional: {
'--fullName': '大屏名',
'--enCode': '大屏编码',
'--category': '分类',
'--expected-enCode': '防 AI 误覆盖 (A2)',
'--force': '跳过 dirty check (A2)',
},
example: 'dacli bigscreen update --id=123 --fullName=新名',
source: { api: 'POST /api/visualdata/update (jnpf-web-datascreen-vue3)', ref: 'web/.../jnpf-web-datascreen-vue3/src/api/visual.js updateVisualData()' },
notes: ['v0.11+ 改用 webapi 镜像 (loadVisualData.updateVisualData)', '大屏 web 端在 jnpf-web-datascreen-vue3'],
notes: [
'v0.11+ 改用 webapi 镜像 (loadVisualData.updateVisualData)',
'大屏 web 端在 jnpf-web-datascreen-vue3',
'v0.11e+ Phase A2: dirty check --expected-enCode (防 AI session 误覆盖)',
'v0.11e+ Phase A1: catch enhanceFromBackend (错误码自动分类)',
],
})
return cmd
}