Skip to content

Commit b718b0e

Browse files
claudiahdzisaacs
authored andcommitted
fix: display multiple versions when --json on npm view
PR-URL: #1657 Credit: @claudiahdz Close: #1657 Reviewed-by: @isaacs
1 parent 2820af1 commit b718b0e

3 files changed

Lines changed: 387 additions & 328 deletions

File tree

lib/view.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ const fetchAndRead = async (nv, args, opts) => {
128128

129129
Object.keys(versions).forEach(function (v) {
130130
if (semver.satisfies(v, version, true)) {
131-
args.forEach(args => {
131+
args.forEach(arg => {
132132
// remove readme unless we asked for it
133133
if (args.indexOf('readme') !== -1) {
134134
delete versions[v].readme
135135
}
136-
results.push(showFields(pckmnt, versions[v], args))
136+
results.push(showFields(pckmnt, versions[v], arg))
137137
})
138138
}
139139
})
@@ -400,7 +400,11 @@ async function printData (data, name, opts) {
400400
const k = Object.keys(msgJson[0])[0]
401401
msgJson = msgJson.map(function (m) { return m[k] })
402402
}
403-
msg = JSON.stringify(msgJson[0], null, 2) + '\n'
403+
if (msgJson.length === 1) {
404+
msg = JSON.stringify(msgJson[0], null, 2) + '\n'
405+
} else if (msgJson.length > 1) {
406+
msg = JSON.stringify(msgJson, null, 2) + '\n'
407+
}
404408
}
405409

406410
// disable the progress bar entirely, as we can't meaningfully update it if

tap-snapshots/test-lib-view.js-TAP.test.js

Lines changed: 110 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -38,231 +38,215 @@ exports[`test/lib/view.js TAP should log info by field name maintainers with ema
3838
exports[`test/lib/view.js TAP should log info by field name maintainers with url > must match snapshot 1`] = `
3939
4040
[
41-
"claudia (http://c.yellow.com)",
42-
"isaacs (http://i.yellow.com)"
41+
"claudia (http://c.pink.com)",
42+
"isaacs (http://i.pink.com)"
4343
]
4444
`
4545

4646
exports[`test/lib/view.js TAP should log info by field name nested field with brackets > must match snapshot 1`] = `
4747
48-
"Jeremy Ashkenas"
48+
"123"
4949
`
5050

5151
exports[`test/lib/view.js TAP should log info by field name readme > must match snapshot 1`] = `
5252
53-
53+
a very useful readme
5454
`
5555

5656
exports[`test/lib/view.js TAP should log info by field name several fields > must match snapshot 1`] = `
5757
5858
{
59-
"name": "underscore",
60-
"version": "1.3.1"
59+
"name": "yellow",
60+
"version": "1.0.0"
6161
}
6262
`
6363

6464
exports[`test/lib/view.js TAP should log info by field name several fields with several versions > must match snapshot 1`] = `
6565
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-
exports[`test/lib/view.js TAP should log info by field name unknown nested field > must match snapshot 1`] = `
95-
96-
66+
67+
68+
9769
`
9870

9971
exports[`test/lib/view.js TAP should log info of package in current working dir non-specific version > must match snapshot 1`] = `
10072
10173
102-
underscore@1.3.1 | Proprietary | deps: none | versions: 26
103-
JavaScript's functional programming helper library.
104-
http://documentcloud.github.com/underscore/
74+
blue@1.0.0 | Proprietary | deps: none | versions: 2
10575
10676
dist
107-
.tarball:http://localhost:1331/underscore/-/underscore-1.3.1.tgz
108-
.shasum:6cb8aad0e77eb5dbbfb54b22bcd8697309cf9641
109-
110-
maintainers:
111-
-jashkenas <[[email protected]>
77+
.tarball:http://hm.blue.com/1.0.0.tgz
78+
.shasum:123
79+
.integrity:---
80+
.unpackedSize:1 B
11281
11382
dist-tags:
114-
latest: 1.5.1
115-
stable: 1.5.1
11683
117-
published over a year ago by jashkenas <[[email protected]>
84+
85+
published a year ago
11886
`
11987

12088
exports[`test/lib/view.js TAP should log info of package in current working dir specific version > must match snapshot 1`] = `
12189
12290
123-
underscore@1.3.1 | Proprietary | deps: none | versions: 26
124-
JavaScript's functional programming helper library.
125-
http://documentcloud.github.com/underscore/
91+
blue@1.0.0 | Proprietary | deps: none | versions: 2
12692
12793
dist
128-
.tarball:http://localhost:1331/underscore/-/underscore-1.3.1.tgz
129-
.shasum:6cb8aad0e77eb5dbbfb54b22bcd8697309cf9641
130-
131-
maintainers:
132-
-jashkenas <[[email protected]>
94+
.tarball:http://hm.blue.com/1.0.0.tgz
95+
.shasum:123
96+
.integrity:---
97+
.unpackedSize:1 B
13398
13499
dist-tags:
135-
latest: 1.5.1
136-
stable: 1.5.1
137-
138-
published over a year ago by jashkenas <[[email protected]>
139-
`
140-
141-
exports[`test/lib/view.js TAP should log package info [email protected] > must match snapshot 1`] = `
142-
143100
144-
mkdirp@0.3.5 | MIT | deps: none | versions: 17
145-
Recursively mkdir, like \`mkdir -p\`
146101
147-
dist
148-
.tarball:http://localhost:1331/mkdirp/-/mkdirp-0.3.5.tgz
149-
.shasum:de3e5f8961c88c787ee1368df849ac4413eca8d7
102+
published a year ago
103+
`
150104

151-
maintainers:
152-
-substack <[[email protected]>
105+
exports[`test/lib/view.js TAP should log package info package with --json and semver range > must match snapshot 1`] = `
153106
154-
dist-tags:
155-
latest: 0.3.5
156-
157-
published over a year ago by substack <[[email protected]>
107+
[
108+
{
109+
"_npmUser": "claudia <[email protected]>",
110+
"name": "cyan",
111+
"dist-tags": {},
112+
"versions": [
113+
"1.0.0",
114+
"1.0.1"
115+
],
116+
"version": "1.0.0",
117+
"dist": {
118+
"shasum": "123",
119+
"tarball": "http://hm.cyan.com/1.0.0.tgz",
120+
"integrity": "---",
121+
"fileCount": 1,
122+
"unpackedSize": 1
123+
}
124+
},
125+
{
126+
"_npmUser": "claudia <[email protected]>",
127+
"name": "cyan",
128+
"dist-tags": {},
129+
"versions": [
130+
"1.0.0",
131+
"1.0.1"
132+
]
133+
}
134+
]
158135
`
159136

160137
exports[`test/lib/view.js TAP should log package info package with homepage > must match snapshot 1`] = `
161138
162139
163-
[4m[1m[32morange[39m@[32mundefined[39m[22m[24m | [1m[31mProprietary[39m[22m | deps: [32mnone[39m | versions: [33m2[39m
140+
[4m[1m[32morange[39m@[32m1.0.0[39m[22m[24m | [1m[31mProprietary[39m[22m | deps: [32mnone[39m | versions: [33m2[39m
164141
http://hm.orange.com
165142
166143
dist
167-
.tarball:[36mundefined[39m
168-
.shasum:[33mundefined[39m
144+
.tarball:[36mhttp://hm.orange.com/1.0.0.tgz[39m
145+
.shasum:[33m123[39m
169146
.integrity:---
170147
.unpackedSize:1 B
171148
172149
dist-tags:
173-
[1m[32m1.0.1[39m[22m: [object Object]
150+
[1m[32mlatest[39m[22m: 1.0.0
174151
`
175152

176153
exports[`test/lib/view.js TAP should log package info package with license, bugs, repository and other fields > must match snapshot 1`] = `
177154
178155
179-
green@undefined | ACME | deps: 2 | versions: 2
156+
green@1.0.0 | ACME | deps: 2 | versions: 2
157+
green is a very important color
180158
181-
[1m[31mDEPRECATED[39m[22m ⚠️ - true
159+
[1m[31mDEPRECATED[39m[22m!! - true
182160
183161
keywords:colors, green, crayola
184162
185163
bin:green
186164
187165
dist
188-
.tarball:[36mundefined[39m
189-
.shasum:[33mundefined[39m
166+
.tarball:[36mhttp://hm.green.com/1.0.0.tgz[39m
167+
.shasum:[33m123[39m
190168
.integrity:---
191169
.unpackedSize:1 B
192170
193171
dependencies:
194-
red: [object Object]
195-
yellow: [object Object]
172+
red: 1.0.0
173+
yellow: 1.0.0
174+
175+
maintainers:
176+
-claudia <[[email protected]>
177+
-isaacs <[[email protected]>
196178
197179
dist-tags:
198-
[1m[32m1.0.1[39m[22m: [object Object]
180+
[1m[32mlatest[39m[22m: 1.0.0
199181
`
200182

201183
exports[`test/lib/view.js TAP should log package info package with maintainers info as object > must match snapshot 1`] = `
202184
203185
204-
[4m[1m[32mpink[39m@[32mundefined[39m[22m[24m | [1m[31mProprietary[39m[22m | deps: [32mnone[39m | versions: [33m2[39m
186+
[4m[1m[32mpink[39m@[32m1.0.0[39m[22m[24m | [1m[31mProprietary[39m[22m | deps: [32mnone[39m | versions: [33m2[39m
205187
206188
dist
207-
.tarball:[36mundefined[39m
208-
.shasum:[33mundefined[39m
189+
.tarball:[36mhttp://hm.pink.com/1.0.0.tgz[39m
190+
.shasum:[33m123[39m
209191
.integrity:---
210192
.unpackedSize:1 B
211193
212194
dist-tags:
213-
[1m[32m1.0.1[39m[22m: [object Object]
195+
[1m[32mlatest[39m[22m: 1.0.0
214196
`
215197

216198
exports[`test/lib/view.js TAP should log package info package with more than 25 deps > must match snapshot 1`] = `
217199
218200
219-
[4m[1m[32mblack[39m@[32mundefined[39m[22m[24m | [1m[31mProprietary[39m[22m | deps: [36m25[39m | versions: [33m2[39m
201+
[4m[1m[32mblack[39m@[32m1.0.0[39m[22m[24m | [1m[31mProprietary[39m[22m | deps: [36m25[39m | versions: [33m2[39m
220202
221203
dist
222-
.tarball:[36mundefined[39m
223-
.shasum:[33mundefined[39m
204+
.tarball:[36mhttp://hm.black.com/1.0.0.tgz[39m
205+
.shasum:[33m123[39m
224206
.integrity:---
225207
.unpackedSize:1 B
226208
227209
dependencies:
228-
[33m0[39m: [object Object]
229-
[33m10[39m: [object Object]
230-
[33m11[39m: [object Object]
231-
[33m12[39m: [object Object]
232-
[33m13[39m: [object Object]
233-
[33m14[39m: [object Object]
234-
[33m15[39m: [object Object]
235-
[33m16[39m: [object Object]
236-
[33m17[39m: [object Object]
237-
[33m18[39m: [object Object]
238-
[33m19[39m: [object Object]
239-
[33m1[39m: [object Object]
240-
[33m20[39m: [object Object]
241-
[33m21[39m: [object Object]
242-
[33m22[39m: [object Object]
243-
[33m23[39m: [object Object]
244-
[33m2[39m: [object Object]
245-
[33m3[39m: [object Object]
246-
[33m4[39m: [object Object]
247-
[33m5[39m: [object Object]
248-
[33m6[39m: [object Object]
249-
[33m7[39m: [object Object]
250-
[33m8[39m: [object Object]
251-
[33m9[39m: [object Object]
210+
[33m0[39m: 1.0.0
211+
[33m10[39m: 1.0.0
212+
[33m11[39m: 1.0.0
213+
[33m12[39m: 1.0.0
214+
[33m13[39m: 1.0.0
215+
[33m14[39m: 1.0.0
216+
[33m15[39m: 1.0.0
217+
[33m16[39m: 1.0.0
218+
[33m17[39m: 1.0.0
219+
[33m18[39m: 1.0.0
220+
[33m19[39m: 1.0.0
221+
[33m1[39m: 1.0.0
222+
[33m20[39m: 1.0.0
223+
[33m21[39m: 1.0.0
224+
[33m22[39m: 1.0.0
225+
[33m23[39m: 1.0.0
226+
[33m2[39m: 1.0.0
227+
[33m3[39m: 1.0.0
228+
[33m4[39m: 1.0.0
229+
[33m5[39m: 1.0.0
230+
[33m6[39m: 1.0.0
231+
[33m7[39m: 1.0.0
232+
[33m8[39m: 1.0.0
233+
[33m9[39m: 1.0.0
252234
(...and 1 more.)
253235
254236
dist-tags:
255-
[1m[32m1.0.1[39m[22m: [object Object]
237+
[1m[32mlatest[39m[22m: 1.0.0
256238
`
257239

258240
exports[`test/lib/view.js TAP should log package info package with no modified time > must match snapshot 1`] = `
259241
260242
261-
[4m[1m[32mcyan[39m@[32mundefined[39m[22m[24m | [1m[31mProprietary[39m[22m | deps: [32mnone[39m | versions: [33m2[39m
243+
[4m[1m[32mcyan[39m@[32m1.0.0[39m[22m[24m | [1m[31mProprietary[39m[22m | deps: [32mnone[39m | versions: [33m2[39m
262244
263245
dist
264-
.tarball:undefined
265-
.shasum:undefined
246+
.tarball:http://hm.cyan.com/1.0.0.tgz
247+
.shasum:123
248+
.integrity:---
249+
.unpackedSize:1 B
266250
267251
dist-tags:
268252
@@ -273,18 +257,16 @@ published by claudia <[[email protected]>
273257
exports[`test/lib/view.js TAP should log package info package with no repo or homepage > must match snapshot 1`] = `
274258
275259
276-
[4m[1m[32mblue[39m@[32mundefined[39m[22m[24m | [1m[31mProprietary[39m[22m | deps: [32mnone[39m | versions: [33m2[39m
260+
[4m[1m[32mblue[39m@[32m1.0.0[39m[22m[24m | [1m[31mProprietary[39m[22m | deps: [32mnone[39m | versions: [33m2[39m
277261
278262
dist
279-
.tarball:undefined
280-
.shasum:undefined
263+
.tarball:http://hm.blue.com/1.0.0.tgz
264+
.shasum:123
265+
.integrity:---
266+
.unpackedSize:1 B
281267
282268
dist-tags:
283269
284270
285271
published a year ago
286272
`
287-
288-
exports[`test/lib/view.js TAP should log package info package with no versions > must match snapshot 1`] = `
289-
290-
`

0 commit comments

Comments
 (0)