From e23c4af1cc4149d0567f8b15bbeba594685288c9 Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 7 Jul 2020 10:20:06 -0700 Subject: rm fetch-package-metadata, refactor bugs/repo/docs - remove the now-outdated 'fetch-package-metadata' module. - refactor the `bugs`, `repo`, and `docs` commands for consistency. - add unit tests for refactored commands and new util module. - update `browser` config handling to honor `browser = false` in config files, along with command line flag. (previously only cli config was honored.) - slight improvement to `open-url` output when browser not used. --- lib/bugs.js | 72 ++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 27 deletions(-) (limited to 'lib/bugs.js') diff --git a/lib/bugs.js b/lib/bugs.js index 10300d1e1..55f81989a 100644 --- a/lib/bugs.js +++ b/lib/bugs.js @@ -1,31 +1,49 @@ -module.exports = bugs - -var log = require('npmlog') -var openUrl = require('./utils/open-url') -var fetchPackageMetadata = require('./fetch-package-metadata.js') -var usage = require('./utils/usage') - -bugs.usage = usage( - 'bugs', - 'npm bugs []' -) - -bugs.completion = function (opts, cb) { - // FIXME: there used to be registry completion here, but it stopped making - // sense somewhere around 50,000 packages on the registry - cb() -} +const log = require('npmlog') +const pacote = require('pacote') +const { promisify } = require('util') +const openUrl = promisify(require('./utils/open-url.js')) +const usageUtil = require('./utils/usage.js') +const npm = require('./npm.js') +const hostedFromMani = require('./utils/hosted-git-info-from-manifest.js') + +const usage = usageUtil('bugs', 'npm bugs []') +const completion = (opts, cb) => cb(null, []) -function bugs (args, cb) { - var n = args.length ? args[0] : '.' - fetchPackageMetadata(n, '.', {fullMetadata: true}, function (er, d) { - if (er) return cb(er) +const cmd = (args, cb) => bugs(args).then(() => cb()).catch(cb) - var url = d.bugs && ((typeof d.bugs === 'string') ? d.bugs : d.bugs.url) - if (!url) { - url = 'https://www.npmjs.org/package/' + d.name +const bugs = async args => { + if (!args || !args.length) { + args = ['.'] + } + await Promise.all(args.map(pkg => getBugs(pkg))) +} + +const getBugsUrl = mani => { + if (mani.bugs) { + if (typeof mani.bugs === 'string') { + return mani.bugs + } + if (typeof mani.bugs === 'object' && mani.bugs.url) { + return mani.bugs.url } - log.silly('bugs', 'url', url) - openUrl(url, 'bug list available at the following URL', cb) - }) + } + + // try to get it from the repo, if possible + const info = hostedFromMani(mani) + if (info) { + return info.bugs() + } + + // just send them to the website, hopefully that has some info! + return `https://www.npmjs.com/package/${mani.name}` } + +const getBugs = async pkg => { + const opts = { ...npm.flatOptions, fullMetadata: true } + const mani = await pacote.manifest(pkg, { fullMetadata: true }) + const url = getBugsUrl(mani) + log.silly('bugs', 'url', url) + await openUrl(url, `${mani.name} bug list available at the following URL`) +} + +module.exports = Object.assign(cmd, { usage, completion }) -- cgit v1.2.3