maests
Version:
An executable compiler for creating Maestro's yaml-flows with typescript.
39 lines (38 loc) • 1.31 kB
JavaScript
import { buildSync } from "esbuild";
import { join } from "path";
import { stringify } from "yaml";
import "./commands.mjs";
import { addOut, handleNest } from "../out.mjs";
import { maestsDir, tsConfigDir, writeFileWithDirectorySync } from "../utils.mjs";
import "fs";
import { deleteExport } from "../rewrite-code.mjs";
const createScriptOutPath = (scriptFullPath) => {
const scriptPath = scriptFullPath.replace(`${tsConfigDir}/`, "");
return join(maestsDir, scriptPath.replace(".ts", ".js"));
}, runScript = (path, funcName) => {
if (typeof path == "function") return;
const scriptPath = createScriptOutPath(path), command = `- runScript: ${scriptPath}
`;
addOut(command);
const { outputFiles } = buildSync({
entryPoints: [path],
bundle: !0,
format: "esm",
sourcemap: !1,
legalComments: "none",
write: !1
});
let code = outputFiles[0].text;
code = code.replace(/(?:\${)?process\.env\.([^\n\s}]*)}?/g, (_, p1) => process.env[p1] || ""), code = deleteExport(code), code += `
${funcName ? `${funcName}();` : ""}`, writeFileWithDirectorySync(scriptPath, code);
}, runFlow = ({
flow,
condition
}) => {
const out = handleNest(flow, !0), result = stringify([{ runFlow: { when: condition, commands: out } }]);
addOut(result);
};
export {
runFlow,
runScript
};