packagePage

tcmts.cli.package_pages.packagePage · #mnnv5jpi · term · runs on the server

uses node:path

A package at one range: what it is, its README, its API, and what uses it.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { apiOf, packageList, packageUsers, readmeIn } from "#codebase/tcmts/cli/package_pages";
import type { PackagePage } from "#codebase/tcmts/cli/package_pages";
import { ensureExternals, npmAlias } from "#codebase/tcmts/loader/packages";
import fs from "node:fs";
import path from "node:path";

export function packagePage(cb: Codebase, name: string, range: string): PackagePage {
  const version = cb.store.lockedVersion(name, range);
  if (!version) throw new Error(`the codebase doesn't run ${name} ${range}`);
  const module = `npm:${name}@${range}`;
  ensureExternals(cb.lay, [{ module, subpath: "", export: "*" }]);
  const alias = npmAlias(module);
  const dir = path.join(cb.lay.cacheDir, "node_modules", alias);
  const typesDir = path.join(cb.lay.cacheDir, "node_modules", "@types", alias);
  let pkg: Record<string, unknown> = {};
  try {
    pkg = JSON.parse(fs.readFileSync(path.join(dir, "package.json"), "utf8")) as Record<string, unknown>;
  } catch {
    // not installed: the page says what it can
  }
  const listed = packageList(cb).find((p) => p.name === name);
  const here = listed?.ranges.find((r) => r.range === range);
  return {
    name,
    range,
    version,
    ...(here?.integrity ? { integrity: here.integrity } : {}),
    ...(here?.types ? { types: here.types } : {}),
    ...(typeof pkg.description === "string" ? { description: pkg.description } : {}),
    ...(typeof pkg.license === "string" ? { license: pkg.license } : {}),
    ...(typeof pkg.homepage === "string" ? { homepage: pkg.homepage } : {}),
    readme: readmeIn(dir) ?? readmeIn(typesDir),
    api: apiOf(cb, alias),
    users: packageUsers(cb, name, range),
    otherRanges: (listed?.ranges ?? []).map((r) => r.range).filter((r) => r !== range),
  };
}

uses

used by