node/deps/npm/lib/docs.js

76 lines
1.9 KiB
JavaScript
Raw Normal View History

module.exports = docs
2013-12-12 02:20:26 +08:00
docs.usage = "npm docs <pkgname>"
docs.usage += "\n"
docs.usage += "npm docs ."
var npm = require("./npm.js")
, opener = require("opener")
, path = require("path")
, log = require("npmlog")
, mapToRegistry = require("./utils/map-to-registry.js")
docs.completion = function (opts, 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 || [])
})
})
}
2013-12-12 02:20:26 +08:00
function url (json) {
return json.homepage ? json.homepage : "https://npmjs.org/package/" + json.name
}
function docs (args, cb) {
2014-01-20 13:13:20 +08:00
args = args || []
var pending = args.length
if (!pending) return getDoc(".", cb)
2014-01-20 13:13:20 +08:00
args.forEach(function(proj) {
getDoc(proj, function(err) {
2014-08-01 00:05:30 +08:00
if (err) {
return cb(err)
}
2014-01-20 13:13:20 +08:00
--pending || cb()
})
})
}
function getDoc (project, cb) {
project = project || "."
2014-09-25 05:41:07 +08:00
var package = path.resolve(npm.localPrefix, "package.json")
2013-12-12 02:20:26 +08:00
if (project === "." || project === "./") {
2014-08-01 00:05:30 +08:00
var json
2013-12-12 02:20:26 +08:00
try {
2014-08-01 00:05:30 +08:00
json = require(package)
2013-12-12 02:20:26 +08:00
if (!json.name) throw new Error('package.json does not have a valid "name" property')
project = json.name
} catch (e) {
log.error(e.message)
return cb(docs.usage)
}
return opener(url(json), { command: npm.config.get("browser") }, cb)
}
mapToRegistry(project, npm.config, function (er, uri, auth) {
2014-09-25 05:41:07 +08:00
if (er) return cb(er)
npm.registry.get(uri + "/latest", { timeout : 3600, auth : auth }, next)
2014-09-25 05:41:07 +08:00
})
function next (er, json) {
2013-12-12 02:20:26 +08:00
var github = "https://github.com/" + project + "#readme"
2013-10-25 00:21:59 +08:00
if (er) {
if (project.split("/").length !== 2) return cb(er)
2013-12-12 02:20:26 +08:00
return opener(github, { command: npm.config.get("browser") }, cb)
2013-10-25 00:21:59 +08:00
}
2013-12-12 02:20:26 +08:00
return opener(url(json), { command: npm.config.get("browser") }, cb)
2014-09-25 05:41:07 +08:00
}
}