Skip to content

Commit

Permalink
fix(publish): consider package-spec when inside workspace dir (#7738)
Browse files Browse the repository at this point in the history
This PR fixes an issue where the `npm publish` command would fail when
run from within a workspace directory with package-spec

fixes: #7726
  • Loading branch information
milaninfy authored Aug 21, 2024
1 parent 24d5350 commit 2404c7e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ class Publish extends BaseCommand {
await this.#publish(args)
}

async execWorkspaces () {
async execWorkspaces (args) {
const useWorkspaces = args.length === 0 || args.includes('.')
if (!useWorkspaces) {
log.warn('Ignoring workspaces for specified package(s)')
return this.exec(args)
}
await this.setWorkspaces()

for (const [name, workspace] of this.workspaces.entries()) {
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 @@ -393,6 +393,10 @@ exports[`test/lib/commands/publish.js TAP workspaces all workspaces - some marke
+ workspace-a@1.2.3-a
`

exports[`test/lib/commands/publish.js TAP workspaces differet package spec > publish different package spec 1`] = `
+ pkg@1.2.3
`

exports[`test/lib/commands/publish.js TAP workspaces json > all workspaces in json 1`] = `
{
"workspace-a": {
Expand Down
42 changes: 42 additions & 0 deletions test/lib/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,48 @@ t.test('workspaces', t => {
await npm.exec('publish', [])
t.matchSnapshot(joinedOutput(), 'all workspaces in json')
})

t.test('differet package spec', async t => {
const testDir = {
'package.json': JSON.stringify(
{
...pkgJson,
workspaces: ['workspace-a'],
}, null, 2),
'workspace-a': {
'package.json': JSON.stringify({
name: 'workspace-a',
version: '1.2.3-a',
}),
},
'dir/pkg': {
'package.json': JSON.stringify({
name: 'pkg',
version: '1.2.3',
}),
},
}

const { npm, joinedOutput } = await loadMockNpm(t, {
config: {
...auth,
},
prefixDir: testDir,
chdir: ({ prefix }) => path.resolve(prefix, './workspace-a'),
})
const registry = new MockRegistry({
tap: t,
registry: npm.config.get('registry'),
authorization: token,
})
registry.nock
.put('/pkg', body => {
return t.match(body, { name: 'pkg' })
}).reply(200, {})
await npm.exec('publish', ['../dir/pkg'])
t.matchSnapshot(joinedOutput(), 'publish different package spec')
})

t.end()
})

Expand Down

0 comments on commit 2404c7e

Please sign in to comment.