Skip to content

Commit c929ed1

Browse files
authored
fix: prioritize CLI flags over publishConfig settings (#7321)
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
1 parent 70497cb commit c929ed1

File tree

5 files changed

+98
-2
lines changed

5 files changed

+98
-2
lines changed

lib/commands/publish.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,12 @@ class Publish extends BaseCommand {
220220
})
221221
}
222222
if (manifest.publishConfig) {
223-
flatten(manifest.publishConfig, opts)
223+
const cliFlags = this.npm.config.data.get('cli').raw
224+
// Filter out properties set in CLI flags to prioritize them over
225+
// corresponding `publishConfig` settings
226+
const filteredPublishConfig = Object.fromEntries(
227+
Object.entries(manifest.publishConfig).filter(([key]) => !(key in cliFlags)))
228+
flatten(filteredPublishConfig, opts)
224229
}
225230
return manifest
226231
}

lib/commands/unpublish.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,12 @@ class Unpublish extends BaseCommand {
141141
// If localPrefix has a package.json with a name that matches the package
142142
// being unpublished, load up the publishConfig
143143
if (manifest?.name === spec.name && manifest.publishConfig) {
144-
flatten(manifest.publishConfig, opts)
144+
const cliFlags = this.npm.config.data.get('cli').raw
145+
// Filter out properties set in CLI flags to prioritize them over
146+
// corresponding `publishConfig` settings
147+
const filteredPublishConfig = Object.fromEntries(
148+
Object.entries(manifest.publishConfig).filter(([key]) => !(key in cliFlags)))
149+
flatten(filteredPublishConfig, opts)
145150
}
146151

147152
const versions = await Unpublish.getKeysOfVersions(spec.name, opts)

tap-snapshots/test/lib/commands/publish.js.test.cjs

+4
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,10 @@ exports[`test/lib/commands/publish.js TAP restricted access > new package versio
452452
453453
`
454454

455+
exports[`test/lib/commands/publish.js TAP prioritize CLI flags over publishConfig > new package version 1`] = `
456+
457+
`
458+
455459
exports[`test/lib/commands/publish.js TAP scoped _auth config scoped registry > new package version 1`] = `
456460
457461
`

test/lib/commands/publish.js

+52
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,58 @@ t.test('re-loads publishConfig.registry if added during script process', async t
131131
t.matchSnapshot(joinedOutput(), 'new package version')
132132
})
133133

134+
t.test('prioritize CLI flags over publishConfig', async t => {
135+
const publishConfig = { registry: 'http://publishconfig' }
136+
const { joinedOutput, npm } = await loadMockNpm(t, {
137+
config: {
138+
[`${alternateRegistry.slice(6)}/:_authToken`]: 'test-other-token',
139+
},
140+
prefixDir: {
141+
'package.json': JSON.stringify({
142+
...pkgJson,
143+
scripts: {
144+
prepare: 'cp new.json package.json',
145+
},
146+
}, null, 2),
147+
'new.json': JSON.stringify({
148+
...pkgJson,
149+
publishConfig,
150+
}),
151+
},
152+
argv: ['--registry', alternateRegistry],
153+
})
154+
const registry = new MockRegistry({
155+
tap: t,
156+
registry: alternateRegistry,
157+
authorization: 'test-other-token',
158+
})
159+
registry.nock.put(`/${pkg}`, body => {
160+
return t.match(body, {
161+
_id: pkg,
162+
name: pkg,
163+
'dist-tags': { latest: '1.0.0' },
164+
access: null,
165+
versions: {
166+
'1.0.0': {
167+
name: pkg,
168+
version: '1.0.0',
169+
_id: `${pkg}@1.0.0`,
170+
dist: {
171+
shasum: /\.*/,
172+
tarball: `http:${alternateRegistry.slice(6)}/test-package/-/test-package-1.0.0.tgz`,
173+
},
174+
publishConfig,
175+
},
176+
},
177+
_attachments: {
178+
[`${pkg}-1.0.0.tgz`]: {},
179+
},
180+
})
181+
}).reply(200, {})
182+
await npm.exec('publish', [])
183+
t.matchSnapshot(joinedOutput(), 'new package version')
184+
})
185+
134186
t.test('json', async t => {
135187
const { joinedOutput, npm, logs } = await loadMockNpm(t, {
136188
config: {

test/lib/commands/unpublish.js

+30
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,36 @@ t.test('publishConfig no spec', async t => {
408408
t.equal(joinedOutput(), '- test-package')
409409
})
410410

411+
t.test('prioritize CLI flags over publishConfig no spec', async t => {
412+
const alternateRegistry = 'https://other.registry.npmjs.org'
413+
const publishConfig = { registry: 'http://publishconfig' }
414+
const { joinedOutput, npm } = await loadMockNpm(t, {
415+
config: {
416+
force: true,
417+
'//other.registry.npmjs.org/:_authToken': 'test-other-token',
418+
},
419+
prefixDir: {
420+
'package.json': JSON.stringify({
421+
name: pkg,
422+
version: '1.0.0',
423+
publishConfig,
424+
}, null, 2),
425+
},
426+
argv: ['--registry', alternateRegistry],
427+
})
428+
429+
const registry = new MockRegistry({
430+
tap: t,
431+
registry: alternateRegistry,
432+
authorization: 'test-other-token',
433+
})
434+
const manifest = registry.manifest({ name: pkg })
435+
await registry.package({ manifest, query: { write: true }, times: 2 })
436+
registry.unpublish({ manifest })
437+
await npm.exec('unpublish', [])
438+
t.equal(joinedOutput(), '- test-package')
439+
})
440+
411441
t.test('publishConfig with spec', async t => {
412442
const alternateRegistry = 'https://other.registry.npmjs.org'
413443
const { joinedOutput, npm } = await loadMockNpm(t, {

0 commit comments

Comments
 (0)