bugs.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const log = require('npmlog')
  2. const pacote = require('pacote')
  3. const openUrl = require('./utils/open-url.js')
  4. const hostedFromMani = require('./utils/hosted-git-info-from-manifest.js')
  5. const BaseCommand = require('./base-command.js')
  6. class Bugs extends BaseCommand {
  7. static get description () {
  8. return 'Report bugs for a package in a web browser'
  9. }
  10. static get name () {
  11. return 'bugs'
  12. }
  13. static get usage () {
  14. return ['[<pkgname>]']
  15. }
  16. /* istanbul ignore next - see test/lib/load-all-commands.js */
  17. static get params () {
  18. return ['browser', 'registry']
  19. }
  20. exec (args, cb) {
  21. this.bugs(args).then(() => cb()).catch(cb)
  22. }
  23. async bugs (args) {
  24. if (!args || !args.length)
  25. args = ['.']
  26. await Promise.all(args.map(pkg => this.getBugs(pkg)))
  27. }
  28. async getBugs (pkg) {
  29. const opts = { ...this.npm.flatOptions, fullMetadata: true }
  30. const mani = await pacote.manifest(pkg, opts)
  31. const url = this.getBugsUrl(mani)
  32. log.silly('bugs', 'url', url)
  33. await openUrl(this.npm, url, `${mani.name} bug list available at the following URL`)
  34. }
  35. getBugsUrl (mani) {
  36. if (mani.bugs) {
  37. if (typeof mani.bugs === 'string')
  38. return mani.bugs
  39. if (typeof mani.bugs === 'object' && mani.bugs.url)
  40. return mani.bugs.url
  41. if (typeof mani.bugs === 'object' && mani.bugs.email)
  42. return `mailto:${mani.bugs.email}`
  43. }
  44. // try to get it from the repo, if possible
  45. const info = hostedFromMani(mani)
  46. if (info)
  47. return info.bugs()
  48. // just send them to the website, hopefully that has some info!
  49. return `https://www.npmjs.com/package/${mani.name}`
  50. }
  51. }
  52. module.exports = Bugs