|
| 1 | +#!/usr/bin/env node |
| 2 | +// Brings up the integration backend (docker-compose) and waits for it to be |
| 3 | +// reachable before launching `vite dev --mode integration`. Playwright starts |
| 4 | +// the webServer command before globalSetup, so we cannot rely on globalSetup |
| 5 | +// to start docker — we have to do it here, otherwise Vite SSR fetches race |
| 6 | +// the API container coming up and Playwright times out the webServer probe. |
| 7 | + |
| 8 | +import { spawn, spawnSync } from 'node:child_process'; |
| 9 | +import { fileURLToPath } from 'node:url'; |
| 10 | +import path from 'node:path'; |
| 11 | +import { request as httpsRequest } from 'node:https'; |
| 12 | +import { request as httpRequest } from 'node:http'; |
| 13 | + |
| 14 | +const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); |
| 15 | +const API_URL = process.env.VITE_API_PROXY_TARGET ?? 'https://localhost:5001'; |
| 16 | +const TIMEOUT_MS = Number(process.env.INTEGRATION_BACKEND_TIMEOUT_MS ?? 10 * 60 * 1000); |
| 17 | +const POLL_INTERVAL_MS = 1500; |
| 18 | + |
| 19 | +function probe(url) { |
| 20 | + return new Promise((resolve) => { |
| 21 | + const req = (url.startsWith('https:') ? httpsRequest : httpRequest)( |
| 22 | + url, |
| 23 | + { method: 'GET', rejectUnauthorized: false, timeout: 2000 }, |
| 24 | + (res) => { |
| 25 | + res.resume(); |
| 26 | + // Any HTTP response (even 404) means the server is accepting connections. |
| 27 | + resolve(true); |
| 28 | + } |
| 29 | + ); |
| 30 | + req.on('error', () => resolve(false)); |
| 31 | + req.on('timeout', () => { |
| 32 | + req.destroy(); |
| 33 | + resolve(false); |
| 34 | + }); |
| 35 | + req.end(); |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); |
| 40 | + |
| 41 | +async function waitForBackend() { |
| 42 | + const deadline = Date.now() + TIMEOUT_MS; |
| 43 | + let attempts = 0; |
| 44 | + process.stdout.write(`[dev:integration] waiting for backend at ${API_URL} ...\n`); |
| 45 | + while (Date.now() < deadline) { |
| 46 | + if (await probe(API_URL)) { |
| 47 | + process.stdout.write(`[dev:integration] backend reachable after ${attempts} attempt(s)\n`); |
| 48 | + return; |
| 49 | + } |
| 50 | + attempts++; |
| 51 | + await sleep(POLL_INTERVAL_MS); |
| 52 | + } |
| 53 | + process.stderr.write( |
| 54 | + `[dev:integration] backend at ${API_URL} did not become reachable within ${TIMEOUT_MS}ms\n` |
| 55 | + ); |
| 56 | + process.exit(1); |
| 57 | +} |
| 58 | + |
| 59 | +function startDockerStack() { |
| 60 | + process.stdout.write('[dev:integration] starting docker-compose stack ...\n'); |
| 61 | + const result = spawnSync( |
| 62 | + 'docker', |
| 63 | + ['compose', '-f', 'docker-compose.integration.yml', 'up', '-d', '--wait'], |
| 64 | + { cwd: ROOT, stdio: 'inherit' } |
| 65 | + ); |
| 66 | + if (result.status !== 0) { |
| 67 | + process.stderr.write( |
| 68 | + '[dev:integration] failed to start docker stack — is Docker Desktop running?\n' |
| 69 | + ); |
| 70 | + process.exit(result.status ?? 1); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +startDockerStack(); |
| 75 | +await waitForBackend(); |
| 76 | + |
| 77 | +const child = spawn('pnpm', ['exec', 'vite', 'dev', '--mode', 'integration'], { |
| 78 | + stdio: 'inherit', |
| 79 | + shell: process.platform === 'win32', |
| 80 | +}); |
| 81 | + |
| 82 | +child.on('exit', (code, signal) => { |
| 83 | + if (signal) process.kill(process.pid, signal); |
| 84 | + else process.exit(code ?? 0); |
| 85 | +}); |
| 86 | + |
| 87 | +for (const sig of ['SIGINT', 'SIGTERM']) { |
| 88 | + process.on(sig, () => child.kill(sig)); |
| 89 | +} |
0 commit comments