rangeFor
uses node:path
The range of name a scratch file should get, and whether the project's own copy provides it.
import type { CodebaseCore } from "#codebase/tcmts/cli/codebase"; import { codebaseImports, installed, mostUsed, newestRangeFirst } from "#codebase/tcmts/cli/provided"; import { scratchNamespace } from "#codebase/tcmts/core/analyze"; import { compatRange } from "#codebase/tcmts/core/semver"; import { pinnedRange } from "#codebase/tcmts/core/types"; import { newestVersion } from "#codebase/tcmts/loader/packages"; import path_1 from "node:path"; export function rangeFor( cb: CodebaseCore, name: string, file: string, text: string, ): { range: string; project: boolean } | undefined { const own = installed(name, path_1.dirname(file)); const project = (range: string) => ({ range, project: !!own && compatRange(own) === range }); // 1. what the definitions it imports use const used = new Map<string, number>(); for (const p of codebaseImports(text)) { for (const kind of ["term", "type"] as const) { for (const h of cb.store.hashesAt(p, kind)) { for (const d of cb.store.getDef(h)?.deps ?? []) { const range = pinnedRange(d.ref, name); if (range) used.set(range, (used.get(range) ?? 0) + 1); } } } } const imported = mostUsed(used); if (imported) return project(imported); // 2. what its namespace uses const ns = scratchNamespace(text); const local = ns ? mostUsed(cb.store.packageRanges(name, ns)) : undefined; if (local) return project(local); // 3. what the project has installed if (own) return { range: compatRange(own), project: true }; // 4. what the rest of the codebase uses (else runs: the newest range it locks) const anywhere = mostUsed(cb.store.packageRanges(name)); if (anywhere) return { range: anywhere, project: false }; const locked = cb.store .lockedPackages() .filter((l) => l.name === name) .map((l) => l.range) .sort(newestRangeFirst)[0]; if (locked) return { range: locked, project: false }; // 5. the newest release const newest = newestVersion(name); return newest ? { range: compatRange(newest), project: false } : undefined; }
uses
- tcmts.cli.codebase.CodebaseCore
- tcmts.cli.provided.codebaseImports
- tcmts.cli.provided.installed
- tcmts.cli.provided.mostUsed
- tcmts.cli.provided.newestRangeFirst
- tcmts.core.analyze.scratchNamespace
- tcmts.core.semver.compatRange
- tcmts.core.types.pinnedRange
- tcmts.loader.packages.newestVersion