diff options
author | Ruy Adorno <ruyadorno@hotmail.com> | 2019-11-20 00:32:57 +0300 |
---|---|---|
committer | Michael Perrotte <mike@npmjs.com> | 2019-12-03 20:13:43 +0300 |
commit | 1c65d26ac9f10ac0037094c207d216fbf0e969bf (patch) | |
tree | d3588f74b51b66f024df0c9f0e66d9a0cf695bb4 /lib | |
parent | e4b97962e5fce0d49beb541ce5a0f96aee0525de (diff) |
fix(fund): open url for string shorthand
Trying to open url for a package that is using the string shorthand is
currently broken using: `npm fund <pkg>`
This commit fixes the issue and adds the missing unit and integration
tests covering that usecase.
Fixes #498
PR-URL: https://github.com/npm/cli/pull/501
Credit: @ruyadorno
Close: #501
Reviewed-by: @mikemimik
Diffstat (limited to 'lib')
-rw-r--r-- | lib/fund.js | 4 | ||||
-rw-r--r-- | lib/utils/funding.js | 18 |
2 files changed, 12 insertions, 10 deletions
diff --git a/lib/fund.js b/lib/fund.js index 4981e4615..00954c844 100644 --- a/lib/fund.js +++ b/lib/fund.js @@ -14,7 +14,7 @@ const readShrinkwrap = require('./install/read-shrinkwrap.js') const mutateIntoLogicalTree = require('./install/mutate-into-logical-tree.js') const output = require('./utils/output.js') const openUrl = require('./utils/open-url.js') -const { getFundingInfo, validFundingUrl } = require('./utils/funding.js') +const { getFundingInfo, retrieveFunding, validFundingUrl } = require('./utils/funding.js') const FundConfig = figgyPudding({ browser: {}, // used by ./utils/open-url @@ -132,7 +132,7 @@ function printHuman (fundingInfo, opts) { function openFundingUrl (packageName, cb) { function getUrlAndOpen (packageMetadata) { const { funding } = packageMetadata - const { type, url } = funding || {} + const { type, url } = retrieveFunding(funding) || {} const noFundingError = new Error(`No funding method available for: ${packageName}`) noFundingError.code = 'ENOFUND' diff --git a/lib/utils/funding.js b/lib/utils/funding.js index dce401476..c3d06b108 100644 --- a/lib/utils/funding.js +++ b/lib/utils/funding.js @@ -3,8 +3,18 @@ const URL = require('url').URL exports.getFundingInfo = getFundingInfo +exports.retrieveFunding = retrieveFunding exports.validFundingUrl = validFundingUrl +// supports both object funding and string shorthand +function retrieveFunding (funding) { + return typeof funding === 'string' + ? { + url: funding + } + : funding +} + // Is the value of a `funding` property of a `package.json` // a valid type+url for `npm fund` to display? function validFundingUrl (funding) { @@ -60,14 +70,6 @@ function getFundingInfo (idealTree, opts) { ) } - function retrieveFunding (funding) { - return typeof funding === 'string' - ? { - url: funding - } - : funding - } - function getFundingDependencies (tree) { const deps = tree && tree.dependencies if (!deps) return empty() |