installedIntegrity

tcmts.loader.packages.installedIntegrity · #5dxxv7a4 · term · runs on the server

uses node:path

What the cache's install got, by name@version: the integrity its package manager recorded for each tarball.

import type { Layout } from "#codebase/tcmts/loader";
import fs from "node:fs";
import path from "node:path";

export function installedIntegrity(lay: Layout): Map<string, string> {
  const out = new Map<string, string>();
  const pnpmLock = path.join(lay.cacheDir, "pnpm-lock.yaml");
  if (fs.existsSync(pnpmLock)) {
    // packages:\n  'name@version':\n    resolution: {integrity: sha512-…}
    for (const m of fs.readFileSync(pnpmLock, "utf8").matchAll(/^ {2}'?(@?[^@'\s]+)@([^'\s:(]+)'?:\n {4}resolution: \{integrity: ([^,}\s]+)/gm)) out.set(`${m[1]}@${m[2]}`, m[3]!);
  }
  const npmLock = path.join(lay.cacheDir, "package-lock.json");
  if (fs.existsSync(npmLock)) {
    const lock = JSON.parse(fs.readFileSync(npmLock, "utf8")) as { packages?: Record<string, { name?: string; version?: string; integrity?: string }> };
    for (const [where, p] of Object.entries(lock.packages ?? {})) {
      const name = p.name ?? where.replace(/^.*node_modules\//, "");
      if (p.version && p.integrity) out.set(`${name}@${p.version}`, p.integrity);
    }
  }
  return out;
}

uses

used by