startDev

tcmts.cli.test.e2e.web.startDev · #bu7jdpkx · term · runs on the server

uses node:child_process

Start tcm dev in dir, kept by keep; resolves with its URL and a way to wait for more output.

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 startDev(keep: <C extends ChildProcess>(c: C) => C, dir: string, app: string): Promise<{ url: string; output: () => string }> {
  const child = keep(spawn(process.execPath, [tcmBin(), "dev", 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({ url: m[1]!, output: () => out });
      }
    }, 50);
    child.on("exit", (code) => reject(new Error(`tcm dev exited ${code}:\n${out}`)));
  });
}

uses

used by