From f1bea2f09e283f994bd8b69a356cbe11559c8c1f Mon Sep 17 00:00:00 2001 From: dr-js Date: Fri, 8 Jan 2021 09:43:30 +0800 Subject: [PATCH 1/3] test: publish: test add read registry only from publishConfig --- test/lib/publish.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/lib/publish.js b/test/lib/publish.js index d4e41605df8c6..675824848e20b 100644 --- a/test/lib/publish.js +++ b/test/lib/publish.js @@ -369,3 +369,44 @@ t.test('throw if not logged in', async t => { }, 'throws when not logged in') }) }) + +t.test('read registry only from publishConfig', t => { + t.plan(3) + + const publishConfig = { registry: 'https://some.registry' } + const testDir = t.testdir({ + 'package.json': JSON.stringify({ + name: 'my-cool-pkg', + version: '1.0.0', + publishConfig, + }, null, 2), + }) + + const publish = requireInject('../../lib/publish.js', { + '../../lib/npm.js': { + flatOptions: { + json: false, + }, + config, + }, + '../../lib/utils/tar.js': { + getContents: () => ({ + id: 'someid', + }), + logTar: () => {}, + }, + '../../lib/utils/output.js': () => {}, + libnpmpublish: { + publish: (manifest, tarData, opts) => { + t.match(manifest, { name: 'my-cool-pkg', version: '1.0.0' }, 'gets manifest') + t.same(opts.registry, publishConfig.registry, 'publishConfig is passed through') + }, + }, + }) + + return publish([testDir], (er) => { + if (er) + throw er + t.pass('got to callback') + }) +}) From 938a77d6e7fb79f8ceb72c51bf06e27127fb9f06 Mon Sep 17 00:00:00 2001 From: dr-js Date: Fri, 8 Jan 2021 09:46:57 +0800 Subject: [PATCH 2/3] fix: publish: delay registry & login check till publishConfig merge --- lib/publish.js | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/publish.js b/lib/publish.js index 8ef7eff4c8a64..49b2088070e7a 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -35,22 +35,7 @@ const publish = async args => { log.verbose('publish', args) const opts = { ...npm.flatOptions } - const { json, defaultTag, registry } = opts - - if (!registry) { - throw Object.assign(new Error('No registry specified.'), { - code: 'ENOREGISTRY', - }) - } - - 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', - }) - } - } + const { json, defaultTag } = opts if (semver.validRange(defaultTag)) throw new Error('Tag name must not be a valid SemVer range: ' + defaultTag.trim()) @@ -91,6 +76,22 @@ const publish_ = async (arg, opts) => { if (manifest.publishConfig) Object.assign(opts, publishConfigToOpts(manifest.publishConfig)) + const { registry } = opts + if (!registry) { + throw Object.assign(new Error('No registry specified.'), { + code: 'ENOREGISTRY', + }) + } + + if (!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', + }) + } + } + // only run scripts for directory type publishes if (spec.type === 'directory') { await runScript({ From d2f8af2da64d510d3d363aec10531bebf840d84e Mon Sep 17 00:00:00 2001 From: dr-js Date: Fri, 8 Jan 2021 09:54:14 +0800 Subject: [PATCH 3/3] fix: publish: test defaultTag caused early error PR-URL: https://github.com/npm/cli/pull/2445 Credit: @dr-js Close: #2445 Reviewed-by: @ruyadorno --- test/lib/publish.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/lib/publish.js b/test/lib/publish.js index 675824848e20b..f0ce0b966533c 100644 --- a/test/lib/publish.js +++ b/test/lib/publish.js @@ -329,7 +329,6 @@ t.test('throw if no registry', async t => { '../../lib/npm.js': { flatOptions: { json: false, - defaultTag: '0.0.13', registry: null, }, config, @@ -350,7 +349,6 @@ t.test('throw if not logged in', async t => { '../../lib/npm.js': { flatOptions: { json: false, - defaultTag: '0.0.13', registry: 'https://registry.npmjs.org/', }, config: {