Skip to content

Commit

Permalink
fix: output configured registry during publish
Browse files Browse the repository at this point in the history
Closes: npm/statusboard#416

PR-URL: #4143
Credit: @lukekarrys
Close: #4143
Reviewed-by: @wraithgar
  • Loading branch information
lukekarrys authored and nlf committed Dec 8, 2021
1 parent e605b12 commit 166d9e1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
10 changes: 7 additions & 3 deletions lib/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,15 @@ class Publish extends BaseCommand {
const resolved = npa.resolve(manifest.name, manifest.version)
const registry = npmFetch.pickRegistry(resolved, opts)
const creds = this.npm.config.getCredentialsByURI(registry)
const outputRegistry = replaceInfo(registry)
if (!creds.token && !creds.username) {
throw Object.assign(new Error('This command requires you to be logged in.'), {
code: 'ENEEDAUTH',
})
throw Object.assign(
new Error(`This command requires you to be logged in to ${outputRegistry}`), {
code: 'ENEEDAUTH',
}
)
}
log.notice('', `Publishing to ${outputRegistry}`)
await otplease(opts, opts => libpub(manifest, tarballData, opts))
}

Expand Down
18 changes: 12 additions & 6 deletions test/lib/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,10 @@ t.test('can publish a tarball', async t => {

t.test('should check auth for default registry', async t => {
t.plan(2)
const Publish = t.mock('../../../lib/commands/publish.js')
const npm = mockNpm()
const registry = npm.config.get('registry')
const errorMessage = `This command requires you to be logged in to ${registry}`
const Publish = t.mock('../../../lib/commands/publish.js')
npm.config.getCredentialsByURI = uri => {
t.same(uri, npm.config.get('registry'), 'gets credentials for expected registry')
return {}
Expand All @@ -351,14 +353,15 @@ t.test('should check auth for default registry', async t => {

await t.rejects(
publish.exec([]),
{ message: 'This command requires you to be logged in.', code: 'ENEEDAUTH' },
{ message: errorMessage, code: 'ENEEDAUTH' },
'throws when not logged in'
)
})

t.test('should check auth for configured registry', async t => {
t.plan(2)
const registry = 'https://some.registry'
const errorMessage = 'This command requires you to be logged in to https://some.registry'
const Publish = t.mock('../../../lib/commands/publish.js')
const npm = mockNpm({
flatOptions: { registry },
Expand All @@ -371,14 +374,15 @@ t.test('should check auth for configured registry', async t => {

await t.rejects(
publish.exec([]),
{ message: 'This command requires you to be logged in.', code: 'ENEEDAUTH' },
{ message: errorMessage, code: 'ENEEDAUTH' },
'throws when not logged in'
)
})

t.test('should check auth for scope specific registry', async t => {
t.plan(2)
const registry = 'https://some.registry'
const errorMessage = 'This command requires you to be logged in to https://some.registry'
const testDir = t.testdir({
'package.json': JSON.stringify(
{
Expand All @@ -402,7 +406,7 @@ t.test('should check auth for scope specific registry', async t => {

await t.rejects(
publish.exec([testDir]),
{ message: 'This command requires you to be logged in.', code: 'ENEEDAUTH' },
{ message: errorMessage, code: 'ENEEDAUTH' },
'throws when not logged in'
)
})
Expand Down Expand Up @@ -735,7 +739,7 @@ t.test('private workspaces', async t => {
})

t.test('unexpected error', async t => {
t.plan(1)
t.plan(2)

const Publish = t.mock('../../../lib/commands/publish.js', {
...mocks,
Expand All @@ -749,7 +753,9 @@ t.test('private workspaces', async t => {
},
},
'proc-log': {
notice () {},
notice (__, msg) {
t.match(msg, 'Publishing to https://registry.npmjs.org/')
},
verbose () {},
},
})
Expand Down

0 comments on commit 166d9e1

Please sign in to comment.