Skip to content

Commit 57d65ee

Browse files
committed
fix: show more information during publish dry-run
Fixes #4317
1 parent 1617bce commit 57d65ee

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

lib/commands/publish.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,20 @@ class Publish extends BaseCommand {
102102
logTar(pkgContents, { unicode })
103103
}
104104

105+
const resolved = npa.resolve(manifest.name, manifest.version)
106+
const registry = npmFetch.pickRegistry(resolved, opts)
107+
const creds = this.npm.config.getCredentialsByURI(registry)
108+
const outputRegistry = replaceInfo(registry)
109+
if (!creds.token && !creds.username) {
110+
throw Object.assign(
111+
new Error(`This command requires you to be logged in to ${outputRegistry}`), {
112+
code: 'ENEEDAUTH',
113+
}
114+
)
115+
}
116+
log.notice('', `Publishing to ${outputRegistry}${dryRun ? ' (dry-run)' : ''}`)
117+
105118
if (!dryRun) {
106-
const resolved = npa.resolve(manifest.name, manifest.version)
107-
const registry = npmFetch.pickRegistry(resolved, opts)
108-
const creds = this.npm.config.getCredentialsByURI(registry)
109-
const outputRegistry = replaceInfo(registry)
110-
if (!creds.token && !creds.username) {
111-
throw Object.assign(
112-
new Error(`This command requires you to be logged in to ${outputRegistry}`), {
113-
code: 'ENEEDAUTH',
114-
}
115-
)
116-
}
117-
log.notice('', `Publishing to ${outputRegistry}`)
118119
await otplease(opts, opts => libpub(manifest, tarballData, opts))
119120
}
120121

test/lib/commands/publish.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ t.test('if loglevel=info and json, should not output package contents', async t
163163

164164
t.test(
165165
/* eslint-disable-next-line max-len */
166-
'if loglevel=silent and dry-run, should not output package contents or publish or validate credentials, should log tarball contents',
166+
'if loglevel=silent and dry-run, should not output package contents or publish, should log tarball contents',
167167
async t => {
168-
t.plan(1)
168+
t.plan(2)
169169

170170
const testDir = t.testdir({
171171
'package.json': JSON.stringify(
@@ -199,8 +199,9 @@ t.test(
199199
throw new Error('should not output in dry run mode')
200200
},
201201
}, t)
202-
npm.config.getCredentialsByURI = () => {
203-
throw new Error('should not call getCredentialsByURI in dry run')
202+
npm.config.getCredentialsByURI = uri => {
203+
t.same(uri, npm.config.get('registry'), 'gets credentials for expected registry')
204+
return { token: 'some.registry.token' }
204205
}
205206

206207
const publish = new Publish(npm)
@@ -213,7 +214,7 @@ t.test(
213214
/* eslint-disable-next-line max-len */
214215
'if loglevel=info and dry-run, should not publish, should log package contents and log tarball contents',
215216
async t => {
216-
t.plan(2)
217+
t.plan(3)
217218

218219
const testDir = t.testdir({
219220
'package.json': JSON.stringify(
@@ -247,9 +248,11 @@ t.test(
247248
t.pass('output fn is called')
248249
},
249250
}, t)
250-
npm.config.getCredentialsByURI = () => {
251-
throw new Error('should not call getCredentialsByURI in dry run')
251+
npm.config.getCredentialsByURI = uri => {
252+
t.same(uri, npm.config.get('registry'), 'gets credentials for expected registry')
253+
return { token: 'some.registry.token' }
252254
}
255+
253256
const publish = new Publish(npm)
254257

255258
await publish.exec([testDir])

0 commit comments

Comments
 (0)