({ path, tree, spec, fundingSourceNumber })
| 147 | } |
| 148 | |
| 149 | async openFundingUrl ({ path, tree, spec, fundingSourceNumber }) { |
| 150 | const arg = npa(spec, path) |
| 151 | |
| 152 | const retrievePackageMetadata = () => { |
| 153 | if (arg.type === 'directory') { |
| 154 | if (tree.path === arg.fetchSpec) { |
| 155 | // matches cwd, e.g: npm fund . |
| 156 | return tree.package |
| 157 | } else { |
| 158 | // matches any file path within current arborist inventory |
| 159 | for (const item of tree.inventory.values()) { |
| 160 | if (item.path === arg.fetchSpec) { |
| 161 | return item.package |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } else { |
| 166 | // tries to retrieve a package from arborist inventory by matching resulted package name from the provided spec |
| 167 | const [item] = [...tree.inventory.query('name', arg.name)] |
| 168 | .filter(i => semver.valid(i.package.version)) |
| 169 | .sort((a, b) => semver.rcompare(a.package.version, b.package.version)) |
| 170 | |
| 171 | if (item) { |
| 172 | return item.package |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | const { funding } = |
| 178 | retrievePackageMetadata() || |
| 179 | (await pacote.manifest(arg, this.npm.flatOptions).catch(() => ({}))) |
| 180 | |
| 181 | const validSources = [].concat(normalizeFunding(funding)).filter(isValidFunding) |
| 182 | |
| 183 | if (!validSources.length) { |
| 184 | throw errCode(`No valid funding method available for: ${spec}`, 'ENOFUND') |
| 185 | } |
| 186 | |
| 187 | const fundSource = fundingSourceNumber |
| 188 | ? validSources[fundingSourceNumber - 1] |
| 189 | : validSources.length === 1 ? validSources[0] |
| 190 | : null |
| 191 | |
| 192 | if (fundSource) { |
| 193 | return openUrl(this.npm, ...this.urlMessage(fundSource)) |
| 194 | } |
| 195 | |
| 196 | const ambiguousUrlMsg = [ |
| 197 | ...validSources.map((s, i) => `${i + 1}: ${this.urlMessage(s).reverse().join(': ')}`), |
| 198 | `Run ${this.usageMessage({ which: '1' })}` + |
| 199 | ', for example, to open the first funding URL listed in that package', |
| 200 | ] |
| 201 | if (fundingSourceNumber) { |
| 202 | ambiguousUrlMsg.unshift(`--which=${fundingSourceNumber} is not a valid index`) |
| 203 | } |
| 204 | output.standard(ambiguousUrlMsg.join('\n')) |
| 205 | } |
| 206 |
no test coverage detected