fund.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict'
  2. const { EOL } = require('os')
  3. const computeMetadata = require('./deps.js').computeMetadata
  4. const mutateIntoLogicalTree = require('./mutate-into-logical-tree.js')
  5. var { getFundingInfo } = require('../utils/funding.js')
  6. exports.getPrintFundingReport = getPrintFundingReport
  7. exports.getPrintFundingReportJSON = getPrintFundingReportJSON
  8. function getFundingResult ({ fund, idealTree }) {
  9. if (fund) {
  10. const fundingInfoTree =
  11. mutateIntoLogicalTree.asReadInstalled(
  12. computeMetadata(idealTree)
  13. )
  14. const fundResult = getFundingInfo(fundingInfoTree, { countOnly: true })
  15. return fundResult
  16. } else {
  17. return {}
  18. }
  19. }
  20. function getPrintFundingReport ({ fund, idealTree }, opts) {
  21. const fundResult = getFundingResult({ fund, idealTree })
  22. const { length } = fundResult || {}
  23. const { json } = opts || {}
  24. function padding (msg) {
  25. return json ? '' : (EOL + msg)
  26. }
  27. function packageQuantity (amount) {
  28. return `package${amount > 1 ? 's are' : ' is'}`
  29. }
  30. if (!length) return ''
  31. return padding('') + length + ' ' +
  32. packageQuantity(length) +
  33. ' looking for funding' +
  34. padding(' run `npm fund` for details\n')
  35. }
  36. function getPrintFundingReportJSON ({ fund, idealTree }) {
  37. return getPrintFundingReport({ fund, idealTree }, { json: true })
  38. }