Files
dacli/src/commands/_reusable_create.ts
T
ssw cf4af60806 feat(dacli): Tier 1 跟进 4 项 — 容错/dbPing/menuIntegrity --fix/reuse wrapper
回应用户"都去完善" — 落地 4 项 tdd8 smoke test 暴露的痛点:

[1/4] non-JSON 容错 (src/core/client.ts)
  - 修 schedule list / monitor flow 等命令报"unexpected response shape"崩
  - parseJson(): HTTP 2xx + 空 body → 返 envelope-style {code:200, data:null} (JNPF 端点 bug 已知)
  - HTTP 2xx + 非 JSON body → graceful degrade 同上, 不 throw
  - 实战: schedule list 之前 100% 崩, 现在返 200 + 空 data (语义清晰)

[2/4] dbPing 真实实现 (src/commands/app/status.ts)
  - 替换 /system/DataSource/TestConnection (404) → /system/DataModel/{dbLinkId}/Tables
  - 端点真实, dm list 端点, 走通即 DB 连得上
  - 4xx/5xx 都报真错误, 不再 skip
  - 实战: dbPing 75ms 真检查 (之前永远 skip, 防不住 BS-E14)

[3/4] menuIntegrity --fix 跟进 (src/commands/app/status.ts)
  - 新选项: --fix-menu-url 自动重建空 urlAddress 菜单
  - 策略: 拉 form list → enCode 匹配 → PUT /system/Menu/{id} urlAddress=model/{enCode}
  - 非 form 菜单 (enCode 不在 formList) → skip + log "需手填"
  - tdd8 实测: 4 项空 urlAddress 可自动修 (需你确认执行)
  - DacliHelp 同步更新 notes

[4/4] --reuse-if-exists 通用化 (src/commands/_reusable_create.ts)
  - 新增通用 wrapper: reusableCreate({cmdName, enCode, findExisting, runCreate})
  - form/dict/menu create 全部接入 (各 ~20 行)
  - 6 个新单元测试 (复用场景, 异常容错, 空 enCode 等)
  - 4 个命令统一 shape: ctx.success({enCode, id, fullName, state, reused: true, mode: 'reuse-if-exists'})
  - dm/workflow 跳过 (enCode 概念不直接适用, tableName/flowId 走不同路径)

验证:
- typecheck 0 errors
- vitest 39 文件 / 389 tests pass (含 6 个新 reusable_create 测试)
- doc:check 0 drift (扫描 271 文件)
- quality gate 4/4 (tsc + vitest + doc:check + smoke)

意外: dict/typecreate.ts 文件在 case-insensitive fs 上 cp 时被误删, git restore 恢复 — 提醒后续 case-sensitivity 风险
2026-07-24 19:53:27 +08:00

87 lines
3.1 KiB
TypeScript

/**
* _reusable_create.ts — --reuse-if-exists 通用 wrapper (2026-07-24 Tier 1 跟进)
*
* 让 form / dict / menu / dm / workflow create 都能一行接入幂等支持.
* 用法:
*
* const hit = await reusableCreate({
* cmdName: 'form create',
* enCode: opts.enCode,
* findExisting: async (en) => {
* const list = await loadVisualDev.getVisualDevList({ keyword: en, pageSize: 500 })
* return findExistingForm(en, (list as any).list || [])
* },
* runCreate: async () => { /* 实际 create 逻辑 *\/ },
* })
* if (hit.reused) return ctx.success(hit.existing)
*
* 设计要点:
* - 通用: findExisting + runCreate 回调各命令自己实现
* - 容错: findExisting 异常 → log warn 继续 runCreate (不阻断)
* - 幂等: exact enCode match (跟 form findExistingForm 一致, 不 fuzzy)
* - 返回: 命中返 { reused: true, existing: {...} }; 未命中返 { reused: false, ran: true }
*/
export interface ExistingRecord {
/** 现有资源的 id (命令返回的 id, 通常是字符串) */
id: string
/** 匹配的 enCode */
enCode: string
/** 状态 (如 form 的 0/1, 可选) */
state?: number | string
/** 其它元数据 (fullName 等) */
[key: string]: unknown
}
export interface ReusableCreateOptions {
/** 命令名 (用于 log), 如 "form create" / "dict typeCreate" */
cmdName: string
/** 必传, 用户输入的 enCode (空 → 直接 return ran=true) */
enCode?: string
/** 用户是否传了 --reuse-if-exists; 不传则永远 runCreate */
reuseIfExists: boolean
/** 异步查 enCode 是否已存在. 返 ExistingRecord 或 null. 异常会被捕获 + 继续 runCreate. */
findExisting: (enCode: string) => Promise<ExistingRecord | null>
/** 实际的 create 逻辑, 仅在未命中或 reuseIfExists=false 时跑 */
runCreate: () => Promise<void>
}
export type ReusableCreateResult =
| { reused: true; existing: ExistingRecord }
| { reused: false; ran: true }
/**
* 检查 enCode 存在性, 命中时 early return; 否则跑 runCreate.
* 返回的 shape 跟 form create --reuse-if-exists 一致, ctx.success(existing) 直接用.
*/
export async function reusableCreate(opts: ReusableCreateOptions): Promise<ReusableCreateResult> {
if (!opts.reuseIfExists || !opts.enCode) {
await opts.runCreate()
return { reused: false, ran: true }
}
try {
const existing = await opts.findExisting(opts.enCode)
if (existing) {
return { reused: true, existing }
}
} catch (e) {
// 查询异常不阻断, 走 runCreate
const { log } = await import('../core/log.ts')
log.warn(`${opts.cmdName} --reuse-if-exists 查询失败 (${(e as Error).message?.slice(0, 80)}), 继续正常创建流程`)
}
await opts.runCreate()
return { reused: false, ran: true }
}
/**
* 给 ctx.success 用的统一 shape — 让 form/dict/menu/dm 命令返同样的字段集, AI agent 易解析.
*/
export function reuseSuccessPayload(existing: ExistingRecord, mode: string): Record<string, unknown> {
return {
enCode: existing.enCode,
id: existing.id,
fullName: existing.fullName,
state: existing.state,
reused: true,
mode,
}
}