193 lines
6.6 KiB
TypeScript
193 lines
6.6 KiB
TypeScript
import {
|
|
findWorkflowsByTaskType,
|
|
getWorkflowDetail,
|
|
listAllWorkflows,
|
|
} from "../../../domains/platform/services.js";
|
|
import { stringOption, numberOption, booleanOption } from "../../parser.js";
|
|
import { createClientsForCorp } from "../../helpers.js";
|
|
import { printJson, printTable, printKeyValue } from "../../printer.js";
|
|
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
|
|
import { PLATFORM_ID_OPTION_KEYS, createWorkflowDetailClient } from "./shared.js";
|
|
|
|
export async function handleList(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
|
const clients = await createClientsForCorp(runtime.selectedCorp!);
|
|
const page = numberOption(parsed.options, ["page"]) || 1;
|
|
const pageSize = numberOption(parsed.options, ["pageSize", "size"]) || 50;
|
|
const filterPlatformId = stringOption(parsed.options, PLATFORM_ID_OPTION_KEYS);
|
|
const keyword = stringOption(parsed.options, ["keyword", "q", "query"]);
|
|
const isFull = booleanOption(parsed.options, ["full", "with-tasks", "withTasks"], false);
|
|
|
|
const rawResult = await listAllWorkflows(clients, { page, pageSize });
|
|
let workflows = filterPlatformId
|
|
? rawResult.workflows.filter(
|
|
(wf) => wf.platformId === filterPlatformId || wf.platformName === filterPlatformId
|
|
)
|
|
: rawResult.workflows;
|
|
|
|
if (keyword) {
|
|
const kw = keyword.toLowerCase();
|
|
workflows = workflows.filter(
|
|
(wf) =>
|
|
wf.pipelineName.toLowerCase().includes(kw) ||
|
|
wf.id.toLowerCase().includes(kw) ||
|
|
Boolean(wf.platformName && wf.platformName.toLowerCase().includes(kw))
|
|
);
|
|
}
|
|
|
|
// 默认轻量化模式:列表态剥离庞大的 tasks 节点树与代码脚本,只输出核心元数据和节点数统计
|
|
const outputWorkflows = isFull
|
|
? workflows
|
|
: workflows.map(({ tasks, ...rest }) => ({
|
|
...rest,
|
|
nodeCount: tasks?.length ?? 0,
|
|
}));
|
|
|
|
const result = {
|
|
...rawResult,
|
|
workflows: outputWorkflows,
|
|
totalWorkflows: workflows.length,
|
|
};
|
|
|
|
if (runtime.jsonMode) {
|
|
return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
if (workflows.length === 0) {
|
|
const emptyMsg = keyword
|
|
? `企业 [${runtime.selectedCorp!.name}] 下未找到包含关键词 "${keyword}" 的工作流\n`
|
|
: `企业 [${runtime.selectedCorp!.name}] 下暂无工作流\n`;
|
|
return {
|
|
stdout: emptyMsg,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const rows = workflows.map((wf, i) => [
|
|
i + 1,
|
|
wf.id,
|
|
wf.pipelineName,
|
|
wf.platformName || wf.platformId || "-",
|
|
wf.state === 1 ? "🟢 启用" : "⚪ 停用",
|
|
wf.tasks?.length ?? (wf as any).nodeCount ?? "-",
|
|
]);
|
|
|
|
const table = printTable(
|
|
["#", "工作流 ID", "工作流名称", "所属自建应用", "状态", "节点数"],
|
|
rows
|
|
);
|
|
|
|
const filterDesc = [
|
|
filterPlatformId ? `应用: ${filterPlatformId}` : "",
|
|
keyword ? `关键词: "${keyword}"` : "",
|
|
].filter(Boolean).join(", ");
|
|
|
|
const titleSuffix = filterDesc ? ` (过滤: ${filterDesc}, 共 ${workflows.length} 条)` : ` (共 ${workflows.length} 条)`;
|
|
|
|
return {
|
|
stdout: `====== [${runtime.selectedCorp!.name}] 工作流列表${titleSuffix} ======\n${table}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
export async function handleGet(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
|
const pipelineId = parsed.args[0] || stringOption(parsed.options, ["id", "pipelineId"]);
|
|
if (!pipelineId) {
|
|
return { stdout: "", stderr: "用法: hose-use workflow get <pipelineId>\n", exitCode: 1 };
|
|
}
|
|
|
|
const { client } = await createWorkflowDetailClient(parsed, runtime, pipelineId);
|
|
const detail = await getWorkflowDetail(client, pipelineId);
|
|
|
|
if (runtime.jsonMode) {
|
|
return { stdout: printJson(detail, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
const isDraft = detail.state === 0;
|
|
const tasksVersion = (detail as Record<string, unknown>).tasksVersion;
|
|
const summary = printKeyValue({
|
|
"工作流 ID": detail.id,
|
|
"工作流名称": detail.label,
|
|
"当前状态": detail.state === 1 ? "🟢 线上启用 (state=1)" : "⚪ 停用/未发布草稿 (state=0)",
|
|
"发布版本 (version)": detail.version || "-",
|
|
...(tasksVersion !== undefined ? { "草稿版本 (tasksVersion)": String(tasksVersion) } : {}),
|
|
"节点总数": detail.tasks?.length ?? 0,
|
|
});
|
|
|
|
const draftNotice = isDraft
|
|
? `\n💡 提示: 该工作流当前处于未发布草稿态 (state=0)。草稿与生产流程共享同一实例 ID 和历史记录;直接修改草稿将在发布后覆盖线上逻辑。如需安全隔离测试,请先执行: hose-use workflow copy ${detail.id}\n`
|
|
: "";
|
|
|
|
let taskList = "";
|
|
if (detail.tasks && detail.tasks.length > 0) {
|
|
const taskRows = detail.tasks.map((t, i) => [
|
|
i + 1,
|
|
t.name || "-",
|
|
t.label || "-",
|
|
t.type,
|
|
t.groupName || "-",
|
|
]);
|
|
taskList = `\n 节点结构:\n` + printTable(["#", "节点标识", "节点名称", "类型", "分组"], taskRows);
|
|
}
|
|
|
|
return {
|
|
stdout: `====== 工作流详情 ======\n${summary}\n${draftNotice}${taskList}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
export async function handleFind(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
|
const taskType = stringOption(parsed.options, ["task", "taskType", "type"]) || parsed.args[0];
|
|
if (!taskType) {
|
|
return {
|
|
stdout: "",
|
|
stderr: "用法: hose-use workflow find --task=<节点类型> (如 code-js2, http, condition)\n",
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
|
|
const clients = await createClientsForCorp(runtime.selectedCorp!);
|
|
const includeDetail = booleanOption(parsed.options, ["includeDetail", "detail"]);
|
|
const page = numberOption(parsed.options, ["page"]) || 1;
|
|
const pageSize = numberOption(parsed.options, ["pageSize", "size"]) || 50;
|
|
|
|
const result = await findWorkflowsByTaskType(clients, taskType, {
|
|
page,
|
|
pageSize,
|
|
includeDetail,
|
|
});
|
|
|
|
if (runtime.jsonMode) {
|
|
return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
if (result.workflows.length === 0) {
|
|
return {
|
|
stdout: `未找到包含节点类型 [${taskType}] 的工作流\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const rows = result.workflows.map((wf, i) => [
|
|
i + 1,
|
|
wf.id,
|
|
wf.pipelineName,
|
|
wf.platformName || wf.platformId || "-",
|
|
wf.state === 1 ? "🟢 启用" : "⚪ 停用",
|
|
]);
|
|
|
|
const table = printTable(
|
|
["#", "工作流 ID", "工作流名称", "所属自建应用", "状态"],
|
|
rows
|
|
);
|
|
|
|
return {
|
|
stdout: `====== 匹配节点类型 [${taskType}] 的工作流 (共 ${result.workflows.length} 条) ======\n${table}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|