node/deps/npm/lib/docs.js

52 lines
1.3 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) {
if (opts.conf.argv.remain.length > 2) return 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) {
2013-12-12 02:20:26 +08:00
var project = args[0] || '.'
, package = path.resolve(process.cwd(), "package.json")
if (project === '.') {
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)
})
}