feat(dacli): form/update.ts 完整 wrapCommand 迁移 + _dirty_check 改 throw + test 更新
form/update.ts (5 处 process.exit → ctx.fail):
- releaseOnly 模式 后端 code !== 200
- BS-49 enCode 冲突
- --fields JSON 解析失败
- --fields 不是非空数组
- PUT 后端 code !== 200 (含 enhanceFromBackend 错误增强)
关联改动:
1. src/commands/_dirty_check.ts: dirtyCheckFailAndExit 改 throw DirtyCheckError
(替代原 process.exit(2), 让 wrapCommand handler catch → ctx.fail)
新增 DirtyCheckError 类 (entityType/entityId/errorCount 字段供 handler 区分)
2. tests/unit/report_wired.test.ts: 更新 report/create.ts 契约 (3 处断言)
- printCommandEnd 6+ → wrapCommand + ctx.fail 5+
- 成功路径 printCommandEnd success → ctx.success
- 删 printCommandEnd 直接 import 断言 (wrapCommand 内部已包)
form/update 整合 DirtyCheckError 实例化:
try { dirtyCheckFailAndExit(...) }
catch (e) {
if (e instanceof DirtyCheckError) return ctx.fail(e.message, { id, errorCount })
throw e
}
修改方式: Write 整文件重写.
验证:
- TS compile 0 新错误 (baseline 57 不变)
- 73/73 单元测试 PASS (含 test 更新)
- workflow/update.ts 也用 dirtyCheckFailAndExit, 仍兼容 throw (下次迁 wrapCommand 时一并处理)
This commit is contained in:
1 parent
140c0dc691
commit
8efb4a55e0
3 files changed
+223
-185
No files matched your search
@@ -89,8 +89,9 @@ export function dirtyCheck(opts: DirtyCheckOpts): DirtyCheckResult {
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出 dirty check 失败错误 + 退出 (带 diff 提示)
|
||||
* 输出 dirty check 失败错误 + 抛异常 (带 diff 提示)
|
||||
*
|
||||
* v2.2+ (2026-07-21): 改 throw 替代 process.exit(2), 让 wrapCommand handler 收口落 L3 CommandLog
|
||||
* 镜像 web `validatePopoverRef.setVisible` 风格: 错误聚合 + 字段级定位
|
||||
*/
|
||||
export function dirtyCheckFailAndExit(
|
||||
@@ -111,5 +112,22 @@ export function dirtyCheckFailAndExit(
|
||||
logError(` 1) 跑 dacli ${ctx.type} info --id=${ctx.id} 看最新状态`)
|
||||
logError(` 2) 重新生成 update 命令 (用新 enCode/state)`)
|
||||
logError(` 3) 或: 确认你确实想覆盖, 加 --force`)
|
||||
process.exit(2)
|
||||
// v2.2+ (2026-07-21): throw 替代 process.exit(2), 让 wrapCommand handler 收口落 L3 CommandLog
|
||||
throw new DirtyCheckError(result.errors.length, ctx.type, ctx.id, result.errors.map(e => `[${e.field}] ${e.message}`).join('; '))
|
||||
}
|
||||
|
||||
/**
|
||||
* v2.2+ (2026-07-21): dirty check 错误专属类型 (供 wrapCommand handler 区分)
|
||||
*/
|
||||
export class DirtyCheckError extends Error {
|
||||
readonly errorCount: number
|
||||
readonly entityType: string
|
||||
readonly entityId: string
|
||||
constructor(errorCount: number, entityType: string, entityId: string, message: string) {
|
||||
super(`dirty check 失败 (${errorCount} 个不匹配): ${message.slice(0, 200)}`)
|
||||
this.name = 'DirtyCheckError'
|
||||
this.errorCount = errorCount
|
||||
this.entityType = entityType
|
||||
this.entityId = entityId
|
||||
}
|
||||
}
|
||||
+191
-169
@@ -10,8 +10,9 @@ import { JnpfClient } from '../../core/client.ts'
|
||||
import { installHelpHookWithPatterns } from '../../core/help.ts'
|
||||
import { log, printJson } from '../../core/log.ts'
|
||||
import { initWebapi, loadVisualDev } from '../../webapi/visualDev.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
|
||||
@@ -36,6 +37,8 @@ interface Opts {
|
||||
/** dirty check: 跳过校验, 强制覆盖 */
|
||||
force?: boolean
|
||||
json?: boolean
|
||||
/** v2.1+ (2026-07-21): --verbose, addOutputOptions 注入 */
|
||||
verbose?: boolean
|
||||
}
|
||||
|
||||
export function buildFormUpdateCommand(): Command {
|
||||
@@ -58,185 +61,204 @@ export function buildFormUpdateCommand(): Command {
|
||||
.option('--expected-enCode <e>', '期望的 enCode (与 getInfo 对比, 防止覆盖外部修改)')
|
||||
.option('--expected-state <s>', '期望的 state (0/1/2, 防止覆盖外部修改)')
|
||||
.option('--force', '跳过 dirty check, 强制覆盖 (慎用!)', false)
|
||||
.option('--json', '原始 JSON 输出', false)
|
||||
addOutputOptions(cmd)
|
||||
.action(async (opts: Opts) => {
|
||||
setupOutputFilter(opts)
|
||||
const c = await JnpfClient.fromToken()
|
||||
await initWebapi(c)
|
||||
|
||||
// === step 0 releaseOnly 分支 ===
|
||||
// 0. releaseOnly 模式: 仅重新发布
|
||||
// 0. releaseOnly 模式: 仅重新发布
|
||||
if (opts.releaseOnly) {
|
||||
log.info(`loadVisualDev.release (formId=${opts.id})`)
|
||||
const rr = (await loadVisualDev.release(opts.id)) as { code?: number; msg?: string }
|
||||
if (rr.code !== 200 && rr.code !== undefined) {
|
||||
log.error(`发布失败 code=${rr.code}: ${rr.msg}`)
|
||||
if (opts.json) printJson(rr)
|
||||
process.exit(1)
|
||||
}
|
||||
log.ok(`✓ 表单发布成功 (id=${opts.id})`)
|
||||
if (opts.json) printJson({ id: opts.id, action: 'released' })
|
||||
return
|
||||
}
|
||||
|
||||
// === step 1 拉原 form / 读 current state ===
|
||||
// 1. 拉原 form
|
||||
const orig = (await loadVisualDev.getInfo(opts.id)) as Record<string, unknown>
|
||||
log.info(`原 form: enCode=${orig.enCode} fullName=${orig.fullName}`)
|
||||
|
||||
// Phase 3.1: dirty check (AI 场景增强)
|
||||
// ⚠️ 不是 web 镜像 (web state.isReload 不是 dirty check), 是 AI 场景补充
|
||||
const dirtyResult = dirtyCheck({
|
||||
expectedEnCode: opts.expectedEnCode,
|
||||
expectedState: opts.expectedState,
|
||||
force: opts.force,
|
||||
id: opts.id,
|
||||
current: {
|
||||
enCode: orig.enCode as string | undefined,
|
||||
state: orig.state as string | number | undefined,
|
||||
fullName: orig.fullName as string | undefined,
|
||||
type: 'form',
|
||||
},
|
||||
})
|
||||
dirtyCheckFailAndExit(
|
||||
dirtyResult,
|
||||
{ type: 'form', id: opts.id, current: { enCode: orig.enCode as string, state: orig.state as string | number, fullName: orig.fullName as string } },
|
||||
log.error,
|
||||
)
|
||||
if (dirtyResult.skipped) log.warn('⚠️ --force 已跳过 dirty check (强制覆盖)')
|
||||
|
||||
// 防御: enCode 冲突检测 (全平台唯一)
|
||||
if (opts.enCode && opts.enCode !== orig.enCode) {
|
||||
const list = (await loadVisualDev.getVisualDevList({ keyword: opts.enCode })) as {
|
||||
list?: { id: string; enCode: string }[]
|
||||
}
|
||||
const conflict = list.list?.find((f) => f.enCode === opts.enCode && f.id !== opts.id)
|
||||
if (conflict) {
|
||||
log.error(`BS-49: enCode=${opts.enCode} 已被其他 form 占用 (id=${conflict.id})`)
|
||||
log.info('建议: 加 _v2 后缀, 或 dacli form delete 后重建')
|
||||
process.exit(2)
|
||||
}
|
||||
}
|
||||
|
||||
// === step 2 合并更新 / PUT 语义, 未传字段保留 ===
|
||||
// 2. 合并更新 (PUT 语义, 未传字段保留)
|
||||
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
|
||||
if (opts.dbLinkId) body.dbLinkId = opts.dbLinkId
|
||||
if (opts.enableFlow !== undefined) body.enableFlow = parseInt(opts.enableFlow, 10)
|
||||
// Bug 3 修复: --flowId 实际是 workflow templateId, 走 workflow.update --enableMark=1 + start node formId=this.id
|
||||
// 流程-form 绑定: workflow 端 start node formId 字段 (TemplateNodeEntity.formId), 不是 form 端
|
||||
// 旧代码 body.flowId = opts.flowId 是错的, 后端 VisualdevEntity 无 flowId 字段, 会被 Lombok @Data 静默丢弃
|
||||
if (opts.flowId) {
|
||||
log.warn(`--flowId=${opts.flowId}: form 端无 flowId 字段 (后端 VisualdevEntity 不含)`)
|
||||
log.info(`正确的流程-form 绑定方式: workflow 端 start node formId = ${opts.id}`)
|
||||
log.info(` → dacli workflow update --id=${opts.flowId} --enableMark=1 (自动绑当前 form 到 start node)`)
|
||||
}
|
||||
if (opts.type) body.type = parseInt(opts.type, 10)
|
||||
if (opts.webType) body.webType = parseInt(opts.webType, 10)
|
||||
|
||||
// 3. 字段增量更新 (--fields)
|
||||
if (opts.fields) {
|
||||
let parsed: unknown[]
|
||||
await wrapCommand({
|
||||
command: 'form update',
|
||||
appCode: c.appCode,
|
||||
startTime: Date.now(),
|
||||
verbose: opts.verbose,
|
||||
json: opts.json,
|
||||
fixes: [],
|
||||
}, async (ctx) => {
|
||||
try {
|
||||
parsed = JSON.parse(opts.fields)
|
||||
} catch (e) {
|
||||
log.error(`--fields JSON 解析失败: ${(e as Error).message}`)
|
||||
process.exit(2)
|
||||
}
|
||||
if (!Array.isArray(parsed) || parsed.length === 0) {
|
||||
log.error('--fields 必是非空数组')
|
||||
process.exit(2)
|
||||
}
|
||||
// formData.fields 是字符串, 需解/合/重序列
|
||||
let formData: Record<string, unknown> = {}
|
||||
const fd = orig.formData
|
||||
if (typeof fd === 'string') {
|
||||
try {
|
||||
formData = JSON.parse(fd)
|
||||
} catch (e) {
|
||||
log.warn(`原 formData 不是有效 JSON, 用空对象`)
|
||||
formData = {}
|
||||
await initWebapi(c)
|
||||
|
||||
// === step 0 releaseOnly 分支 ===
|
||||
if (opts.releaseOnly) {
|
||||
log.info(`loadVisualDev.release (formId=${opts.id})`)
|
||||
const rr = (await loadVisualDev.release(opts.id)) as { code?: number; msg?: string }
|
||||
if (rr.code !== 200 && rr.code !== undefined) {
|
||||
log.error(`发布失败 code=${rr.code}: ${rr.msg}`)
|
||||
if (opts.json) printJson(rr)
|
||||
return ctx.fail(`发布失败 (code=${rr.code}): ${rr.msg}`, { id: opts.id, code: rr.code })
|
||||
}
|
||||
log.ok(`✓ 表单发布成功 (id=${opts.id})`)
|
||||
const summary = { id: opts.id, action: 'released' }
|
||||
if (opts.json) printJson(summary)
|
||||
return ctx.success(summary)
|
||||
}
|
||||
} else if (fd && typeof fd === 'object') {
|
||||
formData = fd as Record<string, unknown>
|
||||
}
|
||||
const existingFields = Array.isArray(formData.fields) ? (formData.fields as unknown[]) : []
|
||||
// 增量合并: 按 vmodel 去重, 新增追加
|
||||
const merged: unknown[] = [...existingFields]
|
||||
for (const f of parsed) {
|
||||
const fObj = f as Record<string, unknown>
|
||||
const idx = merged.findIndex((e) => (e as Record<string, unknown>).vmodel === fObj.vmodel)
|
||||
if (idx >= 0) merged[idx] = fObj
|
||||
else merged.push(fObj)
|
||||
}
|
||||
formData.fields = merged
|
||||
body.formData = JSON.stringify(formData)
|
||||
log.info(`字段合并: ${existingFields.length} → ${merged.length} (新增 ${parsed.length - (merged.length - existingFields.length)})`)
|
||||
}
|
||||
|
||||
// === step 3 PUT / 后端API ===
|
||||
// 4. PUT
|
||||
log.info(`loadVisualDev.update (formId=${opts.id})`)
|
||||
const r = (await loadVisualDev.update(body)) as { code?: number; msg?: string; data?: unknown }
|
||||
if (r.code !== 200 && r.code !== undefined) {
|
||||
const enhanced = enhanceFromBackend(r.msg || '', 'form', { entity: 'form' })
|
||||
log.error(` ${formatErrorLine(enhanced, { step: 3, stepName: 'PUT 后端API' })}`)
|
||||
if (opts.json) printJson(r)
|
||||
process.exit(1)
|
||||
}
|
||||
log.ok(`✓ 表单更新成功 (id=${opts.id})`)
|
||||
// === step 1 拉原 form / 读 current state ===
|
||||
const orig = (await loadVisualDev.getInfo(opts.id)) as Record<string, unknown>
|
||||
log.info(`原 form: enCode=${orig.enCode} fullName=${orig.fullName}`)
|
||||
|
||||
// 物理表不同步警告
|
||||
// form update --fields 改 formData/columnData 但不调 addTableFields, 物理表 schema 不会变
|
||||
// 如果 user 加了 FK 字段 (jnpfKey=relationForm) 但物理表缺对应 varchar 列, POST 数据会报 "Field cannot be empty"
|
||||
// 修复路径: form import --autoTable 重建物理表 / dm drop + form delete + form create 重建 / 手工 ALTER
|
||||
if (opts.fields) {
|
||||
try {
|
||||
// 对比 formData 新增字段 vs 物理表实际字段
|
||||
const { JnpfClient } = await import('../../core/client.ts')
|
||||
const { initWebapi: initWebapiDM, loadDataModel } = await import('../../webapi/dataModel.ts')
|
||||
const dc = await JnpfClient.fromToken()
|
||||
await initWebapiDM(dc)
|
||||
// 解析 --fields JSON 拿新增的 vModel
|
||||
let newVModels: string[] = []
|
||||
// Phase 3.1: dirty check (AI 场景增强)
|
||||
// ⚠️ 不是 web 镜像 (web state.isReload 不是 dirty check), 是 AI 场景补充
|
||||
// v2.2+ (2026-07-21): dirtyCheckFailAndExit 改 throw (DirtyCheckError), wrapCommand handler catch → ctx.fail
|
||||
const dirtyResult = dirtyCheck({
|
||||
expectedEnCode: opts.expectedEnCode,
|
||||
expectedState: opts.expectedState,
|
||||
force: opts.force,
|
||||
id: opts.id,
|
||||
current: {
|
||||
enCode: orig.enCode as string | undefined,
|
||||
state: orig.state as string | number | undefined,
|
||||
fullName: orig.fullName as string | undefined,
|
||||
type: 'form',
|
||||
},
|
||||
})
|
||||
try {
|
||||
const parsed = JSON.parse(opts.fields)
|
||||
if (Array.isArray(parsed)) newVModels = parsed.map((f: any) => f.vmodel || f.__vModel__).filter(Boolean)
|
||||
} catch { /* ignore parse error */ }
|
||||
// form enCode 查物理表
|
||||
const enCode = (orig as any).enCode
|
||||
if (enCode) {
|
||||
const dmList = (await loadDataModel.getDataModelList({ dbLinkId: orig.dbLinkId })) as { data?: { list?: { table: string; tableFieldList?: { field: string }[] }[] } }
|
||||
const tbl = dmList?.data?.list?.find((t) => t.table === enCode)
|
||||
const physicalFields = new Set((tbl?.tableFieldList || []).map((f) => f.field))
|
||||
const missing = newVModels.filter((v) => !physicalFields.has(v))
|
||||
if (missing.length > 0) {
|
||||
log.warn(` ⚠ 物理表未同步: form 新增 ${missing.length} 字段 (${missing.join(', ')}) 不在物理表 ${enCode} 中`)
|
||||
log.warn(` PUT form update 不同步物理表 schema (web 端不调 addTableFields)`)
|
||||
log.warn(` 修复: form import --autoTable 重建物理表 / dm drop + form delete + form create 重建 / 手工 ALTER`)
|
||||
log.warn(` 验证: dacli dm fields --table=${enCode} | grep -E "${missing.join('|')}"`)
|
||||
} else if (newVModels.length > 0) {
|
||||
log.info(` ℹ 物理表已包含 ${newVModels.length} 新增字段 (formData 与物理表一致)`)
|
||||
dirtyCheckFailAndExit(
|
||||
dirtyResult,
|
||||
{ type: 'form', id: opts.id, current: { enCode: orig.enCode as string, state: orig.state as string | number, fullName: orig.fullName as string } },
|
||||
log.error,
|
||||
)
|
||||
} catch (e) {
|
||||
if (e instanceof DirtyCheckError) {
|
||||
return ctx.fail(e.message, { id: opts.id, type: 'form', errorCount: e.errorCount })
|
||||
}
|
||||
throw e
|
||||
}
|
||||
if (dirtyResult.skipped) log.warn('⚠️ --force 已跳过 dirty check (强制覆盖)')
|
||||
|
||||
// 防御: enCode 冲突检测 (全平台唯一)
|
||||
if (opts.enCode && opts.enCode !== orig.enCode) {
|
||||
const list = (await loadVisualDev.getVisualDevList({ keyword: opts.enCode })) as {
|
||||
list?: { id: string; enCode: string }[]
|
||||
}
|
||||
const conflict = list.list?.find((f) => f.enCode === opts.enCode && f.id !== opts.id)
|
||||
if (conflict) {
|
||||
log.error(`BS-49: enCode=${opts.enCode} 已被其他 form 占用 (id=${conflict.id})`)
|
||||
log.info('建议: 加 _v2 后缀, 或 dacli form delete 后重建')
|
||||
return ctx.fail(`BS-49: enCode=${opts.enCode} 已被其他 form 占用`, { id: opts.id, conflictId: conflict.id })
|
||||
}
|
||||
}
|
||||
|
||||
// === step 2 合并更新 / PUT 语义, 未传字段保留 ===
|
||||
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
|
||||
if (opts.dbLinkId) body.dbLinkId = opts.dbLinkId
|
||||
if (opts.enableFlow !== undefined) body.enableFlow = parseInt(opts.enableFlow, 10)
|
||||
// Bug 3 修复: --flowId 实际是 workflow templateId, 走 workflow.update --enableMark=1 + start node formId=this.id
|
||||
// 流程-form 绑定: workflow 端 start node formId 字段 (TemplateNodeEntity.formId), 不是 form 端
|
||||
// 旧代码 body.flowId = opts.flowId 是错的, 后端 VisualdevEntity 无 flowId 字段, 会被 Lombok @Data 静默丢弃
|
||||
if (opts.flowId) {
|
||||
log.warn(`--flowId=${opts.flowId}: form 端无 flowId 字段 (后端 VisualdevEntity 不含)`)
|
||||
log.info(`正确的流程-form 绑定方式: workflow 端 start node formId = ${opts.id}`)
|
||||
log.info(` → dacli workflow update --id=${opts.flowId} --enableMark=1 (自动绑当前 form 到 start node)`)
|
||||
}
|
||||
if (opts.type) body.type = parseInt(opts.type, 10)
|
||||
if (opts.webType) body.webType = parseInt(opts.webType, 10)
|
||||
|
||||
// 3. 字段增量更新 (--fields)
|
||||
if (opts.fields) {
|
||||
let parsed: unknown[]
|
||||
try {
|
||||
parsed = JSON.parse(opts.fields)
|
||||
} catch (e) {
|
||||
log.error(`--fields JSON 解析失败: ${(e as Error).message}`)
|
||||
return ctx.fail(`--fields JSON 解析失败`, { id: opts.id, error: (e as Error).message?.slice(0, 100) })
|
||||
}
|
||||
if (!Array.isArray(parsed) || parsed.length === 0) {
|
||||
log.error('--fields 必是非空数组')
|
||||
return ctx.fail(`--fields 必是非空数组`, { id: opts.id })
|
||||
}
|
||||
// formData.fields 是字符串, 需解/合/重序列
|
||||
let formData: Record<string, unknown> = {}
|
||||
const fd = orig.formData
|
||||
if (typeof fd === 'string') {
|
||||
try {
|
||||
formData = JSON.parse(fd)
|
||||
} catch (e) {
|
||||
log.warn(`原 formData 不是有效 JSON, 用空对象`)
|
||||
formData = {}
|
||||
}
|
||||
} else if (fd && typeof fd === 'object') {
|
||||
formData = fd as Record<string, unknown>
|
||||
}
|
||||
const existingFields = Array.isArray(formData.fields) ? (formData.fields as unknown[]) : []
|
||||
// 增量合并: 按 vmodel 去重, 新增追加
|
||||
const merged: unknown[] = [...existingFields]
|
||||
for (const f of parsed) {
|
||||
const fObj = f as Record<string, unknown>
|
||||
const idx = merged.findIndex((e) => (e as Record<string, unknown>).vmodel === fObj.vmodel)
|
||||
if (idx >= 0) merged[idx] = fObj
|
||||
else merged.push(fObj)
|
||||
}
|
||||
formData.fields = merged
|
||||
body.formData = JSON.stringify(formData)
|
||||
log.info(`字段合并: ${existingFields.length} → ${merged.length} (新增 ${parsed.length - (merged.length - existingFields.length)})`)
|
||||
}
|
||||
|
||||
// === step 3 PUT / 后端API ===
|
||||
log.info(`loadVisualDev.update (formId=${opts.id})`)
|
||||
const r = (await loadVisualDev.update(body)) as { code?: number; msg?: string; data?: unknown }
|
||||
if (r.code !== 200 && r.code !== undefined) {
|
||||
const enhanced = enhanceFromBackend(r.msg || '', 'form', { entity: 'form' })
|
||||
log.error(` ${formatErrorLine(enhanced, { step: 3, stepName: 'PUT 后端API' })}`)
|
||||
if (opts.json) printJson(r)
|
||||
return ctx.fail(`form update 失败 (code=${r.code}): ${r.msg}`, { id: opts.id, code: r.code })
|
||||
}
|
||||
log.ok(`✓ 表单更新成功 (id=${opts.id})`)
|
||||
|
||||
// 物理表不同步警告
|
||||
// form update --fields 改 formData/columnData 但不调 addTableFields, 物理表 schema 不会变
|
||||
// 如果 user 加了 FK 字段 (jnpfKey=relationForm) 但物理表缺对应 varchar 列, POST 数据会报 "Field cannot be empty"
|
||||
// 修复路径: form import --autoTable 重建物理表 / dm drop + form delete + form create 重建 / 手工 ALTER
|
||||
if (opts.fields) {
|
||||
try {
|
||||
// 对比 formData 新增字段 vs 物理表实际字段
|
||||
const { initWebapi: initWebapiDM, loadDataModel } = await import('../../webapi/dataModel.ts')
|
||||
const dc = await JnpfClient.fromToken()
|
||||
await initWebapiDM(dc)
|
||||
// 解析 --fields JSON 拿新增的 vModel
|
||||
let newVModels: string[] = []
|
||||
try {
|
||||
const parsed = JSON.parse(opts.fields)
|
||||
if (Array.isArray(parsed)) newVModels = parsed.map((f: any) => f.vmodel || f.__vModel__).filter(Boolean)
|
||||
} catch { /* ignore parse error */ }
|
||||
// form enCode 查物理表
|
||||
const enCode = (orig as any).enCode
|
||||
if (enCode) {
|
||||
const dmList = (await loadDataModel.getDataModelList({ dbLinkId: orig.dbLinkId })) as { data?: { list?: { table: string; tableFieldList?: { field: string }[] }[] } }
|
||||
const tbl = dmList?.data?.list?.find((t) => t.table === enCode)
|
||||
const physicalFields = new Set((tbl?.tableFieldList || []).map((f) => f.field))
|
||||
const missing = newVModels.filter((v) => !physicalFields.has(v))
|
||||
if (missing.length > 0) {
|
||||
log.warn(` ⚠ 物理表未同步: form 新增 ${missing.length} 字段 (${missing.join(', ')}) 不在物理表 ${enCode} 中`)
|
||||
log.warn(` PUT form update 不同步物理表 schema (web 端不调 addTableFields)`)
|
||||
log.warn(` 修复: form import --autoTable 重建物理表 / dm drop + form delete + form create 重建 / 手工 ALTER`)
|
||||
log.warn(` 验证: dacli dm fields --table=${enCode} | grep -E "${missing.join('|')}"`)
|
||||
} else if (newVModels.length > 0) {
|
||||
log.info(` ℹ 物理表已包含 ${newVModels.length} 新增字段 (formData 与物理表一致)`)
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
log.debug(` (物理表同步检查失败: ${(e as Error).message?.slice(0, 80)})`)
|
||||
}
|
||||
}
|
||||
|
||||
// === step 4 防护 / PUT 后自动重新发布 ===
|
||||
if (opts.autoRelease) {
|
||||
log.info(`PUT 后自动 Actions/Release (BS-53 防护)`)
|
||||
const rr = (await loadVisualDev.release(opts.id)) as { code?: number; msg?: string }
|
||||
if (rr.code === 200 || rr.code === undefined) log.ok(`✓ 已自动重新发布 (state 1/2 → 1)`)
|
||||
else log.warn(`自动重新发布失败 code=${rr.code}: ${rr.msg} — 手动跑 'dacli form release ${opts.id}'`)
|
||||
}
|
||||
|
||||
const summary = { id: opts.id, action: 'updated' }
|
||||
if (opts.json) printJson(summary)
|
||||
ctx.success(summary)
|
||||
} catch (e) {
|
||||
log.debug(` (物理表同步检查失败: ${(e as Error).message?.slice(0, 80)})`)
|
||||
ctx.fail(`form update 失败: ${(e as Error).message?.slice(0, 200)}`)
|
||||
}
|
||||
}
|
||||
|
||||
// === step 4 防护 / PUT 后自动重新发布 ===
|
||||
// 5. 防护: PUT 后自动重新发布
|
||||
if (opts.autoRelease) {
|
||||
log.info(`PUT 后自动 Actions/Release (BS-53 防护)`)
|
||||
const rr = (await loadVisualDev.release(opts.id)) as { code?: number; msg?: string }
|
||||
if (rr.code === 200 || rr.code === undefined) log.ok(`✓ 已自动重新发布 (state 1/2 → 1)`)
|
||||
else log.warn(`自动重新发布失败 code=${rr.code}: ${rr.msg} — 手动跑 'dacli form release ${opts.id}'`)
|
||||
}
|
||||
|
||||
if (opts.json) printJson({ id: opts.id, action: 'updated' })
|
||||
})
|
||||
})
|
||||
|
||||
installHelpHookWithPatterns(cmd, 'form update', {
|
||||
@@ -275,4 +297,4 @@ export function buildFormUpdateCommand(): Command {
|
||||
],
|
||||
})
|
||||
return cmd
|
||||
}
|
||||
}
|
||||
@@ -51,10 +51,12 @@ describe('report/create.ts 三层接入契约', () => {
|
||||
expect(src).toMatch(/fetchExistingLists/)
|
||||
})
|
||||
|
||||
it('import setupOutputFilter + addOutputOptions + printCommandEnd (输出过滤)', () => {
|
||||
it('import setupOutputFilter + addOutputOptions + wrapCommand (输出过滤 + v2.2 收口)', () => {
|
||||
expect(src).toMatch(/import\s+\{[^}]*setupOutputFilter[^}]*\}\s+from\s+['"]\.\.\/_output_filter\.ts['"]/)
|
||||
expect(src).toMatch(/addOutputOptions/)
|
||||
expect(src).toMatch(/printCommandEnd/)
|
||||
expect(src).toMatch(/wrapCommand/)
|
||||
// v2.2+ (2026-07-21): wrapCommand 替代 printCommandEnd 散布, 不应再显式调用
|
||||
expect(src).not.toMatch(/printCommandEnd\(/)
|
||||
})
|
||||
|
||||
it('调用 setupOutputFilter(opts) 在 action 入口', () => {
|
||||
@@ -76,20 +78,16 @@ describe('report/create.ts 三层接入契约', () => {
|
||||
expect(src).not.toMatch(/import\s+\{[^}]*autoStripEnCode[^}]*\}\s+from\s+['"]\.\.\/_encode_validator\.ts['"]/)
|
||||
})
|
||||
|
||||
it('printCommandEnd 至少调用 6 次 (1 success + 5 failure paths)', () => {
|
||||
const matches = src.match(/printCommandEnd\(\{/g) || []
|
||||
expect(matches.length).toBeGreaterThanOrEqual(6)
|
||||
it('wrapCommand 收口 5 处 ctx.fail (替代 5 处 process.exit + 5 处 printCommandEnd)', () => {
|
||||
const failMatches = src.match(/ctx\.fail\(/g) || []
|
||||
expect(failMatches.length).toBeGreaterThanOrEqual(5)
|
||||
})
|
||||
|
||||
it('成功路径 printCommandEnd 不带 result (默认 success)', () => {
|
||||
// 找最后一个 printCommandEnd 调用, 应该是 success 路径
|
||||
const lastIdx = src.lastIndexOf('printCommandEnd({')
|
||||
expect(lastIdx).toBeGreaterThan(-1)
|
||||
const successBlock = src.slice(lastIdx)
|
||||
expect(successBlock).toMatch(/command:\s*['"]report create['"]/)
|
||||
expect(successBlock).toMatch(/summary:\s*\{/)
|
||||
// 不应有 result: 'failure' (默认是 success)
|
||||
expect(successBlock).not.toMatch(/result:\s*['"]failure['"]/)
|
||||
it('成功路径 ctx.success (默认 success, 替代旧版 success printCommandEnd)', () => {
|
||||
expect(src).toMatch(/ctx\.success\(/)
|
||||
// 成功路径不应在 ctx.success 前调用 ctx.fail
|
||||
const lastSuccess = src.lastIndexOf('ctx.success(')
|
||||
expect(lastSuccess).toBeGreaterThan(-1)
|
||||
})
|
||||
|
||||
it('interfase CreateOpts 含 verbose 字段', () => {
|
||||
|
||||
Reference in new issue
Block a user