Skip to content

Commit e30b8df

Browse files
committed
fix: moved throw after auth check
1 parent 03ec495 commit e30b8df

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

lib/commands/publish.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,11 @@ class Publish extends BaseCommand {
130130
}
131131

132132
const resolved = npa.resolve(manifest.name, manifest.version)
133-
const registry = npmFetch.pickRegistry(resolved, opts)
134-
135-
const latestVersion = await this.#latestPublishedVersion(resolved, registry)
136-
const latestSemverIsGreater = !!latestVersion && semver.gte(latestVersion, manifest.version)
137-
138-
if (latestSemverIsGreater && isDefaultTag) {
139-
throw new Error('Cannot publish a lower version without an explicit tag.')
140-
}
141133

142134
// make sure tag is valid, this will throw if invalid
143135
npa(`${manifest.name}@${defaultTag}`)
144136

137+
const registry = npmFetch.pickRegistry(resolved, opts)
145138
const creds = this.npm.config.getCredentialsByURI(registry)
146139
const noCreds = !(creds.token || creds.username || creds.certfile && creds.keyfile)
147140
const outputRegistry = replaceInfo(registry)
@@ -164,6 +157,13 @@ class Publish extends BaseCommand {
164157
}
165158
}
166159

160+
const latestVersion = await this.#latestPublishedVersion(resolved, registry)
161+
const latestSemverIsGreater = !!latestVersion && semver.gte(latestVersion, manifest.version)
162+
163+
if (latestSemverIsGreater && isDefaultTag) {
164+
throw new Error('Cannot publish a lower version without an explicit tag.')
165+
}
166+
167167
const access = opts.access === null ? 'default' : opts.access
168168
let msg = `Publishing to ${outputRegistry} with tag ${defaultTag} and ${access} access`
169169
if (dryRun) {

test/lib/commands/publish.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,14 @@ t.test('throws when invalid tag is semver', async t => {
227227
})
228228

229229
t.test('throws when invalid tag when not url encodable', async t => {
230-
const { npm, registry } = await loadNpmWithRegistry(t, {
230+
const { npm } = await loadNpmWithRegistry(t, {
231231
config: {
232232
tag: '@test',
233233
},
234234
prefixDir: {
235235
'package.json': JSON.stringify(pkgJson, null, 2),
236236
},
237237
})
238-
registry.publish(pkg, { noPut: true })
239-
240238
await t.rejects(
241239
npm.exec('publish', []),
242240
{
@@ -272,12 +270,11 @@ t.test('tarball', async t => {
272270
})
273271

274272
t.test('no auth default registry', async t => {
275-
const { npm, registry } = await loadNpmWithRegistry(t, {
273+
const { npm } = await loadNpmWithRegistry(t, {
276274
prefixDir: {
277275
'package.json': JSON.stringify(pkgJson, null, 2),
278276
},
279277
})
280-
registry.publish(pkg, { noPut: true })
281278
await t.rejects(
282279
npm.exec('publish', []),
283280
{
@@ -303,7 +300,7 @@ t.test('no auth dry-run', async t => {
303300
})
304301

305302
t.test('no auth for configured registry', async t => {
306-
const { npm, registry } = await loadNpmWithRegistry(t, {
303+
const { npm } = await loadNpmWithRegistry(t, {
307304
config: {
308305
registry: alternateRegistry,
309306
...auth,
@@ -312,7 +309,6 @@ t.test('no auth for configured registry', async t => {
312309
'package.json': JSON.stringify(pkgJson, null, 2),
313310
},
314311
})
315-
registry.publish(pkg, { noPut: true })
316312
await t.rejects(
317313
npm.exec('publish', []),
318314
{
@@ -323,7 +319,7 @@ t.test('no auth for configured registry', async t => {
323319
})
324320

325321
t.test('no auth for scope configured registry', async t => {
326-
const { npm, registry } = await loadNpmWithRegistry(t, {
322+
const { npm } = await loadNpmWithRegistry(t, {
327323
config: {
328324
scope: '@npm',
329325
registry: alternateRegistry,
@@ -336,7 +332,6 @@ t.test('no auth for scope configured registry', async t => {
336332
}, null, 2),
337333
},
338334
})
339-
registry.publish('@npm/test-package', { noPut: true })
340335
await t.rejects(
341336
npm.exec('publish', []),
342337
{
@@ -435,7 +430,6 @@ t.test('workspaces', t => {
435430
prefixDir: dir,
436431
authorization: token,
437432
})
438-
registry.publish('workspace-p', { noPut: true })
439433
;['workspace-a', 'workspace-b', 'workspace-n'].forEach(name => {
440434
registry.publish(name)
441435
})
@@ -455,7 +449,6 @@ t.test('workspaces', t => {
455449
prefixDir: dir,
456450
authorization: token,
457451
})
458-
registry.publish('workspace-p', { noPut: true })
459452
;['workspace-a', 'workspace-b', 'workspace-n'].forEach(name => {
460453
registry.publish(name)
461454
})
@@ -526,7 +519,6 @@ t.test('workspaces', t => {
526519
prefixDir: testDir,
527520
authorization: token,
528521
})
529-
registry.publish('@scoped/workspace-p', { noPut: true })
530522
registry.publish('workspace-a')
531523
await npm.exec('publish', [])
532524
t.matchSnapshot(joinedOutput(), 'one marked private')
@@ -558,7 +550,6 @@ t.test('workspaces', t => {
558550
prefixDir: dir,
559551
authorization: token,
560552
})
561-
registry.publish('workspace-p', { noPut: true })
562553
;['workspace-a', 'workspace-b', 'workspace-n'].forEach(name => {
563554
registry.publish(name)
564555
})
@@ -684,7 +675,7 @@ t.test('bare _auth and registry config', async t => {
684675
})
685676

686677
t.test('bare _auth config scoped registry', async t => {
687-
const { npm, registry } = await loadNpmWithRegistry(t, {
678+
const { npm } = await loadNpmWithRegistry(t, {
688679
config: {
689680
scope: '@npm',
690681
registry: alternateRegistry,
@@ -697,7 +688,6 @@ t.test('bare _auth config scoped registry', async t => {
697688
}, null, 2),
698689
},
699690
})
700-
registry.publish('@npm/test-package', { noPut: true })
701691
await t.rejects(
702692
npm.exec('publish', []),
703693
{ message: `This command requires you to be logged in to ${alternateRegistry}` }
@@ -895,7 +885,7 @@ t.test('latest dist tag', (t) => {
895885
registry.publish(pkg, { noPut: true, packuments })
896886
await t.rejects(async () => {
897887
await npm.exec('publish', [])
898-
}, new Error('Cannot publish a lower version without an explicit dist tag.'))
888+
}, new Error('Cannot publish a lower version without an explicit tag.'))
899889
})
900890

901891
t.test('ALLOWS publish when latest is HIGHER than publishing version and flag', async t => {

0 commit comments

Comments
 (0)