Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gluxon committed Jan 21, 2024
1 parent bbfbc6b commit 22ce518
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
49 changes: 49 additions & 0 deletions pkg-manager/resolve-dependencies/src/getCatalogResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { PnpmError } from '@pnpm/error'
import { type Catalogs } from '@pnpm/catalog-types'

interface WantedDependency {
readonly pref: string
readonly alias: string
}

export function getCatalogResolver (catalogs: Catalogs) {
function resolveFromCatalog<T extends WantedDependency> (wantedDependency: T): T {
const catalogName = parseCatalogSpecifier(wantedDependency.pref)

if (catalogName == null) {
return wantedDependency
}

const catalogLookup = catalogs[catalogName]?.[wantedDependency.alias]
if (catalogLookup == null) {
throw new PnpmError(
'CATALOG_ENTRY_NOT_FOUND_FOR_SPEC',
`No catalog entry was found for catalog ${catalogName} and ${wantedDependency.alias}.`)
}

return {
...wantedDependency,
pref: catalogLookup,
}
}

return resolveFromCatalog
}

const CATALOG_PROTOCOL = 'catalog:'

/**
* Returns null if the given specifier does not start with 'catalog:'.
*/
function parseCatalogSpecifier (pref: string): string | 'default' | null {
if (!pref.startsWith(CATALOG_PROTOCOL)) {
return null
}

const catalogNameRaw = pref.slice(CATALOG_PROTOCOL.length).trim()

// Allow a specifier of 'catalog:' to be a short-hand for 'catalog:default'.
const catalogNameNormalized = catalogNameRaw === '' ? 'default' : catalogNameRaw

return catalogNameNormalized
}
18 changes: 13 additions & 5 deletions pkg-manager/resolve-dependencies/src/resolveDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
} from './nodeIdUtils'
import { wantedDepIsLocallyAvailable } from './wantedDepIsLocallyAvailable'
import safePromiseDefer, { type SafePromiseDefer } from 'safe-promise-defer'
import { getCatalogResolver } from './getCatalogResolver'

const dependencyResolvedLogger = logger('_dependency_resolved')

Expand Down Expand Up @@ -349,6 +350,8 @@ async function resolveDependenciesOfImporters (
ctx: ResolutionContext,
importers: ImporterToResolve[]
): Promise<ResolveDependenciesOfImportersResult> {
const catalogResolver = getCatalogResolver(ctx.catalogs)

const extendedWantedDepsForImporterPairs = importers.map((importer): [ExtendedWantedDependency[], ImporterToResolve] => {
const { wantedDependencies, options } = importer
const extendedWantedDeps = getDepsToResolve(wantedDependencies, ctx.wantedLockfile, {
Expand All @@ -367,18 +370,23 @@ async function resolveDependenciesOfImporters (
const postponedPeersResolutionQueue: PostponedPeersResolutionFunction[] = []
const pkgAddresses: PkgAddress[] = []

const resolvedDependenciesOfImporter = await Promise.all(
extendedWantedDeps.map((extendedWantedDep) => resolveDependenciesOfDependency(
async function resolveDependenciesOfImporterWantedDep (extendedWantedDep: ExtendedWantedDependency) {
return resolveDependenciesOfDependency(
ctx,
importer.preferredVersions,
{
...importer.options,
parentPkgAliases: importer.parentPkgAliases,
pickLowestVersion: pickLowestVersion && !importer.updatePackageManifest,
},
extendedWantedDep
))
)
{
...extendedWantedDep,
wantedDependency: catalogResolver(extendedWantedDep.wantedDependency),
})
}

const resolvedDependenciesOfImporter = await Promise.all(
extendedWantedDeps.map((resolveDependenciesOfImporterWantedDep)))

for (const { resolveDependencyResult, postponedPeersResolution, postponedResolution } of resolvedDependenciesOfImporter) {
if (resolveDependencyResult) {
Expand Down

0 comments on commit 22ce518

Please sign in to comment.