runBuild

tcmts.cli.run.runBuild · #udacrk5i · term · runs on the server

uses node:path

Runs tcm build.

import { routeBuild } from "#codebase/tcmts/cli/commands";
import { execute } from "#codebase/tcmts/cli/main";
import type { Context } from "#codebase/tcmts/cli/run";
import { bold, dim, green, yellow } from "#codebase/tcmts/cli/style";
import { chooseName, UsageError } from "#codebase/tcmts/cli/ui";
import { build, buildWorkDir, exportStatic, leaseBuilds, normalBase } from "#codebase/tcmts/cli/web";
import { parse } from "@bomb.sh/router";
import path_2 from "node:path";

export async function runBuild({ cb, cmd, rest, cwd }: Context): Promise<number> {
  const { model } = execute(cmd, parse(routeBuild(), { argv: rest }));
  const app = model.app ?? (await chooseName(cb, "build <app> [--force]", "Which app?", "value"));
  // (held from pruning in another process until it's written out)
  const lease = leaseBuilds(buildWorkDir(cb));
  const built = await build(cb, app, { force: model.force, lease }).finally(() =>
    model.static ? undefined : lease.release(),
  );
  if (model.static) {
    const base = normalBase(model.base ?? undefined);
    const dir = path_2.resolve(cwd, model.static);
    const site = await exportStatic(cb, built, dir, base).finally(() => lease.release());
    for (const w of site.warnings) console.log(yellow(`  warning  ${w}`));
    console.log(
      `${green("wrote")} ${bold(app)} as a static site into ${path_2.relative(cwd, dir) || "."} ${dim(`(${site.pages} page(s), ${site.files.length} files, for ${base || "the site's root"})`)}`,
    );
  } else if (model.base)
    throw new UsageError(
      "--base is for a static site: `tcm build <app> --static <dir> --base /path`",
    );
  return 0;
}

uses

used by