installedPackage
uses node:path
Where a package tcm runs with is installed (its directory): beside the tcm being tested (a packed one), or in the cache of the codebase these tests run in, where tcm provides it under an alias (the codebase's own tcm).
import { tcmBin } from "#codebase/tcmts/cli/test/e2e/harness"; import fs from "node:fs"; import { createRequire } from "node:module"; import path from "node:path"; export function installedPackage(name: string): string { try { return path.dirname(createRequire(tcmBin()).resolve(`${name}/package.json`)); } catch { // not beside it } const modules = path.join(process.cwd(), ".tcm", "cache", "node_modules"); const alias = `npm__${name.replace(/^@/, "").replace("/", "__")}__`; const found = fs.existsSync(modules) ? fs .readdirSync(modules) // (followed by its version: `npm__vitest__` also begins `npm__vitest__browser-playwright__`) .filter((d) => d.startsWith(alias) && /^\d/.test(d.slice(alias.length))) .sort() .at(-1) : undefined; if (!found) throw new Error( `${name} isn't installed for these tests: not beside ${tcmBin()}, nor in ${modules}`, ); return path.join(modules, found); }