Skip to content

Commit

Permalink
fix(view): sort and truncate dist-tags
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wraithgar committed Oct 3, 2024
1 parent 95e2cb1 commit c8e8926
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/commands/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,18 @@ class View extends BaseCommand {
const deps = Object.entries(manifest.dependencies || {}).map(([k, dep]) =>
`${chalk.blue(k)}: ${dep}`
)
// Sort dist-tags by publish time, then tag name, keeping latest at the top of the list
const distTags = Object.entries(packu['dist-tags'])
.sort(([aTag, aVer], [bTag, bVer]) => {
const aTime = aTag === 'latest' ? Infinity : Date.parse(packu.time[aVer])
const bTime = bTag === 'latest' ? Infinity : Date.parse(packu.time[bVer])
if (aTime === bTime) {
return aTag > bTag ? -1 : 1
}
return aTime > bTime ? -1 : 1
})
.map(([k, t]) => `${chalk.blue(k)}: ${t}`)

const site = manifest.homepage?.url || manifest.homepage
const bins = Object.keys(manifest.bin || {})
const licenseField = manifest.license || 'Proprietary'
Expand Down Expand Up @@ -333,9 +345,11 @@ class View extends BaseCommand {
}

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

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

0 comments on commit c8e8926

Please sign in to comment.