runPack

tcmts.cli.run.runPack · #mvx5sz6w · term · runs on the server

uses node:path

Runs tcm pack.

import { routePack } from "#codebase/tcmts/cli/commands";
import { execute } from "#codebase/tcmts/cli/main";
import { pack } from "#codebase/tcmts/cli/pack";
import { packPackage, parseSpec } from "#codebase/tcmts/cli/releases";
import type { Context } from "#codebase/tcmts/cli/run";
import { ask, UsageError } from "#codebase/tcmts/cli/ui";
import { parse } from "@bomb.sh/router";
import path_1 from "node:path";

export async function runPack({ cb, cmd, rest, cwd }: Context): Promise<number> {
  const { model } = execute(cmd, parse(routePack(), { argv: rest }));
  // a package of the codebase, from its manifest
  if (model.name === undefined && !model.bin.length && model.entries.length <= 1) {
    const spec = model.entries[0] === undefined ? undefined : parseSpec(model.entries[0]);
    const r = packPackage(cb, spec?.name, spec?.version, model.out);
    const deps = Object.keys(r.dependencies);
    const shown = path_1.relative(cwd, r.dir);
    console.log(
      `packed ${r.pkg.name}@${r.version} into ${shown.startsWith("..") ? r.dir : shown || "."}: ${r.modules} module(s)${deps.length ? `, depends on ${deps.join(", ")}` : ""}`,
    );
    return 0;
  }

  if (!model.entries.length && !model.bin.length)
    throw new UsageError(
      "usage: tcm pack [[subpath=]<namespace|name>...] --name <pkg>[@version] [--bin cmd=<name>]",
    );
  const spec =
    model.name ??
    (await ask(
      "pack [[subpath=]<namespace|name>...] --name <pkg>[@version]",
      "Package name (and version: name@1.0.0)",
      "my-package",
    ));
  const at = spec.lastIndexOf("@");
  const [name, version] = at > 0 ? [spec.slice(0, at), spec.slice(at + 1)] : [spec, "0.0.0"];
  const pair = (s: string, what: string) => {
    const eq = s.indexOf("=");
    if (what === "bin" && eq < 0) throw new UsageError(`--bin wants cmd=<name>, not '${s}'`);
    return eq < 0 ? ["", s] : [s.slice(0, eq), s.slice(eq + 1)];
  };
  const r = pack(cb, {
    name,
    version,
    description: model.description,
    entries: model.entries
      .map((a) => pair(a, "entry"))
      .map(([subpath, target]) => ({
        subpath: subpath ? subpath.replace(/^\.\//, "") : ".",
        target: target!,
      })),
    bins: model.bin
      .map((b) => pair(b, "bin"))
      .map(([command, target]) => ({ command: command!, target: target! })),
    with: model.with,
    files: model.files,
    out: model.out ?? path_1.join(".tcm", "pack", name.replace(/^@/, "").replace("/", "__")),
  });
  const deps = Object.keys(r.dependencies);
  const shown = path_1.relative(cwd, r.dir);
  console.log(
    `packed ${name}@${version} into ${shown.startsWith("..") ? r.dir : shown || "."}: ${r.modules} module(s)${deps.length ? `, depends on ${deps.join(", ")}` : ""}`,
  );
  return 0;
}

uses

used by