Files
pnpm/patching/commands/src/getPatchedDependency.ts
Zoltan Kochan 4a36b9a110 refactor: rename internal packages to @pnpm/<domain>.<leaf> convention (#10997)
## Summary

Rename all internal packages so their npm names follow the `@pnpm/<domain>.<leaf>` convention, matching their directory structure. Also rename directories to remove redundancy and improve clarity.

### Bulk rename (94 packages)

All `@pnpm/` packages now derive their name from their directory path using dot-separated segments. Exceptions: `packages/`, `__utils__/`, and `pnpm/artifacts/` keep leaf names only.

### Directory renames (removing redundant prefixes)

- `cli/cli-meta` → `cli/meta`, `cli/cli-utils` → `cli/utils`
- `config/config` → `config/reader`, `config/config-writer` → `config/writer`
- `fetching/fetching-types` → `fetching/types`
- `lockfile/lockfile-to-pnp` → `lockfile/to-pnp`
- `store/store-connection-manager` → `store/connection-manager`
- `store/store-controller-types` → `store/controller-types`
- `store/store-path` → `store/path`

### Targeted renames (clarity improvements)

- `deps/dependency-path` → `deps/path` (`@pnpm/deps.path`)
- `deps/calc-dep-state` → `deps/graph-hasher` (`@pnpm/deps.graph-hasher`)
- `deps/inspection/dependencies-hierarchy` → `deps/inspection/tree-builder` (`@pnpm/deps.inspection.tree-builder`)
- `bins/link-bins` → `bins/linker`, `bins/remove-bins` → `bins/remover`, `bins/package-bins` → `bins/resolver`
- `installing/get-context` → `installing/context`
- `store/package-store` → `store/controller`
- `pkg-manifest/manifest-utils` → `pkg-manifest/utils`

### Manifest reader/writer renames

- `workspace/read-project-manifest` → `workspace/project-manifest-reader` (`@pnpm/workspace.project-manifest-reader`)
- `workspace/write-project-manifest` → `workspace/project-manifest-writer` (`@pnpm/workspace.project-manifest-writer`)
- `workspace/read-manifest` → `workspace/workspace-manifest-reader` (`@pnpm/workspace.workspace-manifest-reader`)
- `workspace/manifest-writer` → `workspace/workspace-manifest-writer` (`@pnpm/workspace.workspace-manifest-writer`)

### Workspace package renames

- `workspace/find-packages` → `workspace/projects-reader`
- `workspace/find-workspace-dir` → `workspace/root-finder`
- `workspace/resolve-workspace-range` → `workspace/range-resolver`
- `workspace/filter-packages-from-dir` merged into `workspace/filter-workspace-packages` → `workspace/projects-filter`

### Domain moves

- `pkg-manifest/read-project-manifest` → `workspace/project-manifest-reader`
- `pkg-manifest/write-project-manifest` → `workspace/project-manifest-writer`
- `pkg-manifest/exportable-manifest` → `releasing/exportable-manifest`

### Scope

- 1206 files changed
- Updated: package.json names/deps, TypeScript imports, tsconfig references, changeset files, renovate.json, test fixtures, import ordering
2026-03-17 21:50:40 +01:00

132 lines
4.7 KiB
TypeScript

import path from 'node:path'
import type { Config } from '@pnpm/config.reader'
import { PnpmError } from '@pnpm/error'
import { isGitHostedPkgUrl } from '@pnpm/fetching.pick-fetcher'
import { readCurrentLockfile, type TarballResolution } from '@pnpm/lockfile.fs'
import { nameVerFromPkgSnapshot } from '@pnpm/lockfile.utils'
import { parseWantedDependency, type ParseWantedDependencyResult } from '@pnpm/resolving.parse-wanted-dependency'
import enquirer from 'enquirer'
import { realpathMissing } from 'realpath-missing'
import semver from 'semver'
export type GetPatchedDependencyOptions = {
lockfileDir: string
} & Pick<Config, 'virtualStoreDir' | 'modulesDir'>
export type GetPatchedDependencyResult = ParseWantedDependencyResult & { applyToAll: boolean }
export async function getPatchedDependency (rawDependency: string, opts: GetPatchedDependencyOptions): Promise<GetPatchedDependencyResult> {
const dep = parseWantedDependency(rawDependency)
const { versions, preferredVersions } = await getVersionsFromLockfile(dep, opts)
if (!preferredVersions.length) {
throw new PnpmError(
'PATCH_VERSION_NOT_FOUND',
`Can not find ${rawDependency} in project ${opts.lockfileDir}, ${versions.length ? `you can specify currently installed version: ${versions.map(({ version }) => version).join(', ')}.` : `did you forget to install ${rawDependency}?`}`
)
}
dep.alias = dep.alias ?? rawDependency
if (preferredVersions.length > 1) {
const { version, applyToAll } = await enquirer.prompt<{
version: string
applyToAll: boolean
}>([{
type: 'select',
name: 'version',
message: 'Choose which version to patch',
choices: preferredVersions.map(preferred => ({
name: preferred.version,
message: preferred.version,
value: preferred.gitTarballUrl ?? preferred.version,
hint: preferred.gitTarballUrl ? 'Git Hosted' : undefined,
})),
result (selected) {
const selectedVersion = preferredVersions.find(preferred => preferred.version === selected)!
return selectedVersion.gitTarballUrl ?? selected
},
}, {
type: 'confirm',
name: 'applyToAll',
message: 'Apply this patch to all versions?',
}])
return {
...dep,
applyToAll,
bareSpecifier: version,
}
} else {
const preferred = preferredVersions[0]
if (preferred.gitTarballUrl) {
return {
...opts,
applyToAll: false,
bareSpecifier: preferred.gitTarballUrl,
}
}
return {
...dep,
applyToAll: !dep.bareSpecifier,
bareSpecifier: preferred.version,
}
}
}
// https://github.com/stackblitz-labs/pkg.pr.new
// With pkg.pr.new, each of your commits and pull requests will trigger an instant preview release without publishing anything to NPM.
// This enables users to access features and bug-fixes without the need to wait for release cycles using npm or pull request merges.
// When a package is installed via pkg.pr.new and has never been published to npm,
// the version or name obtained is incorrect, and an error will occur when patching. We can treat it as a tarball url.
export function isPkgPrNewUrl (url: string): boolean {
return url.startsWith('https://pkg.pr.new/')
}
export interface LockfileVersion {
gitTarballUrl?: string
name: string
peerDepGraphHash?: string
version: string
}
export interface LockfileVersionsList {
versions: LockfileVersion[]
preferredVersions: LockfileVersion[]
}
export async function getVersionsFromLockfile (dep: ParseWantedDependencyResult, opts: GetPatchedDependencyOptions): Promise<LockfileVersionsList> {
const modulesDir = await realpathMissing(path.join(opts.lockfileDir, opts.modulesDir ?? 'node_modules'))
const lockfile = await readCurrentLockfile(path.join(modulesDir, '.pnpm'), {
ignoreIncompatible: true,
}) ?? null
if (!lockfile) {
throw new PnpmError(
'PATCH_NO_LOCKFILE',
'The modules directory is not ready for patching',
{
hint: 'Run pnpm install first',
}
)
}
const pkgName = dep.alias && dep.bareSpecifier ? dep.alias : (dep.bareSpecifier ?? dep.alias)
const versions = Object.entries(lockfile.packages ?? {})
.map(([depPath, pkgSnapshot]) => {
const tarball = (pkgSnapshot.resolution as TarballResolution)?.tarball ?? ''
return {
...nameVerFromPkgSnapshot(depPath, pkgSnapshot),
gitTarballUrl: (isGitHostedPkgUrl(tarball) || isPkgPrNewUrl(tarball)) ? tarball : undefined,
}
})
.filter(({ name }) => name === pkgName)
.sort((v1, v2) => semver.compare(v1.version, v2.version))
return {
versions,
preferredVersions: versions.filter(({ version }) => dep.alias && dep.bareSpecifier ? semver.satisfies(version, dep.bareSpecifier) : true),
}
}