serve

tcmts.cli.test.e2e.server.serve · #z5yv74ek · term · runs on the server

uses node:child_process

import { plainEnv, tcmBin } from "#codebase/tcmts/cli/test/e2e/harness";
import { spawn } from "node:child_process";
import type { ChildProcess } from "node:child_process";

export async function serve(dir: string, keep: <C extends ChildProcess>(c: C) => C, ...args: string[]): Promise<{ url: string; output: () => string }> {
  const child = keep(spawn(process.execPath, [tcmBin(), ...args, "--port", "0"], { cwd: dir, env: plainEnv() }));
  let out = "";
  child.stdout.setEncoding("utf8").on("data", (d: string) => (out += d));
  child.stderr.setEncoding("utf8").on("data", (d: string) => (out += d));
  for (const end = Date.now() + 30000; Date.now() < end; await new Promise((r) => setTimeout(r, 100))) {
    const m = /url\s+(http:\/\/\S+)/.exec(out);
    if (m) return { url: m[1]!, output: () => out };
  }
  throw new Error(`no url:\n${out}`);
}

uses

used by