-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
62 additions
and
5 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
pkg-manager/resolve-dependencies/src/getCatalogResolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters