All files / lib bugs.js

100% Statements 34/34
100% Branches 14/14
100% Functions 6/6
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 481x 1x 1x 1x 1x 1x 1x   1x 1x   8x   1x 8x 1x   8x     1x 8x 5x 3x   2x 1x       4x 4x 2x     2x     1x 8x 8x 8x 8x 8x     1x  
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 [<pkgname>]')
const completion = require('./utils/completion/none.js')
 
const cmd = (args, cb) => bugs(args).then(() => cb()).catch(cb)
 
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
  }
 
  // 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, opts)
  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 })