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
70
71
72
73
74
75
|
module.exports = docs
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) {
if (er) return cb(er)
npm.registry.get(uri, { timeout : 60000, auth : auth }, function (er, list) {
return cb(null, list || [])
})
})
}
function url (json) {
return json.homepage ? json.homepage : "https://npmjs.org/package/" + json.name
}
function docs (args, cb) {
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(npm.localPrefix, "package.json")
if (project === "." || project === "./") {
var json
try {
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)
}
mapToRegistry(project, npm.config, function (er, uri, auth) {
if (er) return cb(er)
npm.registry.get(uri + "/latest", { timeout : 3600, auth : auth }, next)
})
function next (er, json) {
var github = "https://github.com/" + project + "#readme"
if (er) {
if (project.split("/").length !== 2) return cb(er)
return opener(github, { command: npm.config.get("browser") }, cb)
}
return opener(url(json), { command: npm.config.get("browser") }, cb)
}
}
|