Skip to content

Commit

Permalink
feat: handle workspace protocol with any semver range specifier. fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
EWhite613 committed Feb 11, 2024
1 parent 25524bd commit bcdd4f5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-geese-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pnpm/exportable-manifest": minor
---

feat: handle workspace protocol with any semver range specifier. fixes #7578
15 changes: 8 additions & 7 deletions pkg-manifest/exportable-manifest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ async function makePublishDependencies (
}

async function makePublishDependency (depName: string, depSpec: string, dir: string, modulesDir?: string) {
if (!depSpec.startsWith('workspace:')) {
if (!depSpec.includes('workspace:')) {
return depSpec
}

// Dependencies with bare "*", "^" and "~" versions
const versionAliasSpecParts = /^workspace:(.*?)@?([\^~*])$/.exec(depSpec)
// Dependencies with bare "*", "^", "~",">=",">","<=",< versions
const workspaceSemverRegex = /workspace:(.*?)@?([\^~*]|>=|>|<=|<)/
const versionAliasSpecParts = workspaceSemverRegex.exec(depSpec)
if (versionAliasSpecParts != null) {
modulesDir = modulesDir ?? path.join(dir, 'node_modules')
const { manifest } = await tryReadProjectManifest(path.join(modulesDir, depName))
Expand All @@ -80,10 +81,10 @@ async function makePublishDependency (depName: string, depSpec: string, dir: str
if (depName !== manifest.name) {
return `npm:${manifest.name!}@${semverRangeToken}${manifest.version}`
}
return `${semverRangeToken}${manifest.version}`
return depSpec.replace(workspaceSemverRegex, `${semverRangeToken}${manifest.version}`)
}
if (depSpec.startsWith('workspace:./') || depSpec.startsWith('workspace:../')) {
const { manifest } = await tryReadProjectManifest(path.join(dir, depSpec.slice(10)))
if (depSpec.includes('workspace:./') || depSpec.includes('workspace:../')) {
const { manifest } = await tryReadProjectManifest(path.join(dir, depSpec.replace('workspace:', '')))
if (!manifest?.name || !manifest?.version) {
throw new PnpmError(
'CANNOT_RESOLVE_WORKSPACE_PROTOCOL',
Expand All @@ -94,7 +95,7 @@ async function makePublishDependency (depName: string, depSpec: string, dir: str
if (manifest.name === depName) return `${manifest.version}`
return `npm:${manifest.name}@${manifest.version}`
}
depSpec = depSpec.slice(10)
depSpec = depSpec.replace('workspace:', '')
if (depSpec.includes('@')) {
return `npm:${depSpec}`
}
Expand Down
8 changes: 8 additions & 0 deletions pkg-manifest/exportable-manifest/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ test('workspace deps are replaced', async () => {
baz: 'workspace:baz@^',
foo: 'workspace:*',
},
peerDependencies: {
foo: 'workspace:>= || ^3.9.0',
baz: '^1.0.0 || workspace:baz@:>',
},
}

preparePackages([
Expand Down Expand Up @@ -123,5 +127,9 @@ test('workspace deps are replaced', async () => {
baz: '^1.2.3',
foo: '4.5.6',
},
peerDependencies: {
baz: '^1.0.0 || >1.2.3',
foo: '>=4.5.6 || ^3.9.0',
},
})
})

0 comments on commit bcdd4f5

Please sign in to comment.