startApp

tcmts.cli.test.e2e.examples.startApp · #hhysi4vt · term · runs on the server

uses node:child_process

Start tcm start <app> in cwd on a free port; resolves with its URL.

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

export async function startApp(keep: Keep, cwd: string, app: string): Promise<string> {
  const child = keep(spawn(process.execPath, [tcmBin(), "start", app, "--port", "0"], { cwd, env: env() }));
  let log = "";
  child.stdout.setEncoding("utf8").on("data", (d: string) => (log += d));
  child.stderr.setEncoding("utf8").on("data", (d: string) => (log += d));
  for (const end = Date.now() + 60000; !/url\s+http/.test(log) && Date.now() < end; ) await new Promise((r) => setTimeout(r, 100));
  const url = /url\s+(http:\/\/\S+)/.exec(log)?.[1];
  assert.ok(url, log);
  return url!;
}

uses

used by