mirror of
https://github.com/pnpm/pnpm.git
synced 2026-05-15 03:56:15 -04:00
* refactor: replace `forEach` with `for`-loops Changes: * Most `Object.keys(o).forEach` are replaced by `for in`. * Most `Array.filter(c).forEach` are replaced by `for of` + `if continue`. * `return` in `forEach` callbacks are replaced by `continue`. There may be minor improvement to memory footprint as this change would reduce the creations of temporary arrays and temporary functions. * fix: return -> continue * refactor: remove the commented out code
37 lines
2.0 KiB
TypeScript
37 lines
2.0 KiB
TypeScript
import path from 'path'
|
|
import { type Lockfile, type TarballResolution } from '@pnpm/lockfile.types'
|
|
import { depPathToFilename } from '@pnpm/dependency-path'
|
|
import { type ProjectId, type DepPath } from '@pnpm/types'
|
|
import { packageIdFromSnapshot } from './packageIdFromSnapshot'
|
|
import { nameVerFromPkgSnapshot } from './nameVerFromPkgSnapshot'
|
|
|
|
type GetLocalLocations = (depPath: DepPath, pkgName: string) => string[]
|
|
|
|
export function extendProjectsWithTargetDirs<T> (
|
|
projects: Array<T & { id: ProjectId }>,
|
|
lockfile: Lockfile,
|
|
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[] }>
|
|
}
|