|
| 1 | +'use strict' |
| 2 | + |
| 3 | +var npm = require('./npm.js') |
| 4 | +var output = require('./utils/output.js') |
| 5 | +var readPackageTree = require('read-package-tree') |
| 6 | +var runParallelLimit = require('run-parallel-limit') |
| 7 | +var simpleGet = require('simple-get') |
| 8 | +var semver = require('semver') |
| 9 | +var hasANSI = require('has-ansi') |
| 10 | + |
| 11 | +module.exports = support |
| 12 | + |
| 13 | +const usage = require('./utils/usage') |
| 14 | +support.usage = usage( |
| 15 | + 'support', |
| 16 | + '\nnpm support [--json]' |
| 17 | +) |
| 18 | + |
| 19 | +support.completion = function (opts, cb) { |
| 20 | + const argv = opts.conf.argv.remain |
| 21 | + |
| 22 | + switch (argv[2]) { |
| 23 | + case 'support': |
| 24 | + return cb(null, []) |
| 25 | + default: |
| 26 | + return cb(new Error(argv[2] + ' not recognized')) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +function support (args, silent, cb) { |
| 31 | + readPackageTree(npm.dir, function (error, tree) { |
| 32 | + if (error) return cb(error) |
| 33 | + var supportablePackages = Array.from(findSupportablePackages(tree)) |
| 34 | + downloadSupportData(supportablePackages, function (error, data) { |
| 35 | + if (error) return cb(error) |
| 36 | + |
| 37 | + if (typeof cb !== 'function') { |
| 38 | + cb = silent |
| 39 | + silent = false |
| 40 | + } |
| 41 | + if (silent) return cb(null, data) |
| 42 | + |
| 43 | + var out |
| 44 | + var json = npm.config.get('json') |
| 45 | + if (json) { |
| 46 | + out = JSON.stringify(data, null, 2) |
| 47 | + } else { |
| 48 | + out = data |
| 49 | + .sort(function (a, b) { |
| 50 | + var comparison = a.name.localeCompare(b.name) |
| 51 | + return comparison === 0 |
| 52 | + ? semver.compare(a.version, b.version) |
| 53 | + : comparison |
| 54 | + }) |
| 55 | + .map(displaySupportData) |
| 56 | + .join('\n\n') |
| 57 | + } |
| 58 | + output(out) |
| 59 | + if (error) process.exitCode = 1 |
| 60 | + cb(error, data) |
| 61 | + }) |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +function findSupportablePackages (root) { |
| 66 | + var set = new Set() |
| 67 | + iterate(root) |
| 68 | + return set |
| 69 | + |
| 70 | + function iterate (node) { |
| 71 | + node.children.forEach(recurse) |
| 72 | + } |
| 73 | + |
| 74 | + function recurse (node) { |
| 75 | + var metadata = node.package |
| 76 | + if (metadata.support) { |
| 77 | + set.add({ |
| 78 | + name: metadata.name, |
| 79 | + version: metadata.version, |
| 80 | + homepage: metadata.homepage, |
| 81 | + repository: metadata.repository, |
| 82 | + support: metadata.support, |
| 83 | + parent: node.parent, |
| 84 | + path: node.path |
| 85 | + }) |
| 86 | + } |
| 87 | + if (node.children) iterate(node) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +function downloadSupportData (supportablePackages, cb) { |
| 92 | + var cache = new Map() |
| 93 | + var headers = { 'user-agent': npm.config.get('user-agent') } |
| 94 | + runParallelLimit(supportablePackages.map(function (entry) { |
| 95 | + return function task (done) { |
| 96 | + var url = entry.support |
| 97 | + get(url, function (error, response, projectData) { |
| 98 | + if (error) { |
| 99 | + return done(null, { |
| 100 | + url: url, |
| 101 | + error: 'could not download data' |
| 102 | + }) |
| 103 | + } |
| 104 | + if (typeof projectData !== 'object' || Array.isArray(projectData)) { |
| 105 | + return done(null, { |
| 106 | + url: url, |
| 107 | + error: 'not an object' |
| 108 | + }) |
| 109 | + } |
| 110 | + var contributors = projectData.contributors |
| 111 | + if (!Array.isArray(contributors)) { |
| 112 | + return done(null, projectData) |
| 113 | + } |
| 114 | + runParallelLimit(contributors.map(function (contributor) { |
| 115 | + return function (done) { |
| 116 | + if ( |
| 117 | + typeof contributor !== 'object' || |
| 118 | + typeof contributor.url !== 'string' |
| 119 | + ) { |
| 120 | + return setImmediate(function () { |
| 121 | + done(null, contributor) |
| 122 | + }) |
| 123 | + } |
| 124 | + get(contributor.url, function (error, response, contributorData) { |
| 125 | + if (error) { |
| 126 | + return done(null, { |
| 127 | + url: contributor.url, |
| 128 | + error: error |
| 129 | + }) |
| 130 | + } |
| 131 | + contributorData.url = contributor.url |
| 132 | + done(null, contributorData) |
| 133 | + }) |
| 134 | + } |
| 135 | + }), 5, function (error, resolvedContributors) { |
| 136 | + if (error) return done(error) |
| 137 | + done(null, { |
| 138 | + name: entry.name, |
| 139 | + version: entry.version, |
| 140 | + url: entry.support, |
| 141 | + homepage: entry.homepage, |
| 142 | + contributors: resolvedContributors |
| 143 | + }) |
| 144 | + }) |
| 145 | + }) |
| 146 | + } |
| 147 | + }), 5, cb) |
| 148 | + |
| 149 | + function get (url, cb) { |
| 150 | + var cached = cache.get(url) |
| 151 | + if (cached) { |
| 152 | + return setImmediate(function () { |
| 153 | + cb(null, {cached: true}, cached) |
| 154 | + }) |
| 155 | + } |
| 156 | + simpleGet.concat({ |
| 157 | + url: url, |
| 158 | + json: true, |
| 159 | + headers: headers |
| 160 | + }, cb) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +function displaySupportData (entry) { |
| 165 | + var returned = [entry.name + '@' + entry.version] |
| 166 | + if (looksLikeURL(entry.homepage)) { |
| 167 | + returned[0] += ' (' + entry.homepage + ')' |
| 168 | + } |
| 169 | + if (Array.isArray(entry.contributors)) { |
| 170 | + entry.contributors.forEach(function (contributor) { |
| 171 | + var name = contributor.name |
| 172 | + if (looksLikeSafeString(name)) { |
| 173 | + var item = ['- ' + name] |
| 174 | + var email = contributor.email |
| 175 | + if (looksLikeSafeString(email)) { |
| 176 | + item[0] += ' <' + email + '>' |
| 177 | + } |
| 178 | + var homepage = contributor.homepage |
| 179 | + if (looksLikeURL(homepage)) { |
| 180 | + item[0] += ' (' + homepage + ')' |
| 181 | + } |
| 182 | + var links = contributor.links |
| 183 | + if (Array.isArray(links)) { |
| 184 | + links.forEach(function (link) { |
| 185 | + if (looksLikeURL(link)) item.push(' ' + link) |
| 186 | + }) |
| 187 | + } |
| 188 | + returned.push(item.join('\n')) |
| 189 | + } |
| 190 | + }) |
| 191 | + } |
| 192 | + return returned.join('\n') |
| 193 | +} |
| 194 | + |
| 195 | +function looksLikeSafeString (argument) { |
| 196 | + return ( |
| 197 | + typeof argument === 'string' && |
| 198 | + argument.length > 0 && |
| 199 | + argument.length < 80 && |
| 200 | + !hasANSI(argument) |
| 201 | + ) |
| 202 | +} |
| 203 | + |
| 204 | +function looksLikeURL (argument) { |
| 205 | + return ( |
| 206 | + looksLikeSafeString(argument) && |
| 207 | + ( |
| 208 | + argument.indexOf('https://') === 0 || |
| 209 | + argument.indexOf('http://') === 0 |
| 210 | + ) |
| 211 | + ) |
| 212 | +} |
0 commit comments