1 |
|
2 | module.exports = bugs
|
3 |
|
4 | bugs.usage = "npm bugs <pkgname>"
|
5 |
|
6 | var npm = require("./npm.js")
|
7 | , log = require("npmlog")
|
8 | , opener = require("opener")
|
9 | , path = require("path")
|
10 | , readJson = require("read-package-json")
|
11 | , npa = require("npm-package-arg")
|
12 | , fs = require("fs")
|
13 | , mapToRegistry = require("./utils/map-to-registry.js")
|
14 |
|
15 | bugs.completion = function (opts, cb) {
|
16 | if (opts.conf.argv.remain.length > 2) return cb()
|
17 | mapToRegistry("-/short", npm.config, function (er, uri, auth) {
|
18 | if (er) return cb(er)
|
19 |
|
20 | npm.registry.get(uri, { timeout : 60000, auth : auth }, function (er, list) {
|
21 | return cb(null, list || [])
|
22 | })
|
23 | })
|
24 | }
|
25 |
|
26 | function bugs (args, cb) {
|
27 | var n = args.length && npa(args[0]).name || "."
|
28 | fs.stat(n, function (er, s) {
|
29 | if (er) {
|
30 | if (er.code === "ENOENT") return callRegistry(n, cb)
|
31 | return cb(er)
|
32 | }
|
33 | if (!s.isDirectory()) return callRegistry(n, cb)
|
34 | readJson(path.resolve(n, "package.json"), function(er, d) {
|
35 | if (er) return cb(er)
|
36 | getUrlAndOpen(d, cb)
|
37 | })
|
38 | })
|
39 | }
|
40 |
|
41 | function getUrlAndOpen (d, cb) {
|
42 | var repo = d.repository || d.repositories
|
43 | , url
|
44 | if (d.bugs) {
|
45 | url = (typeof d.bugs === "string") ? d.bugs : d.bugs.url
|
46 | }
|
47 | else if (repo) {
|
48 | if (Array.isArray(repo)) repo = repo.shift()
|
49 | if (repo.hasOwnProperty("url")) repo = repo.url
|
50 | log.verbose("bugs", "repository", repo)
|
51 | if (repo && repo.match(/^(https?:\/\/|git(:\/\/|@))github.com/)) {
|
52 | url = repo.replace(/^git(@|:\/\/)/, "https://")
|
53 | .replace(/^https?:\/\/github.com:/, "https://github.com/")
|
54 | .replace(/\.git$/, "")+"/issues"
|
55 | }
|
56 | }
|
57 | if (!url) {
|
58 | url = "https://www.npmjs.org/package/" + d.name
|
59 | }
|
60 | log.silly("bugs", "url", url)
|
61 | opener(url, { command: npm.config.get("browser") }, cb)
|
62 | }
|
63 |
|
64 | function callRegistry (name, cb) {
|
65 | mapToRegistry(name, npm.config, function (er, uri, auth) {
|
66 | if (er) return cb(er)
|
67 |
|
68 | npm.registry.get(uri + "/latest", { auth : auth }, function (er, d) {
|
69 | if (er) return cb(er)
|
70 |
|
71 | getUrlAndOpen(d, cb)
|
72 | })
|
73 | })
|
74 | }
|