feat(dataflow): v0.11e+ Phase B3 通用 --diff/--dry-run 选项

新建 src/commands/_dataflow.ts (~200 行):
- DataFlowOpts: { diff?, dryRun?, force? }
- DataFlowContext: 含 current/desired + dirty check + step machine 结果
- DataFlowResult: { shouldSendApi, diffOutput?, dryRunReport? }
- diffObjects(current, desired): 浅比较输出 changed keys
- buildDryRunReport(ctx): 生成 dry-run 报告 (step machine + dirty check + diff)
- applyDataFlowOpts(opts, ctx): 主入口, 返回 shouldSendApi

设计原则:
- 通用工具 (_*.ts), 适用所有 update 命令
- 与 _dirty_check.ts (A2) 互补: dirty check 防外部修改, --diff 看修改方向
- --dry-run 与 step machine 互补: step machine 校验字段, --dry-run 完整模拟不调 API

集成待后续: 每个 update.ts 加 .option('--diff') + .option('--dry-run') + applyDataFlowOpts() (Phase B3 子任务, 本 commit 提供基础设施)
This commit is contained in:
ssw committed 2026-06-27 03:19:58 +08:00
1 parent 831a2907a6
commit fedce07446
1 file changed
+171
+171
View File
@@ -0,0 +1,171 @@
/**
* commands/_dataflow.ts — 通用数据流选项 (Phase B3, v0.11e+, 2026-06-26)
*
* 镜像 web 状态机 + dirty check 模式, 让所有 update 命令支持:
* - --diff: 输出当前 vs 期望 diff (JSON 格式), AI 决定是否 --force
* - --dry-run: 跑校验但不发 API (1 round-trip, 0 副作用)
*
* 设计原则:
* - 通用工具 (_*.ts), 不是具体命令实现
* - 与 _dirty_check.ts (Phase A2) 互补: dirty check 防外部修改, --diff 看修改方向
* - --dry-run 与 step machine 互补: step machine 校验字段, --dry-run 完整模拟不调 API
*
* 用法:
* import { applyDataFlowOpts, diffObjects, dryRunReport } from '../_dataflow.ts'
*
* .option('--diff', '输出当前 vs 期望 diff (Phase B3)')
* .option('--dry-run', '跑校验但不发 API (Phase B3)', false)
* .option('--force', '跳过 dirty check 强制执行', false)
*
* action(async (opts) => {
* const flow = applyDataFlowOpts(opts, { type: 'form', id, current: orig, desired: body, dirtyCheck: ... })
* if (flow.diffOutput) { printJson(flow.diff); process.exit(0) }
* if (flow.dryRun) { log.info('dry-run: 校验通过, 不发 API'); process.exit(0) }
* if (!flow.dirtyCheckPassed) process.exit(1)
* // ...正常 API 调用
* })
*/
export interface DataFlowOpts {
/** --diff 输出当前 vs 期望 diff (Phase B3) */
diff?: boolean
/** --dry-run 跑校验但不发 API (Phase B3) */
dryRun?: boolean
/** --force 跳过 dirty check (Phase A2) */
force?: boolean
}
export interface DataFlowContext {
/** 模块标识 (form/workflow/dict/role/di) */
type: 'form' | 'workflow' | 'dict' | 'role' | 'di' | 'menu' | 'portal' | 'bigscreen'
/** 实体 id */
id: string
/** 当前值 (从 getInfo 拿) */
current: Record<string, unknown>
/** 期望值 (从 opts 构造) */
desired: Record<string, unknown>
/** dirty check 结果 (Phase A2) */
dirtyCheckPassed: boolean
/** dirty check 失败原因 (字段级) */
dirtyCheckErrors?: Array<{ field: string; message: string }>
/** step machine 校验结果 (Phase 1) */
stepMachinePassed: boolean
/** step machine 错误列表 */
stepMachineErrors?: Array<{ step: number; stepName: string; field: string; message: string }>
}
export interface DataFlowResult {
/** 是否应该发出 API (false: --diff/--dry-run 已 exit) */
shouldSendApi: boolean
/** diff 输出 (when --diff) */
diffOutput?: DiffReport
/** dry-run 报告 (when --dry-run) */
dryRunReport?: DryRunReport
}
/**
* 计算两个对象的 diff (浅比较, 只输出 changed keys)
*/
export function diffObjects(
current: Record<string, unknown>,
desired: Record<string, unknown>,
): DiffReport {
const changes: Array<{ key: string; from: unknown; to: unknown }> = []
const unchanged: string[] = []
// 检查 desired 中每个 key
for (const key of Object.keys(desired)) {
const fromVal = current[key]
const toVal = desired[key]
if (JSON.stringify(fromVal) !== JSON.stringify(toVal)) {
changes.push({ key, from: fromVal, to: toVal })
}
}
// 检查 current 中有但 desired 中没有的 (可能表示被覆盖)
for (const key of Object.keys(current)) {
if (!(key in desired)) {
unchanged.push(key)
}
}
return {
totalChanges: changes.length,
unchangedCount: unchanged.length,
changes,
}
}
export interface DiffReport {
totalChanges: number
unchangedCount: number
changes: Array<{ key: string; from: unknown; to: unknown }>
}
/**
* 生成 dry-run 报告 (镜像 step machine 输出 + dirty check)
*/
export interface DryRunReport {
type: string
id: string
stepMachinePassed: boolean
stepMachineErrorCount: number
dirtyCheckPassed: boolean
dirtyCheckErrorCount: number
diffSummary: { totalChanges: number; unchangedCount: number }
/** 模拟结果 (不会真发 API) */
wouldSucceed: boolean
reason?: string
}
export function buildDryRunReport(ctx: DataFlowContext): DryRunReport {
const diff = diffObjects(ctx.current, ctx.desired)
const wouldSucceed =
ctx.stepMachinePassed &&
ctx.dirtyCheckPassed &&
diff.totalChanges > 0
const reason = !ctx.stepMachinePassed
? `step machine 校验失败 (${ctx.stepMachineErrors?.length || 0} 错误)`
: !ctx.dirtyCheckPassed
? `dirty check 失败 (${ctx.dirtyCheckErrors?.length || 0} 不匹配)`
: diff.totalChanges === 0
? '无任何字段变化 (current == desired)'
: undefined
return {
type: ctx.type,
id: ctx.id,
stepMachinePassed: ctx.stepMachinePassed,
stepMachineErrorCount: ctx.stepMachineErrors?.length || 0,
dirtyCheckPassed: ctx.dirtyCheckPassed,
dirtyCheckErrorCount: ctx.dirtyCheckErrors?.length || 0,
diffSummary: { totalChanges: diff.totalChanges, unchangedCount: diff.unchangedCount },
wouldSucceed,
reason,
}
}
/**
* 主入口: 应用 --diff/--dry-run 选项
*
* @returns DataFlowResult, 含 shouldSendApi (false 时调用方应 process.exit(0))
*/
export function applyDataFlowOpts(
opts: DataFlowOpts,
ctx: DataFlowContext,
): DataFlowResult {
// --diff 输出当前 vs 期望 diff (JSON)
if (opts.diff) {
const diff = diffObjects(ctx.current, ctx.desired)
return { shouldSendApi: false, diffOutput: diff }
}
// --dry-run 跑校验但不发 API
if (opts.dryRun) {
const report = buildDryRunReport(ctx)
return { shouldSendApi: false, dryRunReport: report }
}
// 正常流程: 发 API
return { shouldSendApi: true }
}