This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
view.js
203 lines (194 loc) · 5.84 KB
/
view.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// npm view [pkg [pkg ...]]
module.exports = view
view.usage = "npm view pkg[@version] [<field>[.subfield]...]"
view.completion = function (args, index, cb) {
var registry = require("./utils/registry")
, getCompletions = require("./utils/completion/get-completions")
, remotePkgs = require("./utils/completion/remote-packages")
, name = args[0] || ""
, key = args[1] || ""
if (index < 3) remotePkgs(args, index, true, false, false, cb)
else if (index == 3) {
var pieces = name.split("@")
registry.get(pieces[0], pieces[1], 3600, function (er, d) {
var cur = d.versions ? d.versions[d["dist-tags"].latest] : d
, path = key.split(".")
, options
if (path.length > 1) {
for (var i = 0; i < path.length; i++) {
if (!cur) return cb()
if (!cur[path[i]]) break
else cur = cur[path[i]]
}
options = Object.keys(cur).map(function (e) {
return path.slice(0, path.length - 1).join(".") + "." + e
})
} else options = Object.keys(cur).concat(["ctime","mtime","maintainers"])
cb(null, getCompletions(key, options))
})
}
}
var registry = require("./utils/registry")
, ini = require("./utils/ini-parser")
, log = require("./utils/log")
, sys = require("./utils/sys")
, output = require("./utils/output")
, npm = require("../npm")
, semver = require("semver")
, readJson = require("./utils/read-json")
function view (args, silent, cb) {
if (typeof cb !== "function") cb = silent, silent = false
if (!args.length) return cb("Usage: "+view.usage)
var pkg = args.shift()
, nv = pkg.split("@")
, name = nv.shift()
, version = nv.join("@") || npm.config.get("tag")
// get the data about this package
registry.get(name, null, 600, function (er, data) {
if (er) return cb(er)
if (data["dist-tags"].hasOwnProperty(version)) {
version = data["dist-tags"][version]
}
var results = []
, error = null
if (!args.length) args = [""]
Object.keys(data.versions).forEach(function (v) {
try {
data.versions[v] = readJson.processJson(data.versions[v])
} catch (ex) {
delete data.versions[v]
}
if (semver.satisfies(v, version)) args.forEach(function (args) {
results.push(showFields(data, data.versions[v], args))
})
})
results = results.reduce(reducer, {})
if (error || silent) cb(error, results)
else printData(results, cb)
})
}
function reducer (l, r) {
if (r) Object.keys(r).forEach(function (v) {
l[v] = l[v] || {}
Object.keys(r[v]).forEach(function (t) {
l[v][t] = r[v][t]
})
})
return l
}
// return whatever was printed
function showFields (data, version, fields) {
var o = {}
;[data,version].forEach(function (s) {
Object.keys(s).forEach(function (k) {
o[k] = s[k]
})
})
return search(o, fields.split("."), version._id, fields)
}
function search (data, fields, version, title) {
var field
, tail = fields
while (!field && fields.length) field = tail.shift()
fields = [field].concat(tail)
if (!field && !tail.length) {
var o = {}
o[version] = {}
o[version][title] = data
return o
}
var index = field.match(/(.+)\[([0-9]+)\]$/)
if (index) {
field = index[1]
index = index[2]
if (Array.isArray(data[field]) && index < data[field].length) {
return search(data[field][index], tail, version, title)
} else {
field = field + "[" + index + "]"
}
}
if (Array.isArray(data)) {
if (data.length === 1) {
return search(data[0], fields, version, title)
}
var results = []
, res = null
data.forEach(function (data, i) {
var tl = title.length
, newt = title.substr(0, tl-(fields.join(".").length) - 1)
+ "["+i+"]" + [""].concat(fields).join(".")
results.push(search(data, fields.slice(), version, newt))
})
results = results.reduce(reducer, {})
return results
}
if (!data.hasOwnProperty(field)) {
return
}
data = data[field]
if (tail.length) {
if (typeof data === "object") {
// there are more fields to deal with.
return search(data, tail, version, title)
} else {
return new Error("Not an object: "+data)
}
}
var o = {}
o[version] = {}
o[version][title] = data
return o
}
function printData (data, cb) {
var outfd = npm.config.get("outfd")
, versions = Object.keys(data)
, msg = ""
, showVersions = versions.length > 1
, showFields
function cb_ (er) { return cb(er, data) }
versions.forEach(function (v, i) {
var fields = Object.keys(data[v])
showFields = showFields || (fields.length > 1)
fields.forEach(function (f) {
var d = cleanup(data[v][f])
if (showVersions || showFields || typeof d !== "string") {
d = sys.inspect(cleanup(data[v][f]), false, 5, true)
}
if (f && showFields) f += " = "
if (d.indexOf("\n") !== -1) f += "\n"
msg += (showVersions ? v + " " : "") + (showFields ? f : "") + d + "\n"
})
})
output.write(outfd, msg, cb_)
}
function cleanup (data) {
if (Array.isArray(data)) {
if (data.length === 1) {
data = data[0]
} else {
return data.map(cleanup)
}
}
if (!data || typeof data !== "object") return data
delete data.versions
var keys = Object.keys(data)
keys.forEach(function (d) {
if (d.charAt(0) === "_") delete data[d]
else if (typeof data[d] === "object") data[d] = cleanup(data[d])
})
keys = Object.keys(data)
if (keys.length <= 3
&& data.name
&& (keys.length === 1
|| keys.length === 3 && data.email && data.url
|| keys.length === 2 && (data.email || data.url))) {
data = unparsePerson(data)
}
return data
}
function unparsePerson (d) {
if (typeof d === "string") return d
return d.name
+ (d.email ? " <"+d.email+">" : "")
+ (d.url ? " ("+d.url+")" : "")
}