Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(view): sort and truncate dist-tags #7808

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 over a year from now
published {TIME} ago
`

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
Loading