mirror of
https://github.com/pnpm/pnpm.git
synced 2026-03-30 04:52:04 -04:00
37 lines
2.0 KiB
TypeScript
37 lines
2.0 KiB
TypeScript
import path from 'path'
|
|
import { type LockfileObject, type TarballResolution } from '@pnpm/lockfile.types'
|
|
import { depPathToFilename } from '@pnpm/dependency-path'
|
|
import { type ProjectId, type DepPath } from '@pnpm/types'
|
|
import { packageIdFromSnapshot } from './packageIdFromSnapshot.js'
|
|
import { nameVerFromPkgSnapshot } from './nameVerFromPkgSnapshot.js'
|
|
|
|
type GetLocalLocations = (depPath: DepPath, pkgName: string) => string[]
|
|
|
|
export function extendProjectsWithTargetDirs<T> (
|
|
projects: Array<T & { id: ProjectId }>,
|
|
lockfile: LockfileObject,
|
|
ctx: {
|
|
virtualStoreDir: string
|
|
pkgLocationsByDepPath?: Record<DepPath, string[]>
|
|
virtualStoreDirMaxLength: number
|
|
}
|
|
): Array<T & { id: ProjectId, stages: string[], targetDirs: string[] }> {
|
|
const getLocalLocations: GetLocalLocations = ctx.pkgLocationsByDepPath != null
|
|
? (depPath: DepPath) => ctx.pkgLocationsByDepPath![depPath]
|
|
: (depPath: DepPath, pkgName: string) => [path.join(ctx.virtualStoreDir, depPathToFilename(depPath, ctx.virtualStoreDirMaxLength), 'node_modules', pkgName)]
|
|
const projectsById: Record<ProjectId, T & { id: ProjectId, targetDirs: string[], stages?: string[] }> =
|
|
Object.fromEntries(projects.map((project) => [project.id, { ...project, targetDirs: [] as string[] }]))
|
|
for (const [depPath, pkg] of Object.entries(lockfile.packages ?? {})) {
|
|
if ((pkg.resolution as TarballResolution)?.type !== 'directory') continue
|
|
const pkgId = packageIdFromSnapshot(depPath as DepPath, pkg)
|
|
const { name: pkgName } = nameVerFromPkgSnapshot(depPath, pkg)
|
|
const importerId = pkgId.replace(/^file:/, '') as ProjectId
|
|
if (projectsById[importerId] == null) continue
|
|
const localLocations = getLocalLocations(depPath as DepPath, pkgName)
|
|
if (!localLocations) continue
|
|
projectsById[importerId].targetDirs.push(...localLocations)
|
|
projectsById[importerId].stages = ['preinstall', 'install', 'postinstall', 'prepare', 'prepublishOnly']
|
|
}
|
|
return Object.values(projectsById) as Array<T & { id: ProjectId, stages: string[], targetDirs: string[] }>
|
|
}
|