Welcome to mirror list, hosted at ThFree Co, Russian Federation.

funding.js « utils « lib - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5375639109ee2aac7d769cbf1701c27a7b020742 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict'

const URL = require('url').URL

exports.getFundingInfo = getFundingInfo
exports.retrieveFunding = retrieveFunding
exports.validFundingField = validFundingField

const flatCacheSymbol = Symbol('npm flat cache')
exports.flatCacheSymbol = flatCacheSymbol

// supports object funding and string shorthand, or an array of these
// if original was an array, returns an array; else returns the lone item
function retrieveFunding (funding) {
  const sources = [].concat(funding || []).map(item => (
    typeof item === 'string'
      ? { url: item }
      : item
  ))
  return Array.isArray(funding) ? sources : sources[0]
}

// Is the value of a `funding` property of a `package.json`
// a valid type+url for `npm fund` to display?
function validFundingField (funding) {
  if (!funding) return false

  if (Array.isArray(funding)) {
    return funding.every(f => !Array.isArray(f) && validFundingField(f))
  }

  try {
    var parsed = new URL(funding.url || funding)
  } catch (error) {
    return false
  }

  if (
    parsed.protocol !== 'https:' &&
    parsed.protocol !== 'http:'
  ) return false

  return Boolean(parsed.host)
}

const empty = () => Object.create(null)

function getFundingInfo (idealTree, opts) {
  let packageWithFundingCount = 0
  const flat = empty()
  const seen = new Set()
  const { countOnly } = opts || {}
  const _trailingDependencies = Symbol('trailingDependencies')

  function tracked (name, version) {
    const key = String(name) + String(version)
    if (seen.has(key)) {
      return true
    }
    seen.add(key)
  }

  function retrieveDependencies (dependencies) {
    const trailing = dependencies[_trailingDependencies]

    if (trailing) {
      return Object.assign(
        empty(),
        dependencies,
        trailing
      )
    }

    return dependencies
  }

  function hasDependencies (dependencies) {
    return dependencies && (
      Object.keys(dependencies).length ||
      dependencies[_trailingDependencies]
    )
  }

  function addToFlatCache (funding, dep) {
    [].concat(funding || []).forEach((f) => {
      const key = f.url
      if (!Array.isArray(flat[key])) {
        flat[key] = []
      }
      flat[key].push(dep)
    })
  }

  function attachFundingInfo (target, funding, dep) {
    if (funding && validFundingField(funding)) {
      target.funding = retrieveFunding(funding)
      if (!countOnly) {
        addToFlatCache(target.funding, dep)
      }

      packageWithFundingCount++
    }
  }

  function getFundingDependencies (tree) {
    const deps = tree && tree.dependencies
    if (!deps) return empty()

    const directDepsWithFunding = Object.keys(deps).map((key) => {
      const dep = deps[key]
      const { name, funding, version } = dep

      // avoids duplicated items within the funding tree
      if (tracked(name, version)) return empty()

      const fundingItem = {}

      if (version) {
        fundingItem.version = version
      }

      attachFundingInfo(fundingItem, funding, dep)

      return {
        dep,
        fundingItem
      }
    })

    return directDepsWithFunding.reduce((res, { dep: directDep, fundingItem }, i) => {
      if (!fundingItem || fundingItem.length === 0) return res

      // recurse
      const transitiveDependencies = directDep.dependencies &&
        Object.keys(directDep.dependencies).length > 0 &&
        getFundingDependencies(directDep)

      // if we're only counting items there's no need
      // to add all the data to the resulting object
      if (countOnly) return null

      if (hasDependencies(transitiveDependencies)) {
        fundingItem.dependencies = retrieveDependencies(transitiveDependencies)
      }

      if (fundingItem.funding && fundingItem.funding.length !== 0) {
        res[directDep.name] = fundingItem
      } else if (fundingItem.dependencies) {
        res[_trailingDependencies] =
          Object.assign(
            empty(),
            res[_trailingDependencies],
            fundingItem.dependencies
          )
      }

      return res
    }, countOnly ? null : empty())
  }

  const idealTreeDependencies = getFundingDependencies(idealTree)
  const result = {
    length: packageWithFundingCount
  }

  if (!countOnly) {
    result.name = idealTree.name || idealTree.path

    if (idealTree && idealTree.version) {
      result.version = idealTree.version
    }

    if (idealTree && idealTree.funding) {
      result.funding = retrieveFunding(idealTree.funding)
    }

    result.dependencies = retrieveDependencies(idealTreeDependencies)

    result[flatCacheSymbol] = flat
  }

  return result
}