Skip to content

Commit

Permalink
fix: prioritize CLI flags over publishConfig settings (#7321)
Browse files Browse the repository at this point in the history
This PR addresses an issue where CLI flags were not taking precedence
over publishConfig settings. To ensure CLI flags have higher priority,
properties from the publishConfig object that also exist in CLI flags
are filtered out.


  Related to #6400
  • Loading branch information
roni-berlin committed Apr 9, 2024
1 parent 70497cb commit c929ed1
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,12 @@ class Publish extends BaseCommand {
})
}
if (manifest.publishConfig) {
flatten(manifest.publishConfig, opts)
const cliFlags = this.npm.config.data.get('cli').raw
// Filter out properties set in CLI flags to prioritize them over
// corresponding `publishConfig` settings
const filteredPublishConfig = Object.fromEntries(
Object.entries(manifest.publishConfig).filter(([key]) => !(key in cliFlags)))
flatten(filteredPublishConfig, opts)
}
return manifest
}
Expand Down
7 changes: 6 additions & 1 deletion lib/commands/unpublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ class Unpublish extends BaseCommand {
// If localPrefix has a package.json with a name that matches the package
// being unpublished, load up the publishConfig
if (manifest?.name === spec.name && manifest.publishConfig) {
flatten(manifest.publishConfig, opts)
const cliFlags = this.npm.config.data.get('cli').raw
// Filter out properties set in CLI flags to prioritize them over
// corresponding `publishConfig` settings
const filteredPublishConfig = Object.fromEntries(
Object.entries(manifest.publishConfig).filter(([key]) => !(key in cliFlags)))
flatten(filteredPublishConfig, opts)
}

const versions = await Unpublish.getKeysOfVersions(spec.name, opts)
Expand Down
4 changes: 4 additions & 0 deletions tap-snapshots/test/lib/commands/publish.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,10 @@ exports[`test/lib/commands/publish.js TAP restricted access > new package versio
+ @npm/test-package@1.0.0
`

exports[`test/lib/commands/publish.js TAP prioritize CLI flags over publishConfig > new package version 1`] = `
+ test-package@1.0.0
`

exports[`test/lib/commands/publish.js TAP scoped _auth config scoped registry > new package version 1`] = `
+ @npm/test-package@1.0.0
`
Expand Down
52 changes: 52 additions & 0 deletions test/lib/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,58 @@ t.test('re-loads publishConfig.registry if added during script process', async t
t.matchSnapshot(joinedOutput(), 'new package version')
})

t.test('prioritize CLI flags over publishConfig', async t => {
const publishConfig = { registry: 'http://publishconfig' }
const { joinedOutput, npm } = await loadMockNpm(t, {
config: {
[`${alternateRegistry.slice(6)}/:_authToken`]: 'test-other-token',
},
prefixDir: {
'package.json': JSON.stringify({
...pkgJson,
scripts: {
prepare: 'cp new.json package.json',
},
}, null, 2),
'new.json': JSON.stringify({
...pkgJson,
publishConfig,
}),
},
argv: ['--registry', alternateRegistry],
})
const registry = new MockRegistry({
tap: t,
registry: alternateRegistry,
authorization: 'test-other-token',
})
registry.nock.put(`/${pkg}`, body => {
return t.match(body, {
_id: pkg,
name: pkg,
'dist-tags': { latest: '1.0.0' },
access: null,
versions: {
'1.0.0': {
name: pkg,
version: '1.0.0',
_id: `${pkg}@1.0.0`,
dist: {
shasum: /\.*/,
tarball: `http:${alternateRegistry.slice(6)}/test-package/-/test-package-1.0.0.tgz`,
},
publishConfig,
},
},
_attachments: {
[`${pkg}-1.0.0.tgz`]: {},
},
})
}).reply(200, {})
await npm.exec('publish', [])
t.matchSnapshot(joinedOutput(), 'new package version')
})

t.test('json', async t => {
const { joinedOutput, npm, logs } = await loadMockNpm(t, {
config: {
Expand Down
30 changes: 30 additions & 0 deletions test/lib/commands/unpublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,36 @@ t.test('publishConfig no spec', async t => {
t.equal(joinedOutput(), '- test-package')
})

t.test('prioritize CLI flags over publishConfig no spec', async t => {
const alternateRegistry = 'https://other.registry.npmjs.org'
const publishConfig = { registry: 'http://publishconfig' }
const { joinedOutput, npm } = await loadMockNpm(t, {
config: {
force: true,
'//other.registry.npmjs.org/:_authToken': 'test-other-token',
},
prefixDir: {
'package.json': JSON.stringify({
name: pkg,
version: '1.0.0',
publishConfig,
}, null, 2),
},
argv: ['--registry', alternateRegistry],
})

const registry = new MockRegistry({
tap: t,
registry: alternateRegistry,
authorization: 'test-other-token',
})
const manifest = registry.manifest({ name: pkg })
await registry.package({ manifest, query: { write: true }, times: 2 })
registry.unpublish({ manifest })
await npm.exec('unpublish', [])
t.equal(joinedOutput(), '- test-package')
})

t.test('publishConfig with spec', async t => {
const alternateRegistry = 'https://other.registry.npmjs.org'
const { joinedOutput, npm } = await loadMockNpm(t, {
Expand Down

0 comments on commit c929ed1

Please sign in to comment.