node/deps/npm/lib/docs.js

63 lines
1.5 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 ."
docs.completion = function (opts, cb) {
2012-06-11 12:29:47 +08:00
registry.get("/-/short", 60000, function (er, list) {
return cb(null, list || [])
})
}
2013-05-15 05:37:59 +08:00
var npm = require("./npm.js")
2012-06-11 12:29:47 +08:00
, registry = npm.registry
2012-09-25 23:28:55 +08:00
, opener = require("opener")
2013-12-12 02:20:26 +08:00
, path = require('path')
, log = require('npmlog')
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)
args.forEach(function(proj) {
getDoc(proj, function(err) {
if (err) return cb(err)
--pending || cb()
})
})
}
function getDoc (project, cb) {
project = project || '.'
var package = path.resolve(process.cwd(), "package.json")
2013-12-12 02:20:26 +08:00
2013-12-26 11:15:23 +08:00
if (project === '.' || project === './') {
2013-12-12 02:20:26 +08:00
try {
var json = require(package)
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)
}
registry.get(project + "/latest", 3600, function (er, json) {
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)
})
}