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: output configured registry during publish #4143

Merged
merged 1 commit into from
Dec 8, 2021
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
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