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 Sep 30, 2024
1 parent 6ca609e commit 686adf1
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/commands/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,17 @@ class View extends BaseCommand {
const deps = Object.entries(manifest.dependencies || {}).map(([k, dep]) =>
`${chalk.blue(k)}: ${dep}`
)
const distTags = Object.entries(packu['dist-tags'])
.sort(([aTag, aVer], [bTag, bVer]) => {
let aTime = aTag === 'latest' ? Infinity : Date.parse(packu.time[aVer])

Check failure on line 271 in lib/commands/view.js

View workflow job for this annotation

GitHub Actions / Lint

'aTime' is never reassigned. Use 'const' instead
let bTime = bTag === 'latest' ? Infinity : Date.parse(packu.time[bVer])

Check failure on line 272 in lib/commands/view.js

View workflow job for this annotation

GitHub Actions / Lint

'bTime' is never reassigned. Use 'const' instead
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 +344,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 686adf1

Please sign in to comment.