serve

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

uses node:child_process

Start tcm dev or tcm start in dir, kept by keep; resolves with its URL.

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

export function serve(keep: <C extends ChildProcess>(c: C) => C, dir: string, command: "dev" | "start", app: string): Promise<string> {
  const child = keep(spawn(process.execPath, [tcmBin(), command, app, "--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));
  return new Promise((resolve, reject) => {
    const t = setInterval(() => {
      const m = /url\s+(http:\/\/\S+)/.exec(out);
      if (m) {
        clearInterval(t);
        resolve(m[1]!);
      }
    }, 50);
    child.on("exit", (code) => reject(new Error(`tcm ${command} exited ${code}:\n${out}`)));
  });
}

uses

used by