-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvitest.workflowdevkit.setup.ts
More file actions
83 lines (64 loc) · 2.34 KB
/
vitest.workflowdevkit.setup.ts
File metadata and controls
83 lines (64 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { spawn } from "node:child_process";
import { setTimeout as delay } from "node:timers/promises";
import type { ChildProcess } from "node:child_process";
import "dotenv/config";
let nitroServer: ChildProcess | null = null;
const PORT = "4000";
export async function setup() {
// eslint-disable-next-line no-console
console.log("Starting Nitro server for workflow execution...");
// Start nitro dev server with inherited environment variables
nitroServer = spawn("npx", ["nitro", "dev", "--port", PORT], {
stdio: "pipe",
detached: false,
cwd: "test-server",
// eslint-disable-next-line node/no-process-env
env: process.env,
});
// Use a promise to wait for server readiness
const serverReadyPromise = new Promise<boolean>((resolve) => {
const timeout = setTimeout(() => resolve(false), 15000);
// Listen for server output
nitroServer?.stdout?.on("data", (data) => {
const output = data.toString();
// eslint-disable-next-line no-console
console.log("[nitro]", output);
if (output.includes("listening") || output.includes("ready") || output.includes("Nitro")) {
clearTimeout(timeout);
resolve(true);
}
});
nitroServer?.stderr?.on("data", (data) => {
console.error("[nitro]", data.toString());
});
nitroServer?.on("error", (error) => {
console.error("Failed to start Nitro server:", error);
clearTimeout(timeout);
resolve(false);
});
});
await serverReadyPromise;
// Give it an extra moment to fully initialize
await delay(2000);
// eslint-disable-next-line no-console
console.log("Nitro server started and ready for workflow execution");
// Set the base URL and data dir for local workflow execution
// eslint-disable-next-line node/no-process-env
process.env.WORKFLOW_LOCAL_BASE_URL = `http://localhost:${PORT}`;
// eslint-disable-next-line node/no-process-env
process.env.WORKFLOW_LOCAL_DATA_DIR = "./test-server/.workflow-data";
}
export async function teardown() {
if (nitroServer) {
// eslint-disable-next-line no-console
console.log("Stopping Nitro server...");
nitroServer.kill("SIGTERM");
// Give it a moment to shut down gracefully
await delay(1000);
// Force kill if still running
if (!nitroServer.killed) {
nitroServer.kill("SIGKILL");
}
nitroServer = null;
}
}