Skip to content

Commit 4d28bee

Browse files
committed
fix: show fund info with conflicting items
Fixed a problem when having packages with multiple entries in the arborist inventory, now it will always pick up funding info from the latest version of the package found in the inventory.
1 parent f5e1ce4 commit 4d28bee

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

lib/fund.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const archy = require('archy')
44
const Arborist = require('@npmcli/arborist')
5+
const semver = require('semver')
56
const npa = require('npm-package-arg')
67
const { breadth } = require('treeverse')
78
const {
@@ -101,7 +102,7 @@ function printHuman (fundingInfo, opts) {
101102
function openFundingUrl ({ actualTree, spec, fundingSourceNumber }) {
102103
const retrievePackageMetadata = () => {
103104
const arg = npa(spec, actualTree.path)
104-
let packageMetadata = {}
105+
let packageMetadata
105106
if (arg.type === 'directory') {
106107
if (actualTree.path === arg.where) {
107108
packageMetadata = actualTree.package
@@ -113,7 +114,14 @@ function openFundingUrl ({ actualTree, spec, fundingSourceNumber }) {
113114
}
114115
}
115116
} else {
116-
const item = actualTree.inventory.query('name', arg.name)
117+
const [ item ] = [
118+
...actualTree.inventory.values()
119+
]
120+
.filter(i => i && i.package &&
121+
i.package.name === arg.name &&
122+
semver.valid(i.package.version))
123+
.sort((a, b) => semver.rcompare(a.package.version, b.package.version))
124+
117125
if (item) {
118126
packageMetadata = item.package
119127
} else {
@@ -123,7 +131,7 @@ function openFundingUrl ({ actualTree, spec, fundingSourceNumber }) {
123131
return packageMetadata
124132
}
125133

126-
const { funding } = retrievePackageMetadata()
134+
const { funding } = retrievePackageMetadata() || {}
127135
const validSources = [].concat(normalizeFunding(funding)).filter(isValidFunding)
128136

129137
if (validSources.length === 1 || (fundingSourceNumber > 0 && fundingSourceNumber <= validSources.length)) {

tap-snapshots/test-tap-fund.js-TAP.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ http://example.com/donate
4444
4545
`
4646

47+
exports[`test/tap/fund.js TAP fund using pkg name while having conflicting versions > should open greatest version 1`] = `
48+
Funding available at the following URL:
49+
50+
http://example.com/2
51+
52+
`
53+
4754
exports[`test/tap/fund.js TAP fund using string shorthand > should open string-only url 1`] = `
4855
Funding available at the following URL:
4956

test/tap/fund.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,46 @@ const nestedMultipleFundingPackages = {
145145
}
146146
}
147147

148+
const conflictingFundingPackages = {
149+
'package.json': JSON.stringify({
150+
name: 'conflicting-funding-packages',
151+
version,
152+
dependencies: {
153+
foo: '1.0.0'
154+
},
155+
devDependencies: {
156+
bar: '1.0.0'
157+
}
158+
}),
159+
node_modules: {
160+
foo: {
161+
'package.json': JSON.stringify({
162+
name: 'foo',
163+
version: '1.0.0',
164+
funding: 'http://example.com/1'
165+
})
166+
},
167+
bar: {
168+
node_modules: {
169+
foo: {
170+
'package.json': JSON.stringify({
171+
name: 'foo',
172+
version: '2.0.0',
173+
funding: 'http://example.com/2'
174+
})
175+
}
176+
},
177+
'package.json': JSON.stringify({
178+
name: 'bar',
179+
version: '1.0.0',
180+
dependencies: {
181+
foo: '2.0.0'
182+
}
183+
})
184+
}
185+
}
186+
}
187+
148188
test('fund with no package containing funding', t => {
149189
const cwd = t.testdir({
150190
'package.json': JSON.stringify({
@@ -378,6 +418,16 @@ test('fund using nested packages with multiple sources, with a source number', t
378418
t.end()
379419
})
380420

421+
test('fund using pkg name while having conflicting versions', t => {
422+
const cwd = t.testdir(conflictingFundingPackages)
423+
const stdout = execSync(
424+
`${process.execPath} ${bin} fund foo --which=1 --no-browser`,
425+
{ cwd }
426+
)
427+
t.matchSnapshot(stdout.toString(), 'should open greatest version')
428+
t.end()
429+
})
430+
381431
test('fund using package argument with no browser, using --json option', t => {
382432
const cwd = t.testdir(maintainerOwnsAllDeps)
383433
const stdout = execSync(

0 commit comments

Comments
 (0)