mirror of
https://github.com/pnpm/pnpm.git
synced 2026-06-28 09:55:39 -04:00
* build: enable `@typescript-eslint/no-import-type-side-effects` * build: disable `@typescript-eslint/consistent-type-imports` * chore: apply fixes for `no-import-type-side-effects` pnpm exec eslint "**/src/**/*.ts" "**/test/**/*.ts" --fix
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import { PnpmError } from '@pnpm/error'
|
|
import type { ExtendedPatchInfo, PatchGroupRangeItem, PatchGroupRecord } from '@pnpm/patching.types'
|
|
import { satisfies } from 'semver'
|
|
|
|
class PatchKeyConflictError extends PnpmError {
|
|
constructor (
|
|
pkgName: string,
|
|
pkgVersion: string,
|
|
satisfied: Array<Pick<PatchGroupRangeItem, 'version'>>
|
|
) {
|
|
const pkgId = `${pkgName}@${pkgVersion}`
|
|
const satisfiedVersions = satisfied.map(({ version }) => version)
|
|
const message = `Unable to choose between ${satisfied.length} version ranges to patch ${pkgId}: ${satisfiedVersions.join(', ')}`
|
|
super('PATCH_KEY_CONFLICT', message, {
|
|
hint: `Explicitly set the exact version (${pkgId}) to resolve conflict`,
|
|
})
|
|
}
|
|
}
|
|
|
|
export function getPatchInfo (
|
|
patchFileGroups: PatchGroupRecord | undefined,
|
|
pkgName: string,
|
|
pkgVersion: string
|
|
): ExtendedPatchInfo | undefined {
|
|
if (!patchFileGroups?.[pkgName]) return undefined
|
|
|
|
const exactVersion = patchFileGroups[pkgName].exact[pkgVersion]
|
|
if (exactVersion) return exactVersion
|
|
|
|
const satisfied = patchFileGroups[pkgName].range.filter(item => satisfies(pkgVersion, item.version))
|
|
if (satisfied.length > 1) {
|
|
throw new PatchKeyConflictError(pkgName, pkgVersion, satisfied)
|
|
}
|
|
if (satisfied.length === 1) {
|
|
return satisfied[0].patch
|
|
}
|
|
|
|
return patchFileGroups[pkgName].all
|
|
}
|