Files
hose-use/src/cli/mutation.ts
T
giteaandClaude 5be2bd5432 fix(cli): 统一生产环境确认协议与预览行为
- 新增 src/cli/production.ts:--allow-prod 为唯一生产覆盖参数,
  --allow-production / --force 仅作兼容别名;JSON 阻断统一为
  PROD_OVERRIDE_REQUIRED,并携带 nextAction 与 details.operation
- workflow test 与 workflow nodes grant 统一提示文案、错误结构与
  nextAction 生成;workflow test 的 JSON 阻断不再返回空 stdout
- 预览 --dry-run 始终放行,实际执行才要求覆盖参数;nodes grant
  参数校验前置,缺少节点类型时不再先报生产阻断
- 两个命令的成功结果统一携带 productionOverride / productionWarning
- corps tag 将企业从 prod 降级为 test/demo 时输出显著警告
- 同步 CLI 帮助、CLI/workflow README 与 Skill;补充对应单测

Co-Authored-By: Claude <noreply@anthropic.com>
2026-11-18 16:44:57 +08:00

107 lines
3.5 KiB
TypeScript

import { booleanOption } from "./parser.js";
import type { CliParsedArgs, CliResult, CliRuntime } from "./types.js";
import { CliError } from "./errors.js";
/**
* All mutating CLI commands use the same two-step contract:
* 1. --dry-run only describes the intended change;
* 2. --yes/--confirm is required before the real operation runs.
*
* The parsed-argument fallback keeps command handlers easy to unit-test without
* requiring every hand-built CliRuntime to include the newer fields.
*/
export class MutationConfirmationRequired extends CliError {
readonly operation: string;
constructor(operation: string, nextAction: string) {
super(
`拒绝执行${operation}:该操作会改变数据。请先使用 --dry-run 预览,确认后追加 --yes(或 --confirm)。`,
10,
nextAction
);
this.name = "MutationConfirmationRequired";
this.operation = operation;
}
}
export function isDryRun(parsed: CliParsedArgs, runtime: CliRuntime): boolean {
return (Object.hasOwn(runtime, "dryRun") && runtime.dryRun === true) || booleanOption(parsed.options, ["dry-run", "dryRun", "dryrun"]);
}
export function isConfirmed(parsed: CliParsedArgs, runtime: CliRuntime): boolean {
return (
(Object.hasOwn(runtime, "confirmed") && runtime.confirmed === true) ||
booleanOption(parsed.options, ["yes", "y", "confirm", "force-write"])
);
}
export function requireMutationConfirmation(
parsed: CliParsedArgs,
runtime: CliRuntime,
operation: string,
nextAction: string
): void {
if (isDryRun(parsed, runtime) || isConfirmed(parsed, runtime)) return;
throw new MutationConfirmationRequired(operation, nextAction);
}
/**
* 基于当前原始参数构造「确认执行」命令,并补齐缺失的附加参数。
* 所有命令的 nextAction 都走这里,避免各自拼接造成格式漂移。
*/
export function buildConfirmedCommand(parsed: CliParsedArgs, extraArgs: string[] = []): string {
const strip = new Set([
"--dry-run", "--dryRun", "--dryrun",
"--yes", "-y", "--confirm", "--force-write", "--forceWrite",
]);
const args = (parsed.raw ?? []).filter((arg) => !strip.has(arg));
const isAiPro = args[0] === "hose-aipro" || args[0] === "aipro";
const cleanArgs =
args[0] === "hose-aipro" || args[0] === "hose-use" || args[0] === "aipro"
? args.slice(1)
: args;
const extra = extraArgs.filter((arg) => arg && !cleanArgs.includes(arg));
return `${isAiPro ? "hose-aipro" : "hose-use"} ${[...cleanArgs, ...extra].join(" ")} --yes`;
}
export function mutationPreview(
parsed: CliParsedArgs,
runtime: CliRuntime,
operation: string,
target: string,
changes: Record<string, unknown>,
options: { extraArgs?: string[] } = {}
): CliResult {
const nextAction = buildConfirmedCommand(parsed, options.extraArgs);
const payload = {
dryRun: true,
operation,
target,
changes,
nextAction,
};
if (runtime.jsonMode) {
return {
stdout: `${JSON.stringify(payload, null, 2)}\n`,
stderr: "",
exitCode: 0,
};
}
const lines = [
"[DRY-RUN] 未执行任何写操作。",
`操作: ${operation}`,
`目标: ${target}`,
"变更:",
...Object.entries(changes).map(([key, value]) => ` ${key}: ${formatPreviewValue(value)}`),
`下一步: ${nextAction}`,
];
return { stdout: `${lines.join("\n")}\n`, stderr: "", exitCode: 0 };
}
function formatPreviewValue(value: unknown): string {
if (typeof value === "string") return value;
return JSON.stringify(value);
}