新增子流程功能,可以添加子流程,方便后续作为函数调用

This commit is contained in:
fofolee
2025-01-21 18:14:46 +08:00
parent 2728c5783f
commit e0208eb119
5 changed files with 416 additions and 161 deletions

View File

@@ -1,9 +1,14 @@
export function generateCode(commandFlow) {
export function generateCode(commandFlow, functionName = null) {
// 检查是否包含异步函数
const hasAsyncFunction = commandFlow.some((cmd) => cmd.isAsync);
let code = hasAsyncFunction ? ["async function run() {"] : [];
const indent = hasAsyncFunction ? " " : "";
let code = [];
const funcName = functionName || "run";
// 生成函数声明
code.push(`${hasAsyncFunction ? "async " : ""}function ${funcName}() {`);
const indent = " ";
commandFlow.forEach((cmd) => {
// 跳过禁用的命令
@@ -20,9 +25,11 @@ export function generateCode(commandFlow) {
code.push(line);
});
if (hasAsyncFunction) {
code.push("}"); // Close the async function
code.push("run();"); // Call the function
code.push("}"); // Close the function
// 如果是主函数,则自动执行
if (functionName === "main") {
code.push("\nmain();"); // Call the main function
}
return code.join("\n");