|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const path = require('path') |
| 4 | + |
| 5 | +const archy = require('archy') |
| 6 | +const figgyPudding = require('figgy-pudding') |
| 7 | +const readPackageTree = require('read-package-tree') |
| 8 | + |
| 9 | +const npm = require('./npm.js') |
| 10 | +const npmConfig = require('./config/figgy-config.js') |
| 11 | +const fetchPackageMetadata = require('./fetch-package-metadata.js') |
| 12 | +const computeMetadata = require('./install/deps.js').computeMetadata |
| 13 | +const readShrinkwrap = require('./install/read-shrinkwrap.js') |
| 14 | +const mutateIntoLogicalTree = require('./install/mutate-into-logical-tree.js') |
| 15 | +const output = require('./utils/output.js') |
| 16 | +const openUrl = require('./utils/open-url.js') |
| 17 | +const { getFundingInfo, validFundingUrl } = require('./utils/funding.js') |
| 18 | + |
| 19 | +const FundConfig = figgyPudding({ |
| 20 | + browser: {}, // used by ./utils/open-url |
| 21 | + global: {}, |
| 22 | + json: {}, |
| 23 | + unicode: {} |
| 24 | +}) |
| 25 | + |
| 26 | +module.exports = fundCmd |
| 27 | + |
| 28 | +const usage = require('./utils/usage') |
| 29 | +fundCmd.usage = usage( |
| 30 | + 'fund', |
| 31 | + 'npm fund [--json]', |
| 32 | + 'npm fund [--browser] [[<@scope>/]<pkg>' |
| 33 | +) |
| 34 | + |
| 35 | +fundCmd.completion = function (opts, cb) { |
| 36 | + const argv = opts.conf.argv.remain |
| 37 | + switch (argv[2]) { |
| 38 | + case 'fund': |
| 39 | + return cb(null, []) |
| 40 | + default: |
| 41 | + return cb(new Error(argv[2] + ' not recognized')) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +function printJSON (fundingInfo) { |
| 46 | + return JSON.stringify(fundingInfo, null, 2) |
| 47 | +} |
| 48 | + |
| 49 | +// the human-printable version does some special things that turned out to |
| 50 | +// be very verbose but hopefully not hard to follow: we stack up items |
| 51 | +// that have a shared url/type and make sure they're printed at the highest |
| 52 | +// level possible, in that process they also carry their dependencies along |
| 53 | +// with them, moving those up in the visual tree |
| 54 | +function printHuman (fundingInfo, opts) { |
| 55 | + // mapping logic that keeps track of seen items in order to be able |
| 56 | + // to push all other items from the same type/url in the same place |
| 57 | + const seen = new Map() |
| 58 | + |
| 59 | + function seenKey ({ type, url } = {}) { |
| 60 | + return url ? String(type) + String(url) : null |
| 61 | + } |
| 62 | + |
| 63 | + function setStackedItem (funding, result) { |
| 64 | + const key = seenKey(funding) |
| 65 | + if (key && !seen.has(key)) seen.set(key, result) |
| 66 | + } |
| 67 | + |
| 68 | + function retrieveStackedItem (funding) { |
| 69 | + const key = seenKey(funding) |
| 70 | + if (key && seen.has(key)) return seen.get(key) |
| 71 | + } |
| 72 | + |
| 73 | + // --- |
| 74 | + |
| 75 | + const getFundingItems = (fundingItems) => |
| 76 | + Object.keys(fundingItems || {}).map((fundingItemName) => { |
| 77 | + // first-level loop, prepare the pretty-printed formatted data |
| 78 | + const fundingItem = fundingItems[fundingItemName] |
| 79 | + const { version, funding } = fundingItem |
| 80 | + const { type, url } = funding || {} |
| 81 | + |
| 82 | + const printableVersion = version ? `@${version}` : '' |
| 83 | + const printableType = type && { label: `type: ${funding.type}` } |
| 84 | + const printableUrl = url && { label: `url: ${funding.url}` } |
| 85 | + const result = { |
| 86 | + fundingItem, |
| 87 | + label: fundingItemName + printableVersion, |
| 88 | + nodes: [] |
| 89 | + } |
| 90 | + |
| 91 | + if (printableType) { |
| 92 | + result.nodes.push(printableType) |
| 93 | + } |
| 94 | + |
| 95 | + if (printableUrl) { |
| 96 | + result.nodes.push(printableUrl) |
| 97 | + } |
| 98 | + |
| 99 | + setStackedItem(funding, result) |
| 100 | + |
| 101 | + return result |
| 102 | + }).reduce((res, result) => { |
| 103 | + // recurse and exclude nodes that are going to be stacked together |
| 104 | + const { fundingItem } = result |
| 105 | + const { dependencies, funding } = fundingItem |
| 106 | + const items = getFundingItems(dependencies) |
| 107 | + const stackedResult = retrieveStackedItem(funding) |
| 108 | + items.forEach(i => result.nodes.push(i)) |
| 109 | + |
| 110 | + if (stackedResult && stackedResult !== result) { |
| 111 | + stackedResult.label += `, ${result.label}` |
| 112 | + items.forEach(i => stackedResult.nodes.push(i)) |
| 113 | + return res |
| 114 | + } |
| 115 | + |
| 116 | + res.push(result) |
| 117 | + |
| 118 | + return res |
| 119 | + }, []) |
| 120 | + |
| 121 | + const [ result ] = getFundingItems({ |
| 122 | + [fundingInfo.name]: { |
| 123 | + dependencies: fundingInfo.dependencies, |
| 124 | + funding: fundingInfo.funding, |
| 125 | + version: fundingInfo.version |
| 126 | + } |
| 127 | + }) |
| 128 | + |
| 129 | + return archy(result, '', { unicode: opts.unicode }) |
| 130 | +} |
| 131 | + |
| 132 | +function openFundingUrl (packageName, cb) { |
| 133 | + function getUrlAndOpen (packageMetadata) { |
| 134 | + const { funding } = packageMetadata |
| 135 | + const { type, url } = funding || {} |
| 136 | + const noFundingError = |
| 137 | + new Error(`No funding method available for: ${packageName}`) |
| 138 | + noFundingError.code = 'ENOFUND' |
| 139 | + const typePrefix = type ? `${type} funding` : 'Funding' |
| 140 | + const msg = `${typePrefix} available at the following URL` |
| 141 | + |
| 142 | + if (validFundingUrl(funding)) { |
| 143 | + openUrl(url, msg, cb) |
| 144 | + } else { |
| 145 | + throw noFundingError |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + fetchPackageMetadata( |
| 150 | + packageName, |
| 151 | + '.', |
| 152 | + { fullMetadata: true }, |
| 153 | + function (err, packageMetadata) { |
| 154 | + if (err) return cb(err) |
| 155 | + getUrlAndOpen(packageMetadata) |
| 156 | + } |
| 157 | + ) |
| 158 | +} |
| 159 | + |
| 160 | +function fundCmd (args, cb) { |
| 161 | + const opts = FundConfig(npmConfig()) |
| 162 | + const dir = path.resolve(npm.dir, '..') |
| 163 | + const packageName = args[0] |
| 164 | + |
| 165 | + if (opts.global) { |
| 166 | + const err = new Error('`npm fund` does not support globals') |
| 167 | + err.code = 'EFUNDGLOBAL' |
| 168 | + throw err |
| 169 | + } |
| 170 | + |
| 171 | + if (packageName) { |
| 172 | + openFundingUrl(packageName, cb) |
| 173 | + return |
| 174 | + } |
| 175 | + |
| 176 | + readPackageTree(dir, function (err, tree) { |
| 177 | + if (err) { |
| 178 | + process.exitCode = 1 |
| 179 | + return cb(err) |
| 180 | + } |
| 181 | + |
| 182 | + readShrinkwrap.andInflate(tree, function () { |
| 183 | + const fundingInfo = getFundingInfo( |
| 184 | + mutateIntoLogicalTree.asReadInstalled( |
| 185 | + computeMetadata(tree) |
| 186 | + ) |
| 187 | + ) |
| 188 | + |
| 189 | + const print = opts.json |
| 190 | + ? printJSON |
| 191 | + : printHuman |
| 192 | + |
| 193 | + output( |
| 194 | + print( |
| 195 | + fundingInfo, |
| 196 | + opts |
| 197 | + ) |
| 198 | + ) |
| 199 | + cb(err, tree) |
| 200 | + }) |
| 201 | + }) |
| 202 | +} |
0 commit comments