node/deps/npm/lib/bugs.js

75 lines
2.0 KiB
JavaScript
Raw Normal View History

module.exports = bugs
bugs.usage = "npm bugs <pkgname>"
2013-05-15 05:37:59 +08:00
var npm = require("./npm.js")
2012-06-11 12:29:47 +08:00
, log = require("npmlog")
2012-09-25 23:28:55 +08:00
, opener = require("opener")
2013-12-26 11:15:23 +08:00
, path = require("path")
, readJson = require("read-package-json")
2014-09-25 05:41:07 +08:00
, npa = require("npm-package-arg")
2013-12-26 11:15:23 +08:00
, fs = require("fs")
2014-09-25 05:41:07 +08:00
, mapToRegistry = require("./utils/map-to-registry.js")
2012-06-11 12:29:47 +08:00
bugs.completion = function (opts, cb) {
if (opts.conf.argv.remain.length > 2) return cb()
mapToRegistry("-/short", npm.config, function (er, uri, auth) {
2014-09-25 05:41:07 +08:00
if (er) return cb(er)
npm.registry.get(uri, { timeout : 60000, auth : auth }, function (er, list) {
2014-09-25 05:41:07 +08:00
return cb(null, list || [])
})
})
}
function bugs (args, cb) {
var n = args.length && npa(args[0]).name || "."
2013-12-26 11:15:23 +08:00
fs.stat(n, function (er, s) {
if (er) {
if (er.code === "ENOENT") return callRegistry(n, cb)
return cb(er)
}
2013-12-26 11:15:23 +08:00
if (!s.isDirectory()) return callRegistry(n, cb)
readJson(path.resolve(n, "package.json"), function(er, d) {
2014-02-03 12:24:09 +08:00
if (er) return cb(er)
2013-12-26 11:15:23 +08:00
getUrlAndOpen(d, cb)
})
})
}
function getUrlAndOpen (d, cb) {
var repo = d.repository || d.repositories
2013-12-26 11:15:23 +08:00
, url
if (d.bugs) {
url = (typeof d.bugs === "string") ? d.bugs : d.bugs.url
}
else if (repo) {
2013-12-26 11:15:23 +08:00
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://")
2013-12-26 11:15:23 +08:00
.replace(/^https?:\/\/github.com:/, "https://github.com/")
.replace(/\.git$/, "")+"/issues"
2013-12-26 11:15:23 +08:00
}
}
if (!url) {
url = "https://www.npmjs.org/package/" + d.name
2013-12-26 11:15:23 +08:00
}
log.silly("bugs", "url", url)
2013-12-26 11:15:23 +08:00
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)
2014-09-25 05:41:07 +08:00
npm.registry.get(uri + "/latest", { auth : auth }, function (er, d) {
2014-09-25 05:41:07 +08:00
if (er) return cb(er)
getUrlAndOpen(d, cb)
2014-09-25 05:41:07 +08:00
})
})
}