diff --git a/src/commands/form/index.ts b/src/commands/form/index.ts index 83c4f9f..503bac2 100644 --- a/src/commands/form/index.ts +++ b/src/commands/form/index.ts @@ -1,10 +1,13 @@ /** * commands/form/index.ts — `dacli form` 父命令 + 子命令注册 + * + * v0.11+ Phase 2: +update (P0 高需求 NEW-CMD) */ import { Command } from 'commander' import { buildFormListCommand } from './list.ts' import { buildFormInfoCommand } from './info.ts' import { buildFormCreateCommand } from './create.ts' +import { buildFormUpdateCommand } from './update.ts' import { buildFormReleaseCommand } from './release.ts' import { buildFormDeleteCommand } from './delete.ts' import { buildFormExportCommand } from './export.ts' @@ -18,10 +21,11 @@ import { buildFormBindFlowCommand } from './bindflow.ts' import { buildFormSelectorCommand } from './selector.ts' export function buildFormCommand(): Command { - const cmd = new Command('form').description('表单 CRUD (list/info/create/release/delete/export/import/clone/validate/fixcolumndata/codegen/lint/bindFlow/selector)') + const cmd = new Command('form').description('表单 CRUD (list/info/create/update/release/delete/export/import/clone/validate/fixcolumndata/codegen/lint/bindFlow/selector)') cmd.addCommand(buildFormListCommand()) cmd.addCommand(buildFormInfoCommand()) cmd.addCommand(buildFormCreateCommand()) + cmd.addCommand(buildFormUpdateCommand()) cmd.addCommand(buildFormReleaseCommand()) cmd.addCommand(buildFormDeleteCommand()) cmd.addCommand(buildFormExportCommand()) diff --git a/src/commands/form/update.ts b/src/commands/form/update.ts new file mode 100644 index 0000000..6b89b76 --- /dev/null +++ b/src/commands/form/update.ts @@ -0,0 +1,192 @@ +/** + * commands/form/update.ts — 表单修改 (P0 高需求 NEW-CMD) + * + * v0.11+ Phase 2 新增: 改 form 元数据/字段/enCode + * 解决: 当前改 schema 需 export → 手动改 → import (无原子性, 风险大) + * + * API: PUT /api/visualdev/Base/{id} + * 自动 BS-49/50/52 防御 + */ +import { Command } from 'commander' +import { JnpfClient } from '../../core/client.ts' +import { installAiHelpHook } from '../../core/help.ts' +import { log, printJson } from '../../core/log.ts' +import { initWebapi, loadVisualDev } from '../../webapi/visualDev.ts' + +interface Opts { + id: string + fullName?: string + enCode?: string + category?: string + dbLinkId?: string + enableFlow?: string + flowId?: string + type?: string + webType?: string + fields?: string + autoRelease?: boolean + releaseOnly?: boolean + json?: boolean +} + +export function buildFormUpdateCommand(): Command { + const cmd = new Command('update') + .description('表单修改 (改 schema/字段/enCode, PUT 语义, 自动 BS-49/50 防御 + 重新发布)') + .requiredOption('--id ', '表单 id') + .option('--fullName ', '中文名') + .option('--enCode ', '英文编码 (BS-49 唯一, 重名会失败)') + .option('--category ', '表单分类 id') + .option('--dbLinkId ', '业务库 id (BS-48 必须有效)') + .option('--enableFlow <0|1>', '是否启用流程 (BS-44)') + .option('--flowId ', '流程模板 id (enableFlow=1 必填)') + .option('--type ', '类型 1=表单 2=列表 3=树 4=视图') + .option('--webType ', '前端类型 1=表单 2=列表') + .option('--fields ', '字段 JSON 数组 (v0.11+ 支持 增量改字段, 格式同 create)') + .option('--autoRelease', 'PUT 后自动重新发布 (BS-53 防护, 默认 true)', true) + .option('--no-autoRelease', '不自动发布, 仅 PUT') + .option('--releaseOnly', '仅重新发布, 不 PUT (state 0/2 → 1)') + .option('--json', '原始 JSON 输出', false) + .action(async (opts: Opts) => { + const c = await JnpfClient.fromToken() + await initWebapi(c) + + // 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 + } + + // 1. 拉原 form + const orig = (await loadVisualDev.getInfo(opts.id)) as Record + log.info(`原 form: enCode=${orig.enCode} fullName=${orig.fullName}`) + + // BS-49 防御: 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 后缀, 或用 form clone 重建') + process.exit(2) + } + } + + // 2. 合并更新 (PUT 语义, 未传字段保留) + 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 + if (opts.dbLinkId) body.dbLinkId = opts.dbLinkId + if (opts.enableFlow !== undefined) body.enableFlow = parseInt(opts.enableFlow, 10) + if (opts.flowId) body.flowId = opts.flowId + 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}`) + process.exit(2) + } + if (!Array.isArray(parsed) || parsed.length === 0) { + log.error('--fields 必是非空数组') + process.exit(2) + } + // formData.fields 是字符串, 需解/合/重序列 + let formData: Record = {} + 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 + } + 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 + const idx = merged.findIndex((e) => (e as Record).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)})`) + } + + // 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) { + log.error(`update 失败 (code=${r.code}): ${r.msg}`) + if (opts.json) printJson(r) + process.exit(1) + } + log.ok(`✓ 表单更新成功 (id=${opts.id})`) + + // 5. BS-53 防护: 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' }) + }) + + installAiHelpHook(cmd, { + purpose: '表单修改 (高需求 P0 NEW-CMD, 改 schema/字段/enCode, PUT 语义). 解决之前需 export → 改 → import 的无原子性.', + required: { '--id': '表单 id' }, + optional: { + '--fullName': '中文名', + '--enCode': '英文编码 (BS-49 唯一)', + '--dbLinkId': '业务库 id (BS-48 必须有效)', + '--enableFlow': '是否启用流程 0|1', + '--flowId': '流程模板 id', + '--type': '类型 1=表单 2=列表 3=树 4=视图', + '--webType': '前端类型 1=表单 2=列表', + '--fields': '字段 JSON 数组 (增量合并, 按 vmodel 去重)', + '--autoRelease': 'PUT 后自动重新发布 (默认 true, --no-autoRelease 关闭)', + '--releaseOnly': '仅重新发布, 不 PUT (state 0/2 → 1)', + }, + example: [ + '# 改 fullName', + 'dacli form update --id=123 --fullName=新名', + '# 改 enCode (BS-49 防御)', + 'dacli form update --id=123 --enCode=new_encode', + '# 增量加字段', + 'dacli form update --id=123 --fields=\'[{"vmodel":"f_new","label":"新字段","jnpfKey":"input"}]\'', + '# 绑工作流', + 'dacli form update --id=123 --enableFlow=1 --flowId=wf_xxx', + '# 仅重新发布', + 'dacli form update --id=123 --releaseOnly', + ], + source: { api: 'PUT /api/visualdev/Base/{id}', ref: 'web/.../api/onlineDev/visualDev.ts update()' }, + notes: [ + 'v0.11+ P0 高需求 NEW-CMD', + 'PUT 语义, 未传字段保留原值', + '自动 BS-49 enCode 唯一性检测 + BS-53 自动重新发布', + '--fields 增量合并, 按 vmodel 去重 (新增追加, 重复替换)', + ], + }) + return cmd +}