findPackage

tcmts.core.analyze.findPackage · #2wlt3xf3 · term · runs on the server

uses node:path

import fs from "node:fs";
import path from "node:path";

export function findPackage(name: string, fromDir: string): { name: string; version: string } | undefined {
  for (let dir = fromDir; ; dir = path.dirname(dir)) {
    const pj = path.join(dir, "node_modules", name, "package.json");
    if (fs.existsSync(pj)) {
      const json = JSON.parse(fs.readFileSync(pj, "utf8")) as { name: string; version: string };
      return { name: json.name, version: json.version };
    }
    if (path.dirname(dir) === dir) return undefined;
  }
}

used by