Skip to content

Commit

Permalink
fix(view): sort and truncate dist-tags (#7808)
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.

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)
  • Loading branch information
wraithgar authored Oct 11, 2024
1 parent feb54f7 commit 70cd88d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 4 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
74 changes: 73 additions & 1 deletion tap-snapshots/test/lib/commands/view.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ dist
dist-tags:
latest: 1.0.0
z: 1.0.0
y: 1.0.0
v1: 1.0.0
prev: 1.0.0
d: 1.0.0
c: 1.0.0
b: 1.0.0
a: 1.0.0
x: 1.0.1
next: 1.0.1
h: 1.0.1
(...and 3 more.)
published {TIME} ago
`
Expand All @@ -116,6 +128,18 @@ dist
dist-tags:
latest: 1.0.0
z: 1.0.0
y: 1.0.0
v1: 1.0.0
prev: 1.0.0
d: 1.0.0
c: 1.0.0
b: 1.0.0
a: 1.0.0
x: 1.0.1
next: 1.0.1
h: 1.0.1
(...and 3 more.)
published {TIME} ago
`
Expand All @@ -130,6 +154,18 @@ dist
dist-tags:
latest: 1.0.0
z: 1.0.0
y: 1.0.0
v1: 1.0.0
prev: 1.0.0
d: 1.0.0
c: 1.0.0
b: 1.0.0
a: 1.0.0
x: 1.0.1
next: 1.0.1
h: 1.0.1
(...and 3 more.)
published {TIME} ago
`
Expand Down Expand Up @@ -269,6 +305,18 @@ dist
dist-tags:
latest: 1.0.0
z: 1.0.0
y: 1.0.0
v1: 1.0.0
prev: 1.0.0
d: 1.0.0
c: 1.0.0
b: 1.0.0
a: 1.0.0
x: 1.0.1
next: 1.0.1
h: 1.0.1
(...and 3 more.)
published {TIME} ago
`
Expand All @@ -283,6 +331,18 @@ dist
dist-tags:
latest: 1.0.0
z: 1.0.0
y: 1.0.0
v1: 1.0.0
prev: 1.0.0
d: 1.0.0
c: 1.0.0
b: 1.0.0
a: 1.0.0
x: 1.0.1
next: 1.0.1
h: 1.0.1
(...and 3 more.)
published {TIME} ago
Expand All @@ -296,8 +356,20 @@ dist
dist-tags:
latest: 1.0.0
z: 1.0.0
y: 1.0.0
v1: 1.0.0
prev: 1.0.0
d: 1.0.0
c: 1.0.0
b: 1.0.0
a: 1.0.0
x: 1.0.1
next: 1.0.1
h: 1.0.1
(...and 3 more.)
published [36mover a year from now[39m
published {TIME} ago[39m
`

exports[`test/lib/commands/view.js TAP package with single version full json > must match snapshot 1`] = `
Expand Down
15 changes: 15 additions & 0 deletions test/lib/commands/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,25 @@ const packument = (nv, opts) => {
_id: 'blue',
name: 'blue',
'dist-tags': {
v1: '1.0.0',
next: '1.0.1',
prev: '1.0.0',
latest: '1.0.0',
a: '1.0.0',
c: '1.0.0',
b: '1.0.0',
d: '1.0.0',
f: '1.0.1',
g: '1.0.1',
h: '1.0.1',
e: '1.0.1',
z: '1.0.0',
x: '1.0.1',
y: '1.0.0',
},
time: {
'1.0.0': yesterday,
'1.0.1': '2012-12-20T00:00:00.000Z',
},
versions: {
'1.0.0': {
Expand Down

0 comments on commit 70cd88d

Please sign in to comment.