startServer

tcmts.cli.test.e2e.contributions.startServer · #mrpfnzc5 · term · runs on the server

uses node:child_process

Start tcm serve on a free port (with args besides); resolves to its base URL.

import { env } from "#codebase/tcmts/cli/test/e2e/contributions";
import type { Keep } from "#codebase/tcmts/cli/test/e2e/contributions";
import { tcmBin } from "#codebase/tcmts/cli/test/e2e/harness";
import { spawn } from "node:child_process";

export function startServer(
  keep: Keep,
  dir: string,
  token: string,
  args: string[] = [],
): Promise<string> {
  const child = keep(
    spawn(process.execPath, [tcmBin(), "serve", "--port", "0", "--token", token, ...args], {
      cwd: dir,
      env: env(),
    }),
  );
  return new Promise((resolve, reject) => {
    let out = "";
    child.stdout.setEncoding("utf8").on("data", (d: string) => {
      out += d;
      const m = /url\s+(https?:\/\/[\d.]+:\d+)/.exec(out);
      if (m) resolve(m[1]!);
    });
    child.stderr.setEncoding("utf8").on("data", (d: string) => (out += d));
    child.on("exit", (code) => reject(new Error(`server exited ${code}: ${out}`)));
  });
}

uses

used by