Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 8 additions & 21 deletions lib/fund.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const archy = require('archy')
const Arborist = require('@npmcli/arborist')
const chalk = require('chalk')
const pacote = require('pacote')
const semver = require('semver')
const npa = require('npm-package-arg')
Expand All @@ -13,6 +14,7 @@ const {
} = require('libnpmfund')

const npm = require('./npm.js')
const completion = require('./utils/completion/installed-deep.js')
const output = require('./utils/output.js')
const openUrl = require('./utils/open-url.js')
const usageUtil = require('./utils/usage.js')
Expand All @@ -23,16 +25,6 @@ const usage = usageUtil(
'npm fund [--json] [--browser] [--unicode] [[<@scope>/]<pkg> [--which=<fundingSourceNumber>]'
)

const completion = (opts, cb) => {
const argv = opts.conf.argv.remain
switch (argv[2]) {
case 'fund':
return cb(null, [])
default:
return cb(new Error(argv[2] + ' not recognized'))
}
}

const cmd = (args, cb) => fund(args).then(() => cb()).catch(cb)

function printJSON (fundingInfo) {
Expand All @@ -44,7 +36,7 @@ const getPrintableName = ({ name, version }) => {
return `${name}${printableVersion}`
}

function printHuman (fundingInfo, { unicode }) {
function printHuman (fundingInfo, { color, unicode }) {
const seenUrls = new Map()

const tree = obj =>
Expand All @@ -58,7 +50,7 @@ function printHuman (fundingInfo, { unicode }) {
const { url } = funding || {}
const pkgRef = getPrintableName({ name, version })
const label = url ? tree({
label: url,
label: color ? chalk.white(url) : url,
nodes: [pkgRef]
}).trim() : pkgRef
let item = {
Expand Down Expand Up @@ -95,7 +87,8 @@ function printHuman (fundingInfo, { unicode }) {
}))
})

return tree(result)
const res = tree(result)
return color ? chalk.reset(res) : res
}

async function openFundingUrl ({ path, tree, spec, fundingSourceNumber }) {
Expand Down Expand Up @@ -126,14 +119,8 @@ async function openFundingUrl ({ path, tree, spec, fundingSourceNumber }) {
}
}

let { funding } = retrievePackageMetadata() || {}

if (!funding) {
// if still has not funding info, let's try
// fetching metadata from the registry then
const manifest = await pacote.manifest(arg, npm.flatOptions)
funding = manifest.funding
}
const { funding } = retrievePackageMetadata() ||
await pacote.manifest(arg, npm.flatOptions).catch(() => ({}))

const validSources = []
.concat(normalizeFunding(funding))
Expand Down
84 changes: 84 additions & 0 deletions tap-snapshots/test-lib-fund.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/lib/fund.js TAP fund a package with type and multiple sources > should print prompt select message 1`] = `
1: Foo funding available at the following URL: http://example.com/foo
2: Lorem funding available at the following URL: http://example.com/foo-lorem
Run \`npm fund [<@scope>/]<pkg> --which=1\`, for example, to open the first funding URL listed in that package

`

exports[`test/lib/fund.js TAP fund colors > should print output with color info 1`] = `
test-fund-colors@1.0.0
+-- http://example.com/a
| \`-- a@1.0.0
\`-- http://example.com/b
 | \`-- b@1.0.0, c@1.0.0
 +-- http://example.com/d
 | \`-- d@1.0.0
 \`-- http://example.com/e
 \`-- e@1.0.0


`

exports[`test/lib/fund.js TAP fund containing multi-level nested deps with no funding > should omit dependencies with no funding declared 1`] = `
nested-no-funding-packages@1.0.0
+-- https://example.com/lorem
| \`-- lorem@1.0.0
\`-- http://example.com/donate
\`-- bar@1.0.0


`

exports[`test/lib/fund.js TAP fund in which same maintainer owns all its deps > should print stack packages together 1`] = `
http://example.com/donate
\`-- maintainer-owns-all-deps@1.0.0, dep-foo@1.0.0, dep-sub-foo@1.0.0, dep-bar@1.0.0


`

exports[`test/lib/fund.js TAP fund pkg missing version number > should print name only 1`] = `
http://example.com/foo
\`-- foo


`

exports[`test/lib/fund.js TAP fund using nested packages with multiple sources > should prompt with all available URLs 1`] = `
1: Funding available at the following URL: https://one.example.com
2: Funding available at the following URL: https://two.example.com
Run \`npm fund [<@scope>/]<pkg> --which=1\`, for example, to open the first funding URL listed in that package

`

exports[`test/lib/fund.js TAP fund using nested packages with multiple sources, with a source number > should open the numbered URL 1`] = `
Funding available at the following URL:
https://one.example.com
`

exports[`test/lib/fund.js TAP fund using package argument > should open funding url 1`] = `
individual funding available at the following URL:
http://example.com/donate
`

exports[`test/lib/fund.js TAP fund using pkg name while having conflicting versions > should open greatest version 1`] = `
Funding available at the following URL:
http://example.com/2
`

exports[`test/lib/fund.js TAP fund using string shorthand > should open string-only url 1`] = `
Funding available at the following URL:
https://example.com/sponsor
`

exports[`test/lib/fund.js TAP fund with no package containing funding > should print empty funding info 1`] = `
no-funding-package@0.0.0


`
64 changes: 0 additions & 64 deletions tap-snapshots/test-tap-fund.js-TAP.test.js

This file was deleted.

Loading