From b2e2edf8aee57347c96a61209c7a10139a0cc85a Mon Sep 17 00:00:00 2001 From: "fuhao.xu" Date: Tue, 29 Dec 2020 18:56:34 +0800 Subject: [PATCH] fix: npm publish --dry-run should not check login status PR-URL: https://github.com/npm/cli/pull/2422 Credit: @buyan302 Close: #2422 Reviewed-by: @isaacs --- lib/publish.js | 12 +++++++----- test/lib/publish.js | 6 +++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/publish.js b/lib/publish.js index 0a4123303e6be..8ef7eff4c8a64 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -43,11 +43,13 @@ const publish = async args => { }) } - const creds = npm.config.getCredentialsByURI(registry) - if (!creds.token && !creds.username) { - throw Object.assign(new Error('This command requires you to be logged in.'), { - code: 'ENEEDAUTH', - }) + if (!opts.dryRun) { + const creds = npm.config.getCredentialsByURI(registry) + if (!creds.token && !creds.username) { + throw Object.assign(new Error('This command requires you to be logged in.'), { + code: 'ENEEDAUTH', + }) + } } if (semver.validRange(defaultTag)) diff --git a/test/lib/publish.js b/test/lib/publish.js index c1f353f6fdbc5..d4e41605df8c6 100644 --- a/test/lib/publish.js +++ b/test/lib/publish.js @@ -193,7 +193,11 @@ t.test('should log tarball contents', (t) => { dryRun: true, registry: 'https://registry.npmjs.org/', }, - config, + config: { + ...config, + getCredentialsByURI: () => { + throw new Error('should not call getCredentialsByURI!') + }}, }, '../../lib/utils/tar.js': { getContents: () => ({