Skip to content

Commit e4c7a41

Browse files
authored
fix(publish): skip workspace packages marked private on publish (#7564)
`npm publish --workspaces` will skip workspace packages marked as private in package.json. Currently it's skipping those packages only when you have configured auth for those packages, it would error out with `ENEEDAUTH` if it doesn't find the valid auth information. this fix checks for the private property before checking for auth for the packages that will essentially not going to get published. Fixes #7199
1 parent 8f94ae8 commit e4c7a41

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

lib/commands/publish.js

+9
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ class Publish extends BaseCommand {
127127
const noCreds = !(creds.token || creds.username || creds.certfile && creds.keyfile)
128128
const outputRegistry = replaceInfo(registry)
129129

130+
// if a workspace package is marked private then we skip it
131+
if (workspace && manifest.private) {
132+
throw Object.assign(
133+
new Error(`This package has been marked as private
134+
Remove the 'private' field from the package.json to publish it.`),
135+
{ code: 'EPRIVATE' }
136+
)
137+
}
138+
130139
if (noCreds) {
131140
const msg = `This command requires you to be logged in to ${outputRegistry}`
132141
if (dryRun) {

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

+4
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,10 @@ Array [
418418
]
419419
`
420420

421+
exports[`test/lib/commands/publish.js TAP workspaces all workspaces - some marked private > one marked private 1`] = `
422+
+ workspace-a@1.2.3-a
423+
`
424+
421425
exports[`test/lib/commands/publish.js TAP workspaces json > all workspaces in json 1`] = `
422426
{
423427
"workspace-a": {

test/lib/commands/publish.js

+42
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,48 @@ t.test('workspaces', t => {
617617
await t.rejects(npm.exec('publish', []), { code: 'E404' })
618618
})
619619

620+
t.test('all workspaces - some marked private', async t => {
621+
const testDir = {
622+
'package.json': JSON.stringify(
623+
{
624+
...pkgJson,
625+
workspaces: ['workspace-a', 'workspace-p'],
626+
}, null, 2),
627+
'workspace-a': {
628+
'package.json': JSON.stringify({
629+
name: 'workspace-a',
630+
version: '1.2.3-a',
631+
}),
632+
},
633+
'workspace-p': {
634+
'package.json': JSON.stringify({
635+
name: '@scoped/workspace-p',
636+
private: true,
637+
version: '1.2.3-p-scoped',
638+
}),
639+
},
640+
}
641+
642+
const { npm, joinedOutput } = await loadMockNpm(t, {
643+
config: {
644+
...auth,
645+
workspaces: true,
646+
},
647+
prefixDir: testDir,
648+
})
649+
const registry = new MockRegistry({
650+
tap: t,
651+
registry: npm.config.get('registry'),
652+
authorization: token,
653+
})
654+
registry.nock
655+
.put('/workspace-a', body => {
656+
return t.match(body, { name: 'workspace-a' })
657+
}).reply(200, {})
658+
await npm.exec('publish', [])
659+
t.matchSnapshot(joinedOutput(), 'one marked private')
660+
})
661+
620662
t.test('invalid workspace', async t => {
621663
const { npm } = await loadMockNpm(t, {
622664
config: {

0 commit comments

Comments
 (0)