Skip to content

Commit c8e8926

Browse files
committed
fix(view): sort and truncate dist-tags
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.
1 parent 95e2cb1 commit c8e8926

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

lib/commands/view.js

Lines changed: 17 additions & 3 deletions
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),

0 commit comments

Comments
 (0)