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: publish --dry-run require login & publishConfig loaded after registry check #2445

Merged
merged 3 commits into from
Jan 28, 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
33 changes: 17 additions & 16 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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({
Expand Down
43 changes: 41 additions & 2 deletions test/lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: {
Expand All @@ -369,3 +367,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')
})
})