Skip to content

Commit

Permalink
refactor: create a separate type for DepPath
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed May 17, 2024
1 parent 53fc66a commit 347d824
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion deps/graph-builder/src/lockfileToDepGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface LockfileToDepGraphOptions {
patchedDependencies?: Record<string, PatchFile>
registries: Registries
sideEffectsCacheRead: boolean
skipped: Set<string>
skipped: Set<DepPath>
storeController: StoreController
storeDir: string
virtualStoreDir: string
Expand Down
4 changes: 3 additions & 1 deletion pkg-manager/core/src/install/allProjectsAreUpToDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@pnpm/lockfile-file'
import { refIsLocalDirectory, refIsLocalTarball, satisfiesPackageManifest } from '@pnpm/lockfile-utils'
import { safeReadPackageJsonFromDir } from '@pnpm/read-package-json'
import { refToRelative } from '@pnpm/dependency-path'
import { type DirectoryResolution, type WorkspacePackages } from '@pnpm/resolver-base'
import {
DEPENDENCIES_FIELDS,
Expand Down Expand Up @@ -99,7 +100,8 @@ async function linkedPackagesAreUpToDate (
if (!currentSpec) return true
const lockfileRef = lockfileDeps[depName]
if (refIsLocalDirectory(project.snapshot.specifiers[depName])) {
return isLocalFileDepUpdated(lockfileDir, lockfilePackages?.[lockfileRef])
const depPath = refToRelative(lockfileRef, depName)
return depPath != null && isLocalFileDepUpdated(lockfileDir, lockfilePackages?.[depPath])
}
const isLinked = lockfileRef.startsWith('link:')
if (
Expand Down
25 changes: 13 additions & 12 deletions pkg-manager/core/src/install/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { type StoreController, type TarballResolution } from '@pnpm/store-controller-types'
import { symlinkDependency } from '@pnpm/symlink-dependency'
import {
type DepPath,
type HoistedDependencies,
type Registries,
} from '@pnpm/types'
Expand All @@ -44,7 +45,7 @@ const brokenModulesLogger = logger('_broken_node_modules')
export interface LinkPackagesOptions {
currentLockfile: Lockfile
dedupeDirectDeps: boolean
dependenciesByProjectId: Record<string, Record<string, string>>
dependenciesByProjectId: Record<string, Record<string, DepPath>>
disableRelinkLocalDirDeps?: boolean
force: boolean
depsStateCache: DepsStateCache
Expand All @@ -65,7 +66,7 @@ export interface LinkPackagesOptions {
rootModulesDir: string
sideEffectsCacheRead: boolean
symlink: boolean
skipped: Set<string>
skipped: Set<DepPath>
storeController: StoreController
virtualStoreDir: string
virtualStoreDirMaxLength: number
Expand Down Expand Up @@ -174,8 +175,8 @@ export async function linkPackages (projects: ImporterToUpdate[], depGraph: Depe
const packages = opts.currentLockfile.packages ?? {}
if (opts.wantedLockfile.packages != null) {
for (const depPath in opts.wantedLockfile.packages) { // eslint-disable-line:forin
if (depGraph[depPath]) {
packages[depPath] = opts.wantedLockfile.packages[depPath]
if (depGraph[depPath as DepPath]) {
packages[depPath as DepPath] = opts.wantedLockfile.packages[depPath as DepPath]
}
}
}
Expand Down Expand Up @@ -320,7 +321,7 @@ interface LinkNewPackagesOptions {
lockfileDir: string
sideEffectsCacheRead: boolean
symlink: boolean
skipped: Set<string>
skipped: Set<DepPath>
storeController: StoreController
virtualStoreDir: string
}
Expand All @@ -336,9 +337,9 @@ async function linkNewPackages (
depGraph: DependenciesGraph,
opts: LinkNewPackagesOptions
): Promise<LinkNewPackagesResult> {
const wantedRelDepPaths = difference(Object.keys(wantedLockfile.packages ?? {}), Array.from(opts.skipped))
const wantedRelDepPaths = difference(Object.keys(wantedLockfile.packages ?? {}) as DepPath[], Array.from(opts.skipped))

let newDepPathsSet: Set<string>
let newDepPathsSet: Set<DepPath>
if (opts.force) {
newDepPathsSet = new Set(
wantedRelDepPaths
Expand Down Expand Up @@ -379,7 +380,7 @@ async function linkNewPackages (

const newDepPaths = Array.from(newDepPathsSet)

const newPkgs = props<string, DependenciesGraphNode>(newDepPaths, depGraph)
const newPkgs = props<DepPath, DependenciesGraphNode>(newDepPaths, depGraph)

await Promise.all(newPkgs.map(async (depNode) => fs.mkdir(depNode.modules, { recursive: true })))
await Promise.all([
Expand All @@ -404,15 +405,15 @@ async function linkNewPackages (
}

async function selectNewFromWantedDeps (
wantedRelDepPaths: string[],
wantedRelDepPaths: DepPath[],
currentLockfile: Lockfile,
depGraph: DependenciesGraph
): Promise<Set<string>> {
const newDeps = new Set<string>()
): Promise<Set<DepPath>> {
const newDeps = new Set<DepPath>()
const prevDeps = currentLockfile.packages ?? {}
await Promise.all(
wantedRelDepPaths.map(
async (depPath: string) => {
async (depPath) => {
const depNode = depGraph[depPath]
if (!depNode) return
const prevDep = prevDeps[depPath]
Expand Down
14 changes: 11 additions & 3 deletions pkg-manager/headless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ import {
type StoreController,
} from '@pnpm/store-controller-types'
import { symlinkDependency } from '@pnpm/symlink-dependency'
import { type DependencyManifest, type HoistedDependencies, type ProjectManifest, type Registries, DEPENDENCIES_FIELDS, type SupportedArchitectures } from '@pnpm/types'
import {
type DepPath,
type DependencyManifest,
type HoistedDependencies,
type ProjectManifest,
type Registries,
DEPENDENCIES_FIELDS,
type SupportedArchitectures,
} from '@pnpm/types'
import * as dp from '@pnpm/dependency-path'
import { symlinkAllModules } from '@pnpm/worker'
import pLimit from 'p-limit'
Expand Down Expand Up @@ -156,7 +164,7 @@ export interface HeadlessOptions {
ownLifecycleHooksStdio?: 'inherit' | 'pipe'
pendingBuilds: string[]
resolveSymlinksInInjectedDirs?: boolean
skipped: Set<string>
skipped: Set<DepPath>
enableModulesDir?: boolean
nodeLinker?: 'isolated' | 'hoisted' | 'pnp'
useGitBranchLockfile?: boolean
Expand Down Expand Up @@ -218,7 +226,7 @@ export async function headlessInstall (opts: HeadlessOptions): Promise<Installat
unsafePerm: opts.unsafePerm || false,
}

const skipped = opts.skipped || new Set<string>()
const skipped = opts.skipped || new Set<DepPath>()
const filterOpts = {
include: opts.include,
registries: opts.registries,
Expand Down
4 changes: 2 additions & 2 deletions pkg-manager/headless/src/lockfileToHoistedDepGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { type IncludedDependencies } from '@pnpm/modules-yaml'
import { packageIsInstallable } from '@pnpm/package-is-installable'
import { safeReadPackageJsonFromDir } from '@pnpm/read-package-json'
import { type SupportedArchitectures, type PatchFile, type Registries } from '@pnpm/types'
import { type DepPath, type SupportedArchitectures, type PatchFile, type Registries } from '@pnpm/types'
import {
type FetchPackageToStoreFunction,
type StoreController,
Expand Down Expand Up @@ -161,7 +161,7 @@ async function fetchDeps (
): Promise<DepHierarchy> {
const depHierarchy: Record<string, DepHierarchy> = {}
await Promise.all(Array.from(deps).map(async (dep) => {
const depPath = Array.from(dep.references)[0]
const depPath = Array.from(dep.references)[0] as DepPath
if (opts.skipped.has(depPath) || depPath.startsWith('workspace:')) return
const pkgSnapshot = opts.lockfile.packages![depPath]
if (!pkgSnapshot) {
Expand Down
4 changes: 2 additions & 2 deletions pkg-manager/hoist/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,15 @@ function hoistGraph (
})
// build the alias map and the id map
.forEach((depNode) => {
for (const [childAlias, childPath] of Object.entries<DepPath>(depNode.children)) {
for (const [childAlias, childPath] of Object.entries<DepPath | string>(depNode.children)) {
const hoist = opts.getAliasHoistType(childAlias)
if (!hoist) continue
const childAliasNormalized = childAlias.toLowerCase()
// if this alias has already been taken, skip it
if (hoistedAliases.has(childAliasNormalized)) {
continue
}
if (opts.lockfile.packages?.[childPath]?.hasBin) {
if (opts.lockfile.packages?.[childPath as DepPath]?.hasBin) {
hoistedAliasesWithBins.add(childAlias)
}
hoistedAliases.add(childAliasNormalized)
Expand Down
4 changes: 3 additions & 1 deletion reviewing/dependencies-hierarchy/src/TreeNodeId.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { type DepPath } from '@pnpm/types'

export type TreeNodeId = TreeNodeIdImporter | TreeNodeIdPackage

/**
Expand All @@ -13,7 +15,7 @@ interface TreeNodeIdImporter {
*/
interface TreeNodeIdPackage {
readonly type: 'package'
readonly depPath: string
readonly depPath: DepPath
}

export function serializeTreeNodeId (treeNodeId: TreeNodeId): string {
Expand Down

0 comments on commit 347d824

Please sign in to comment.