Skip to content

Commit

Permalink
fix: Is there a way to not upgrade a package if it's peer depndencies…
Browse files Browse the repository at this point in the history
… are not currently met raineorshine#1418
  • Loading branch information
rbnayax committed Jul 7, 2024
1 parent 961172f commit c43f4d1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/lib/upgradePackageDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@ import upgradeDependencies from './upgradeDependencies'
*
* @returns
*/
const checkIfInPeerViolation = (
const checkIfInPeerViolation = async (
currentDependencies: Index<VersionSpec>,
filteredUpgradedDependencies: Index<VersionSpec>,
upgradedPeerDependencies: Index<Index<VersionSpec>>,
latestVersionResults: Index<VersionResult>,
options: Options,
) => {
const upgradedDependencies = { ...currentDependencies, ...filteredUpgradedDependencies }
const filteredLatestDependencies = pickBy(latestVersionResults, (spec, dep) => upgradedDependencies[dep])
const currentVersionResults = await queryVersions(upgradedDependencies, {
...options,
target: 'semver',
peer: false,
pre: true,
deprecated: true,
enginesNode: false,
peerDependencies: undefined,
})
const filteredLatestDependencies = pickBy(currentVersionResults, (spec, dep) => upgradedDependencies[dep])
const filteredUpgradedPeerDependencies = { ...upgradedPeerDependencies }
let wereUpgradedDependenceFiltered = false
const filteredUpgradedDependenciesAfterPeers = pickBy(filteredUpgradedDependencies, (spec, dep) => {
Expand Down Expand Up @@ -113,11 +122,11 @@ export async function upgradePackageDefinitions(

if (options.peer && Object.keys(filteredLatestDependencies).length > 0) {
const upgradedPeerDependencies = await getPeerDependenciesFromRegistry(filteredLatestDependencies, options)
const checkPeerViolationResult = checkIfInPeerViolation(
const checkPeerViolationResult = await checkIfInPeerViolation(
currentDependencies,
filteredUpgradedDependencies,
upgradedPeerDependencies,
latestVersionResults,
options,
)
if (checkPeerViolationResult.issuesFound) {
const fullRerunResult = await rerunUpgradeIfChangedPeers(
Expand All @@ -128,11 +137,11 @@ export async function upgradePackageDefinitions(
options,
)
if (fullRerunResult) {
const checkPeerViolationResultFullRerun = checkIfInPeerViolation(
const checkPeerViolationResultFullRerun = await checkIfInPeerViolation(
currentDependencies,
fullRerunResult[0],
fullRerunResult[2]!,
fullRerunResult[1],
options,
)
if (!checkPeerViolationResultFullRerun.issuesFound) {
return fullRerunResult
Expand All @@ -146,11 +155,11 @@ export async function upgradePackageDefinitions(
options,
)
if (partialRerunResult) {
const checkPeerViolationResultPartialRerun = checkIfInPeerViolation(
const checkPeerViolationResultPartialRerun = await checkIfInPeerViolation(
currentDependencies,
partialRerunResult[0],
partialRerunResult[2]!,
partialRerunResult[1],
options,
)
if (!checkPeerViolationResultPartialRerun.issuesFound) {
return partialRerunResult
Expand Down
15 changes: 15 additions & 0 deletions test/peer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,19 @@ describe('peer dependencies', function () {
const upgrades = await ncu({ cwd, peer: true })
upgrades!.should.contain.keys('@vitest/ui', 'vitest')
})

it('ignores if post upgrade peers are unmet', async () => {
const cwd = path.join(__dirname, 'test-data/peer-post-upgrade/')
const upgrades = await ncu({
cwd,
peer: true,
target: packageName => {
return packageName === 'eslint-plugin-unused-imports' ? 'latest' : 'semver'
},
})
upgrades!.should.deep.equal({
'@vitest/ui': '^1.6.0',
vitest: '^1.6.0',
})
})
})
10 changes: 10 additions & 0 deletions test/test-data/peer-post-upgrade/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"license": "MIT",
"dependencies": {
"@vitest/ui": "^1.3.1",
"vitest": "^1.3.1",
"eslint": "8.57.0",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-unused-imports": "^3"
}
}

0 comments on commit c43f4d1

Please sign in to comment.