|
| 1 | +/** |
| 2 | + * Subprocess entry point for list command |
| 3 | + * |
| 4 | + * Loads scenarios and outputs metadata via TCP IPC. |
| 5 | + * Communication is via NDJSON over TCP (not stdin/stdout). |
| 6 | + * |
| 7 | + * @module |
| 8 | + * @internal |
| 9 | + */ |
| 10 | + |
| 11 | +import { loadScenarios } from "@probitas/core/loader"; |
| 12 | +import { applySelectors } from "@probitas/core/selector"; |
| 13 | +import { toErrorObject } from "@core/errorutil/error-object"; |
| 14 | +import type { ListInput, ListOutput, ScenarioMeta } from "./list_protocol.ts"; |
| 15 | +import { |
| 16 | + closeIpc, |
| 17 | + connectIpc, |
| 18 | + parseIpcPort, |
| 19 | + readInput, |
| 20 | + writeOutput, |
| 21 | +} from "./utils.ts"; |
| 22 | + |
| 23 | +/** |
| 24 | + * Main entry point |
| 25 | + */ |
| 26 | +async function main(): Promise<void> { |
| 27 | + // Parse IPC port from command line arguments |
| 28 | + const port = parseIpcPort(Deno.args); |
| 29 | + |
| 30 | + // Connect to parent process IPC server |
| 31 | + const ipc = await connectIpc(port); |
| 32 | + |
| 33 | + try { |
| 34 | + // Read input from IPC (TCP connection establishes readiness) |
| 35 | + const input = await readInput(ipc) as ListInput; |
| 36 | + |
| 37 | + // Load scenarios from files |
| 38 | + const scenarios = await loadScenarios(input.filePaths, { |
| 39 | + onImportError: (file, err) => { |
| 40 | + const m = err instanceof Error ? err.message : String(err); |
| 41 | + throw new Error(`Failed to load scenario from ${file}: ${m}`); |
| 42 | + }, |
| 43 | + }); |
| 44 | + |
| 45 | + // Apply selectors to filter scenarios |
| 46 | + const filtered = input.selectors.length > 0 |
| 47 | + ? applySelectors(scenarios, input.selectors) |
| 48 | + : scenarios; |
| 49 | + |
| 50 | + // Build output metadata |
| 51 | + const scenarioMetas: ScenarioMeta[] = filtered.map((s) => ({ |
| 52 | + name: s.name, |
| 53 | + tags: s.tags, |
| 54 | + steps: s.steps.filter((e) => e.kind === "step").length, |
| 55 | + file: s.origin?.path || "unknown", |
| 56 | + })); |
| 57 | + |
| 58 | + await writeOutput( |
| 59 | + ipc, |
| 60 | + { |
| 61 | + type: "result", |
| 62 | + scenarios: scenarioMetas, |
| 63 | + } satisfies ListOutput, |
| 64 | + ); |
| 65 | + } catch (error) { |
| 66 | + const err = error instanceof Error ? error : new Error(String(error)); |
| 67 | + try { |
| 68 | + await writeOutput( |
| 69 | + ipc, |
| 70 | + { |
| 71 | + type: "error", |
| 72 | + error: toErrorObject(err), |
| 73 | + } satisfies ListOutput, |
| 74 | + ); |
| 75 | + } catch { |
| 76 | + // Failed to write error to IPC, log to console as fallback |
| 77 | + console.error("Subprocess error:", error); |
| 78 | + } |
| 79 | + } finally { |
| 80 | + closeIpc(ipc); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// Run main and exit explicitly to avoid async operations keeping process alive |
| 85 | +main().finally(() => { |
| 86 | + // Ensure process exits after output is flushed |
| 87 | + setTimeout(() => Deno.exit(0), 0); |
| 88 | +}); |
0 commit comments