Skip to content

Commit 70cd88d

Browse files
authored
fix(view): sort and truncate dist-tags (#7808)
This sorts dist-tags by publish date so that newer tags show first, giving top priority to the `latest` tag. It also truncates the list in a similar manner to how dependencies are truncated. Needs tests. Before: ![dist-tags list in npm view showing every tag shorted by version](https://github.com/user-attachments/assets/9a8b094f-f466-439c-927b-21dbeb896322) After: ![dist-tags list in npm view showing a truncated list sorted by publish date](https://github.com/user-attachments/assets/8727c387-21ed-4fe3-8f09-dc8b93bfe75f)
1 parent feb54f7 commit 70cd88d

File tree

3 files changed

+105
-4
lines changed

3 files changed

+105
-4
lines changed

lib/commands/view.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,18 @@ class View extends BaseCommand {
266266
const deps = Object.entries(manifest.dependencies || {}).map(([k, dep]) =>
267267
`${chalk.blue(k)}: ${dep}`
268268
)
269+
// Sort dist-tags by publish time, then tag name, keeping latest at the top of the list
270+
const distTags = Object.entries(packu['dist-tags'])
271+
.sort(([aTag, aVer], [bTag, bVer]) => {
272+
const aTime = aTag === 'latest' ? Infinity : Date.parse(packu.time[aVer])
273+
const bTime = bTag === 'latest' ? Infinity : Date.parse(packu.time[bVer])
274+
if (aTime === bTime) {
275+
return aTag > bTag ? -1 : 1
276+
}
277+
return aTime > bTime ? -1 : 1
278+
})
279+
.map(([k, t]) => `${chalk.blue(k)}: ${t}`)
280+
269281
const site = manifest.homepage?.url || manifest.homepage
270282
const bins = Object.keys(manifest.bin || {})
271283
const licenseField = manifest.license || 'Proprietary'
@@ -333,9 +345,11 @@ class View extends BaseCommand {
333345
}
334346

335347
res.push('\ndist-tags:')
336-
res.push(columns(Object.entries(packu['dist-tags']).map(([k, t]) =>
337-
`${chalk.blue(k)}: ${t}`
338-
)))
348+
const maxTags = 12
349+
res.push(columns(distTags.slice(0, maxTags), { padding: 1, sort: false }))
350+
if (distTags.length > maxTags) {
351+
res.push(chalk.dim(`(...and ${distTags.length - maxTags} more.)`))
352+
}
339353

340354
const publisher = manifest._npmUser && unparsePerson({
341355
name: chalk.blue(manifest._npmUser.name),

tap-snapshots/test/lib/commands/view.js.test.cjs

+73-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ dist
102102
103103
dist-tags:
104104
latest: 1.0.0
105+
z: 1.0.0
106+
y: 1.0.0
107+
v1: 1.0.0
108+
prev: 1.0.0
109+
d: 1.0.0
110+
c: 1.0.0
111+
b: 1.0.0
112+
a: 1.0.0
113+
x: 1.0.1
114+
next: 1.0.1
115+
h: 1.0.1
116+
(...and 3 more.)
105117
106118
published {TIME} ago
107119
`
@@ -116,6 +128,18 @@ dist
116128
117129
dist-tags:
118130
latest: 1.0.0
131+
z: 1.0.0
132+
y: 1.0.0
133+
v1: 1.0.0
134+
prev: 1.0.0
135+
d: 1.0.0
136+
c: 1.0.0
137+
b: 1.0.0
138+
a: 1.0.0
139+
x: 1.0.1
140+
next: 1.0.1
141+
h: 1.0.1
142+
(...and 3 more.)
119143
120144
published {TIME} ago
121145
`
@@ -130,6 +154,18 @@ dist
130154
131155
dist-tags:
132156
latest: 1.0.0
157+
z: 1.0.0
158+
y: 1.0.0
159+
v1: 1.0.0
160+
prev: 1.0.0
161+
d: 1.0.0
162+
c: 1.0.0
163+
b: 1.0.0
164+
a: 1.0.0
165+
x: 1.0.1
166+
next: 1.0.1
167+
h: 1.0.1
168+
(...and 3 more.)
133169
134170
published {TIME} ago
135171
`
@@ -269,6 +305,18 @@ dist
269305
270306
dist-tags:
271307
latest: 1.0.0
308+
z: 1.0.0
309+
y: 1.0.0
310+
v1: 1.0.0
311+
prev: 1.0.0
312+
d: 1.0.0
313+
c: 1.0.0
314+
b: 1.0.0
315+
a: 1.0.0
316+
x: 1.0.1
317+
next: 1.0.1
318+
h: 1.0.1
319+
(...and 3 more.)
272320
273321
published {TIME} ago
274322
`
@@ -283,6 +331,18 @@ dist
283331
284332
dist-tags:
285333
latest: 1.0.0
334+
z: 1.0.0
335+
y: 1.0.0
336+
v1: 1.0.0
337+
prev: 1.0.0
338+
d: 1.0.0
339+
c: 1.0.0
340+
b: 1.0.0
341+
a: 1.0.0
342+
x: 1.0.1
343+
next: 1.0.1
344+
h: 1.0.1
345+
(...and 3 more.)
286346
287347
published {TIME} ago
288348
@@ -296,8 +356,20 @@ dist
296356
297357
dist-tags:
298358
latest: 1.0.0
359+
z: 1.0.0
360+
y: 1.0.0
361+
v1: 1.0.0
362+
prev: 1.0.0
363+
d: 1.0.0
364+
c: 1.0.0
365+
b: 1.0.0
366+
a: 1.0.0
367+
x: 1.0.1
368+
next: 1.0.1
369+
h: 1.0.1
370+
(...and 3 more.)
299371
300-
published [36mover a year from now[39m
372+
published {TIME} ago[39m
301373
`
302374

303375
exports[`test/lib/commands/view.js TAP package with single version full json > must match snapshot 1`] = `

test/lib/commands/view.js

+15
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,25 @@ const packument = (nv, opts) => {
3636
_id: 'blue',
3737
name: 'blue',
3838
'dist-tags': {
39+
v1: '1.0.0',
40+
next: '1.0.1',
41+
prev: '1.0.0',
3942
latest: '1.0.0',
43+
a: '1.0.0',
44+
c: '1.0.0',
45+
b: '1.0.0',
46+
d: '1.0.0',
47+
f: '1.0.1',
48+
g: '1.0.1',
49+
h: '1.0.1',
50+
e: '1.0.1',
51+
z: '1.0.0',
52+
x: '1.0.1',
53+
y: '1.0.0',
4054
},
4155
time: {
4256
'1.0.0': yesterday,
57+
'1.0.1': '2012-12-20T00:00:00.000Z',
4358
},
4459
versions: {
4560
'1.0.0': {

0 commit comments

Comments
 (0)