bcbf2bebb10bc791c2cb14db279e30549075725d
[platform/upstream/nodejs.git] / deps / npm / lib / bugs.js
1
2 module.exports = bugs
3
4 bugs.usage = "npm bugs <pkgname>"
5
6 var npm = require("./npm.js")
7   , registry = npm.registry
8   , log = require("npmlog")
9   , opener = require("opener")
10
11 bugs.completion = function (opts, cb) {
12   if (opts.conf.argv.remain.length > 2) return cb()
13   registry.get("/-/short", 60000, function (er, list) {
14     return cb(null, list || [])
15   })
16 }
17
18 function bugs (args, cb) {
19   if (!args.length) return cb(bugs.usage)
20   var n = args[0].split("@").shift()
21   registry.get(n + "/latest", 3600, function (er, d) {
22     if (er) return cb(er)
23     var bugs = d.bugs
24       , repo = d.repository || d.repositories
25       , url
26     if (bugs) {
27       url = (typeof bugs === "string") ? bugs : bugs.url
28     } else if (repo) {
29       if (Array.isArray(repo)) repo = repo.shift()
30       if (repo.hasOwnProperty("url")) repo = repo.url
31       log.verbose("repository", repo)
32       if (repo && repo.match(/^(https?:\/\/|git(:\/\/|@))github.com/)) {
33         url = repo.replace(/^git(@|:\/\/)/, "https://")
34                   .replace(/^https?:\/\/github.com:/, "https://github.com/")
35                   .replace(/\.git$/, '')+"/issues"
36       }
37     }
38     if (!url) {
39       url = "https://npmjs.org/package/" + d.name
40     }
41     opener(url, { command: npm.config.get("browser") }, cb)
42   })
43 }