90 lines
3.1 KiB
TypeScript
90 lines
3.1 KiB
TypeScript
import { createWorkflow } from "../../../domains/platform/services.js";
|
|
import { stringOption } from "../../parser.js";
|
|
import { printJson, printKeyValue } from "../../printer.js";
|
|
import { isDryRun, mutationPreview, requireMutationConfirmation } from "../../mutation.js";
|
|
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
|
|
import { createWorkflowDetailClient } from "./shared.js";
|
|
|
|
export async function handleCreate(
|
|
parsed: CliParsedArgs,
|
|
runtime: CliRuntime
|
|
): Promise<CliResult> {
|
|
const name = parsed.args[0] || stringOption(parsed.options, ["name", "label", "n"]);
|
|
const trigger = stringOption(parsed.options, ["trigger", "t"]);
|
|
const platformId = stringOption(parsed.options, ["platform-id", "platformId", "app-id", "appId"]);
|
|
const file = stringOption(parsed.options, ["file", "f"]);
|
|
const desc = stringOption(parsed.options, ["desc", "description", "d"]);
|
|
|
|
requireMutationConfirmation(
|
|
parsed,
|
|
runtime,
|
|
"创建工作流",
|
|
`hose-use workflow create${name ? ` --name="${name}"` : ""}${trigger ? ` --trigger=${trigger}` : ""}${platformId ? ` --platform-id=${platformId}` : ""}${file ? ` --file=${file}` : ""} --yes`
|
|
);
|
|
|
|
if (isDryRun(parsed, runtime)) {
|
|
return mutationPreview(parsed, runtime, "创建工作流", name || "未命名流程", {
|
|
name: name || "未命名流程",
|
|
trigger: trigger || (file ? "from-file" : "none"),
|
|
platformId: platformId || "-",
|
|
fromFile: file || "-",
|
|
initialState: 0,
|
|
});
|
|
}
|
|
|
|
const { client } = await createWorkflowDetailClient(parsed, runtime, "");
|
|
|
|
const result = await createWorkflow(client, {
|
|
label: name,
|
|
trigger,
|
|
platformId,
|
|
file,
|
|
desc,
|
|
});
|
|
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson(result, runtime.outputOptions),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const triggerNode = result.tasks?.[0];
|
|
const triggerSummary = triggerNode ? `${triggerNode.label || triggerNode.name} (${triggerNode.type})` : "无首节点 (需在设计器配置)";
|
|
const webhookUrl = triggerNode?.type === "webhook" && (triggerNode as any).urlPath
|
|
? `https://piper.ekuaibao.com/api/webhook/${(triggerNode as any).urlPath}`
|
|
: undefined;
|
|
|
|
const kvData: Record<string, string> = {
|
|
"新工作流 ID (pipelineId)": result.id,
|
|
"工作流名称": result.label,
|
|
"自建应用 ID (platformId)": String((result as any).platformId || "-"),
|
|
"首节点触发器": triggerSummary,
|
|
};
|
|
|
|
if (webhookUrl) {
|
|
kvData["Webhook 调用地址"] = webhookUrl;
|
|
}
|
|
|
|
kvData["节点总数"] = String(result.tasks?.length || 0);
|
|
kvData["版本号"] = result.version || "26.10.1";
|
|
kvData["当前状态"] = "⚪ 草稿未发布 (停用中)";
|
|
|
|
const out = [
|
|
`✨ 工作流创建成功!新工作流已生成为草稿状态 (state = 0):`,
|
|
printKeyValue(kvData),
|
|
"",
|
|
"下一步操作建议:",
|
|
` 查看明细: hose-use workflow get ${result.id}`,
|
|
` 测试执行: hose-use workflow test ${result.id}`,
|
|
` 发布上线: hose-use workflow publish ${result.id} --yes`,
|
|
].join("\n");
|
|
|
|
return {
|
|
stdout: `${out}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|