Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bugs.js « lib - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 306d9bb4bfc9febce0d698a47b36d1e51c44e1f6 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

module.exports = bugs

bugs.usage = "npm bugs <pkgname>"

var npm = require("./npm.js")
  , log = require("npmlog")
  , opener = require("opener")
  , path = require("path")
  , readJson = require("read-package-json")
  , npa = require("npm-package-arg")
  , fs = require("fs")
  , mapToRegistry = require("./utils/map-to-registry.js")

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()
}

function bugs (args, cb) {
  var n = args.length && npa(args[0]).name || "."
  fs.stat(n, function (er, s) {
    if (er) {
      if (er.code === "ENOENT") return callRegistry(n, cb)
      return cb(er)
    }
    if (!s.isDirectory()) return callRegistry(n, cb)
    readJson(path.resolve(n, "package.json"), function(er, d) {
      if (er) return cb(er)
      getUrlAndOpen(d, cb)
    })
  })
}

function getUrlAndOpen (d, cb) {
  var repo = d.repository || d.repositories
    , url
  if (d.bugs) {
    url = (typeof d.bugs === "string") ? d.bugs : d.bugs.url
  }
  else if (repo) {
    if (Array.isArray(repo)) repo = repo.shift()
    if (repo.hasOwnProperty("url")) repo = repo.url
    log.verbose("bugs", "repository", repo)
    if (repo && repo.match(/^(https?:\/\/|git(:\/\/|@))github.com/)) {
      url = repo.replace(/^git(@|:\/\/)/, "https://")
                .replace(/^https?:\/\/github.com:/, "https://github.com/")
                .replace(/\.git$/, "")+"/issues"
    }
  }
  if (!url) {
    url = "https://www.npmjs.org/package/" + d.name
  }
  log.silly("bugs", "url", url)
  opener(url, { command: npm.config.get("browser") }, cb)
}

function callRegistry (name, cb) {
  mapToRegistry(name, npm.config, function (er, uri, auth) {
    if (er) return cb(er)

    npm.registry.get(uri + "/latest", { auth : auth }, function (er, d) {
      if (er) return cb(er)

      getUrlAndOpen(d, cb)
    })
  })
}