import path from "path"; import { createRequire } from "module"; import type { WorkflowContext } from "../run"; import type { WorkflowStep } from "../lib/parameters"; type CustomStepRunner = ( context: WorkflowContext, step: WorkflowStep, ) => Promise | unknown; export const runCustomStep = async ( context: WorkflowContext, step: WorkflowStep, ): Promise => { const script = typeof step.script === "string" ? step.script : ""; if (!script) { throw new Error(`Custom step ${step.id} requires a script path`); } const scriptPath = path.isAbsolute(script) ? script : path.join(context.root, script); const requireFromProject = createRequire(path.join(context.root, "package.json")); const module = requireFromProject(scriptPath) as { default?: CustomStepRunner; main?: CustomStepRunner; run?: CustomStepRunner; }; const runner = module.default || module.main || module.run; if (typeof runner !== "function") { throw new Error(`Custom step script ${script} must export default, main, or run`); } const result = await runner(context, step); if (result && typeof result === "object") { context.reporter.result("execution", { stepId: step.id, kind: step.kind, mode: "custom", script, result, }); } };