importProject

tcmts.cli.main.importProject · #5is7czhi · term · runs on the server

uses node:path

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { importFiles } from "#codebase/tcmts/cli/main";
import { packagesIn } from "#codebase/tcmts/cli/manifests";
import { linkMirror, loadMirror, replayReleases, syncMirror } from "#codebase/tcmts/cli/mirror";
import { dim } from "#codebase/tcmts/cli/style";
import path from "node:path";

export function importProject(cb: Codebase, dir: string, mode: "add" | "update", into: string, verbose: boolean, exclude: string[], tests: boolean, mirror: boolean): boolean {
  const root = path.resolve(dir);
  // importing the mirrored directory again (its files changed outside tcm) follows the files, and re-links it
  // rather than writing back to it
  const linked = loadMirror(cb);
  const following = !mirror && linked?.dir === root ? linked : undefined;
  const relink = mirror || !!following;
  if (relink) cb.syncsMirror = false;
  let ok = false;
  try {
    // linking a mirror into a codebase: its releases, kept with its files in git, are made again first
    let made = 0;
    if (mirror) {
      made = replayReleases(
        cb,
        root,
        (at, label) => !!importFiles(cb, at, label, "update", into, false, exclude, tests),
        (pkg) => packagesIn(cb, cb.store.names()).find((p) => p.name === pkg)?.namespace,
      );
      if (made) console.log(dim(`made ${made} release${made > 1 ? "s" : ""} again from git`));
    }
    // after the releases, the files as they are now are an update of the last one
    const held = importFiles(cb, root, dir, made ? "update" : mode, into, verbose, exclude, tests, following);
    ok = !!held;
    if (held && relink) {
      linkMirror(cb, root, into, tests, exclude, following && { log: following.log, held });
      if (mirror) console.log(dim(`mirror: changes made in tcm under ${into || "the root namespace"} are written back to ${dir}`));
    }
  } finally {
    cb.syncsMirror = true;
  }
  // what the codebase changed while the files did (write-back waited for them) is written to them now
  if (ok && following) syncMirror(cb);
  return ok;
}

uses

used by