import { isAbsolute, resolve } from 'node:path'; import { runBoundedProcess, resolveScriptRuntime } from '../../../../src/plugin-sdk.js'; import { assertWorkflowTimeout } from './model.js'; export interface WorkflowFileOptions { cwd?: string; input?: string; timeoutMs?: number; env?: Record; } export interface WorkflowProcessResult { stdout: string; stderr: string; exitCode: number; timedOut: boolean; } export async function executeWorkflowFile( path: string, args: string[], options: WorkflowFileOptions, runtime: { cwd: string; workflowDir: string; signal?: AbortSignal }, ): Promise { assertWorkflowTimeout(options.timeoutMs); runtime.signal?.throwIfAborted(); const started = Date.now(); const target = isAbsolute(path) ? path : resolve(runtime.workflowDir || runtime.cwd, path); const script = /\.(?:mjs|cjs|js)$/i.test(target); const env = { ...process.env, ...options.env }; const executable = script ? await resolveScriptRuntime({ env, signal: runtime.signal, timeoutMs: options.timeoutMs }) : target; const remaining = options.timeoutMs === undefined ? undefined : options.timeoutMs - (Date.now() - started); if (remaining !== undefined && remaining <= 0) return { stdout: '', stderr: 'Script runtime resolution exceeded process deadline', exitCode: 124, timedOut: true }; return runBoundedProcess(executable, script ? [target, ...args.map(String)] : args.map(String), { ...options, timeoutMs: remaining, cwd: options.cwd ? resolve(runtime.cwd, options.cwd) : runtime.cwd, env, signal: runtime.signal, }); }