Skip to content

Commit e2f4455

Browse files
committed
feat: uses packument to determine semver latest
1 parent 26d4c2b commit e2f4455

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

lib/commands/publish.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class Publish extends BaseCommand {
132132

133133
const resolved = npa.resolve(manifest.name, manifest.version)
134134

135-
const latestDistTag = await this.#latestDistTag(resolved)
135+
const latestDistTag = await this.#latestPublishedVersion(resolved)
136136
const latestTagIsGreater = latestDistTag ? semver.gte(latestDistTag, manifest.version) : false
137137

138138
if (latestTagIsGreater && isDefaultTag) {
@@ -204,6 +204,26 @@ class Publish extends BaseCommand {
204204
}
205205
}
206206

207+
async #latestPublishedVersion (spec) {
208+
try {
209+
const uri = '/' + spec.escapedName
210+
const packument = await npmFetch.json(uri, {
211+
...this.npm.flatOptions,
212+
spec,
213+
})
214+
if (typeof packument?.versions === 'undefined') {
215+
return null
216+
}
217+
const ordered = Object.keys(packument?.versions)
218+
.map(v => new semver.SemVer(v))
219+
.filter(v => !v.prerelease.length)
220+
.sort((a, b) => b.compare(a))
221+
return ordered.length >= 1 ? ordered[0].version : null
222+
} catch (e) {
223+
return null
224+
}
225+
}
226+
207227
async #latestDistTag (spec) {
208228
try {
209229
const tags = await DistTag.fetchTags(spec, this.npm.flatOptions)

test/lib/commands/publish.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function loadMockNpm (test, args) {
3030
...args,
3131
mocks: {
3232
...mockNpmRegistryFetch({
33-
[`/-/package/${pkg}/dist-tags`]: () => {
33+
[`/${pkg}`]: () => {
3434
throw new Error('not found')
3535
},
3636
}).mocks,
@@ -1088,7 +1088,6 @@ t.test('does not abort when prerelease and authored tag latest', async t => {
10881088
})
10891089

10901090
t.test('PREVENTS publish when latest dist-tag is HIGHER than publishing version', async t => {
1091-
const latest = '100.0.0'
10921091
const version = '50.0.0'
10931092

10941093
const { npm } = await loadMockNpm(t, {
@@ -1111,7 +1110,7 @@ t.test('PREVENTS publish when latest dist-tag is HIGHER than publishing version'
11111110
},
11121111
mocks: {
11131112
...mockNpmRegistryFetch({
1114-
[`/-/package/${pkg}/dist-tags`]: { latest },
1113+
[`/${pkg}`]: { versions: { '50.0.0': {}, '99.0.0': {}, '100.0.0': {}, '101.0.0-pre': {} } },
11151114
}).mocks,
11161115
},
11171116
})
@@ -1120,9 +1119,8 @@ t.test('PREVENTS publish when latest dist-tag is HIGHER than publishing version'
11201119
}, new Error('Cannot publish a lower version without an explicit dist tag.'))
11211120
})
11221121

1123-
t.test('ALLOWS publish when latest dist-tag is LOWER than publishing version', async t => {
1122+
t.test('ALLOWS publish when latest versions are LOWER than publishing version', async t => {
11241123
const version = '100.0.0'
1125-
const latest = '50.0.0'
11261124

11271125
const { npm } = await loadMockNpm(t, {
11281126
config: {
@@ -1138,7 +1136,7 @@ t.test('ALLOWS publish when latest dist-tag is LOWER than publishing version', a
11381136
},
11391137
mocks: {
11401138
...mockNpmRegistryFetch({
1141-
[`/-/package/${pkg}/dist-tags`]: { latest },
1139+
[`/${pkg}`]: { versions: { '50.0.0': {}, '99.0.0': {}, '101.0.0-pre': {} } },
11421140
}).mocks,
11431141
},
11441142
})
@@ -1155,7 +1153,7 @@ t.test('ALLOWS publish when latest dist-tag is LOWER than publishing version', a
11551153
await npm.exec('publish', [])
11561154
})
11571155

1158-
t.test('ALLOWS publish when latest dist-tag is missing from response', async t => {
1156+
t.test('ALLOWS publish when not published yet', async t => {
11591157
const version = '100.0.0'
11601158

11611159
const { npm } = await loadMockNpm(t, {
@@ -1172,7 +1170,7 @@ t.test('ALLOWS publish when latest dist-tag is missing from response', async t =
11721170
},
11731171
mocks: {
11741172
...mockNpmRegistryFetch({
1175-
[`/-/package/${pkg}/dist-tags`]: { },
1173+
[`/${pkg}`]: { },
11761174
}).mocks,
11771175
},
11781176
})

0 commit comments

Comments
 (0)