mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-12 00:24:13 +08:00
28 lines
696 B
JavaScript
28 lines
696 B
JavaScript
export function generateCode(commandFlow) {
|
|
// 检查是否包含异步函数
|
|
const hasAsyncFunction = commandFlow.some((cmd) => cmd.isAsync);
|
|
|
|
let code = hasAsyncFunction ? ["async function run() {"] : [];
|
|
const indent = hasAsyncFunction ? " " : "";
|
|
|
|
commandFlow.forEach((cmd) => {
|
|
if (!cmd.code) return;
|
|
let line = indent;
|
|
|
|
if (cmd.outputVariable) {
|
|
line += `let ${cmd.outputVariable} = `;
|
|
}
|
|
|
|
let awaitCmd = cmd.isAsync ? "await " : "";
|
|
line += `${awaitCmd} ${cmd.code}`;
|
|
code.push(line);
|
|
});
|
|
|
|
if (hasAsyncFunction) {
|
|
code.push("}"); // Close the async function
|
|
code.push("run();"); // Call the function
|
|
}
|
|
|
|
return code.join("\n");
|
|
}
|