Files
pnpm/resolving/local-resolver/src/parseBareSpecifier.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

136 lines
4.2 KiB
TypeScript

import os from 'node:os'
import path from 'node:path'
import { PnpmError } from '@pnpm/error'
import type { PkgResolutionId } from '@pnpm/resolving.resolver-base'
import normalize from 'normalize-path'
// @ts-expect-error
const isWindows = process.platform === 'win32' || global['FAKE_WINDOWS']
const isFilespec = isWindows ? /^(?:[./\\]|~\/|[a-z]:)/i : /^(?:[./]|~\/|[a-z]:)/i
const isFilename = /\.(?:tgz|tar.gz|tar)$/i
const isAbsolutePath = /^\/|^[A-Z]:/i
export interface LocalPackageSpec {
dependencyPath: string
fetchSpec: string
id: PkgResolutionId
type: 'directory' | 'file'
normalizedBareSpecifier: string
}
export interface WantedLocalDependency {
bareSpecifier: string
injected?: boolean
}
class PathIsUnsupportedProtocolError extends PnpmError {
bareSpecifier: string
protocol: string
constructor (bareSpecifier: string, protocol: string) {
super('PATH_IS_UNSUPPORTED_PROTOCOL', 'Local dependencies via `path:` protocol are not supported. ' +
'Use the `link:` protocol for folder dependencies and `file:` for local tarballs')
this.bareSpecifier = bareSpecifier
this.protocol = protocol
}
}
export function parseBareSpecifier (
wd: WantedLocalDependency,
projectDir: string,
lockfileDir: string,
opts: { preserveAbsolutePaths: boolean }
): LocalPackageSpec | null {
if (wd.bareSpecifier.startsWith('link:') || wd.bareSpecifier.startsWith('workspace:')) {
return fromLocal(wd, projectDir, lockfileDir, 'directory', opts)
}
if (wd.bareSpecifier.endsWith('.tgz') ||
wd.bareSpecifier.endsWith('.tar.gz') ||
wd.bareSpecifier.endsWith('.tar') ||
wd.bareSpecifier.includes(path.sep) ||
wd.bareSpecifier.startsWith('file:') ||
isFilespec.test(wd.bareSpecifier)
) {
const type = isFilename.test(wd.bareSpecifier) ? 'file' : 'directory'
return fromLocal(wd, projectDir, lockfileDir, type, opts)
}
if (wd.bareSpecifier.startsWith('path:')) {
throw new PathIsUnsupportedProtocolError(wd.bareSpecifier, 'path:')
}
return null
}
function fromLocal (
{ bareSpecifier, injected }: WantedLocalDependency,
projectDir: string,
lockfileDir: string,
type: 'file' | 'directory',
opts: { preserveAbsolutePaths: boolean }
): LocalPackageSpec {
const spec = bareSpecifier.replace(/\\/g, '/')
.replace(/^(?:file|link|workspace):\/*([A-Z]:)/i, '$1') // drive name paths on windows
.replace(/^(?:file|link|workspace):(?:\/*([~./]))?/, '$1')
let protocol!: string
if (bareSpecifier.startsWith('file:')) {
protocol = 'file:'
} else if (bareSpecifier.startsWith('link:')) {
protocol = 'link:'
} else {
protocol = type === 'directory' && !injected ? 'link:' : 'file:'
}
let fetchSpec!: string
let normalizedBareSpecifier!: string
if (/^~\//.test(spec)) {
// this is needed for windows and for file:~/foo/bar
fetchSpec = resolvePath(os.homedir(), spec.slice(2))
normalizedBareSpecifier = `${protocol}${spec}`
} else {
fetchSpec = resolvePath(projectDir, spec)
if (isAbsolute(spec)) {
normalizedBareSpecifier = `${protocol}${spec}`
} else {
normalizedBareSpecifier = `${protocol}${path.relative(projectDir, fetchSpec)}`
}
}
function normalizeRelativeOrAbsolute (relativeTo: string, fromPath: string) {
let specPath
if (opts.preserveAbsolutePaths && isAbsolute(spec)) {
specPath = path.resolve(fromPath)
} else {
specPath = path.relative(relativeTo, fromPath)
}
return normalize(specPath)
}
injected = protocol === 'file:'
const dependencyPath = injected
? normalizeRelativeOrAbsolute(lockfileDir, fetchSpec)
: normalize(path.resolve(fetchSpec))
const id = (
!injected && (type === 'directory' || projectDir === lockfileDir)
? `${protocol}${normalizeRelativeOrAbsolute(projectDir, fetchSpec)}`
: `${protocol}${normalizeRelativeOrAbsolute(lockfileDir, fetchSpec)}`
) as PkgResolutionId
return {
dependencyPath,
fetchSpec,
id,
normalizedBareSpecifier,
type,
}
}
function resolvePath (where: string, spec: string): string {
if (isAbsolutePath.test(spec)) return spec
return path.resolve(where, spec)
}
function isAbsolute (dir: string): boolean {
if (dir[0] === '/') return true
if (/^[A-Z]:/i.test(dir)) return true
return false
}