Skip to content

Commit

Permalink
refactor: resolve dependencies (#7408)
Browse files Browse the repository at this point in the history
Make updates to the lockfile in one place.
  • Loading branch information
zkochan authored Dec 13, 2023
1 parent e71c0f1 commit 0830a73
Showing 1 changed file with 52 additions and 49 deletions.
101 changes: 52 additions & 49 deletions pkg-manager/resolve-dependencies/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,48 +134,8 @@ export async function resolveDependencies (
})
}

const linkedDependenciesByProjectId: Record<string, LinkedDependency[]> = {}
const projectsToLink = await Promise.all<ProjectToLink>(projectsToResolve.map(async (project, index) => {
const projectsToLink = await Promise.all<ProjectToLink>(projectsToResolve.map(async (project) => {
const resolvedImporter = resolvedImporters[project.id]
linkedDependenciesByProjectId[project.id] = resolvedImporter.linkedDependencies
let updatedManifest: ProjectManifest | undefined = project.manifest
let updatedOriginalManifest: ProjectManifest | undefined = project.originalManifest
if (project.updatePackageManifest) {
const manifests = await updateProjectManifest(project, {
directDependencies: resolvedImporter.directDependencies,
preserveWorkspaceProtocol: opts.preserveWorkspaceProtocol,
saveWorkspaceProtocol: opts.saveWorkspaceProtocol,
})
updatedManifest = manifests[0]
updatedOriginalManifest = manifests[1]
} else {
packageManifestLogger.debug({
prefix: project.rootDir,
updated: project.manifest,
})
}

if (updatedManifest != null) {
if (opts.autoInstallPeers) {
if (updatedManifest.peerDependencies) {
const allDeps = getAllDependenciesFromManifest(updatedManifest)
for (const [peerName, peerRange] of Object.entries(updatedManifest.peerDependencies)) {
if (allDeps[peerName]) continue
updatedManifest.dependencies ??= {}
updatedManifest.dependencies[peerName] = peerRange
}
}
}
const projectSnapshot = opts.wantedLockfile.importers[project.id]
opts.wantedLockfile.importers[project.id] = addDirectDependenciesToLockfile(
updatedManifest,
projectSnapshot,
resolvedImporter.linkedDependencies,
resolvedImporter.directDependencies,
opts.registries,
opts.excludeLinksFromLockfile
)
}

const topParents: Array<{ name: string, version: string, alias?: string, linkedDir?: string }> = project.manifest
? await getTopParents(
Expand All @@ -200,8 +160,6 @@ export async function resolveDependencies (
})
})

const manifest = updatedOriginalManifest ?? project.originalManifest ?? project.manifest
importers[index].manifest = manifest
return {
binsDir: project.binsDir,
directNodeIdsByAlias: resolvedImporter.directNodeIdsByAlias,
Expand All @@ -227,11 +185,55 @@ export async function resolveDependencies (
resolvePeersFromWorkspaceRoot: Boolean(opts.resolvePeersFromWorkspaceRoot),
})

for (const { id, manifest } of projectsToLink) {
for (const [alias, depPath] of Object.entries(dependenciesByProjectId[id])) {
const projectSnapshot = opts.wantedLockfile.importers[id]
if (manifest.dependenciesMeta != null) {
projectSnapshot.dependenciesMeta = manifest.dependenciesMeta
const linkedDependenciesByProjectId: Record<string, LinkedDependency[]> = {}
await Promise.all(projectsToResolve.map(async (project, index) => {
const resolvedImporter = resolvedImporters[project.id]
linkedDependenciesByProjectId[project.id] = resolvedImporter.linkedDependencies
let updatedManifest: ProjectManifest | undefined
let updatedOriginalManifest: ProjectManifest | undefined
if (project.updatePackageManifest) {
[updatedManifest, updatedOriginalManifest] = await updateProjectManifest(project, {
directDependencies: resolvedImporter.directDependencies,
preserveWorkspaceProtocol: opts.preserveWorkspaceProtocol,
saveWorkspaceProtocol: opts.saveWorkspaceProtocol,
})
} else {
updatedManifest = project.manifest
updatedOriginalManifest = project.originalManifest
packageManifestLogger.debug({
prefix: project.rootDir,
updated: project.manifest,
})
}

if (updatedManifest != null) {
if (opts.autoInstallPeers) {
if (updatedManifest.peerDependencies) {
const allDeps = getAllDependenciesFromManifest(updatedManifest)
for (const [peerName, peerRange] of Object.entries(updatedManifest.peerDependencies)) {
if (allDeps[peerName]) continue
updatedManifest.dependencies ??= {}
updatedManifest.dependencies[peerName] = peerRange
}
}
}
const projectSnapshot = opts.wantedLockfile.importers[project.id]
opts.wantedLockfile.importers[project.id] = addDirectDependenciesToLockfile(
updatedManifest,
projectSnapshot,
resolvedImporter.linkedDependencies,
resolvedImporter.directDependencies,
opts.registries,
opts.excludeLinksFromLockfile
)
}

importers[index].manifest = updatedOriginalManifest ?? project.originalManifest ?? project.manifest

for (const [alias, depPath] of Object.entries(dependenciesByProjectId[project.id])) {
const projectSnapshot = opts.wantedLockfile.importers[project.id]
if (project.manifest.dependenciesMeta != null) {
projectSnapshot.dependenciesMeta = project.manifest.dependenciesMeta
}

const depNode = dependenciesGraph[depPath]
Expand All @@ -250,7 +252,8 @@ export async function resolveDependencies (
projectSnapshot.optionalDependencies[alias] = ref
}
}
}
}))

if (opts.dedupeDirectDeps) {
const rootDeps = dependenciesByProjectId['.']
if (rootDeps) {
Expand Down

0 comments on commit 0830a73

Please sign in to comment.