install

tcmts.loader.packages.install · #azmgm3yd · term · runs on the server

uses node:child_process

import type { Layout } from "#codebase/tcmts/loader";
import { packageManager, progress } from "#codebase/tcmts/loader/packages";
import { spawnSync } from "node:child_process";

export function install(lay: Layout, what: string[]) {
  const shown = progress.start(`installing ${what.join(", ")}`);
  const started = Date.now();
  const args =
    packageManager() === "pnpm"
      ? ["install", "--ignore-scripts", "--ignore-workspace", "--reporter=silent", "--config.auto-install-peers=true", "--config.strict-peer-dependencies=false"]
      : ["install", "--ignore-scripts", "--no-audit", "--no-fund", "--loglevel=error"];
  // the package manager's complaints come after the progress line, not through it
  const r = spawnSync(packageManager(), args, { cwd: lay.cacheDir, stdio: ["ignore", "ignore", "pipe"], encoding: "utf8" });
  if (r.status !== 0) {
    shown.fail(`couldn't install ${what.join(", ")}`);
    process.stderr.write(r.stderr ?? "");
    throw new Error(`${packageManager()} install failed in .tcm/cache`);
  }
  shown.stop(`installed ${what.join(", ")} in ${((Date.now() - started) / 1000).toFixed(1)}s`);
  if (r.stderr) process.stderr.write(r.stderr);
}

uses

used by