fix: pnpm doesn't respect save-prefix (#3420)

When save-prefix in .npmrc is set to an empty string,
 pnpm does not honor it and sets the package version with the default ^ as a prefix.

Close #3414

Co-authored-by: amit <amit@enso.security>
This commit is contained in:
amit
2021-05-06 15:50:28 +04:00
committed by GitHub
parent e8c61859f2
commit 9d2ff0309b
3 changed files with 48 additions and 1 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/plugin-commands-installation": patch
---
Fix: align pnpm save-prefix behavior when a range is not specified explicitly.

View File

@@ -1,4 +1,4 @@
export default function getPinnedVersion (opts: { saveExact?: boolean, savePrefix?: string }) {
if (opts.saveExact === true) return 'patch'
if (opts.saveExact === true || opts.savePrefix === '') return 'patch'
return opts.savePrefix === '~' ? 'minor' : 'major'
}

View File

@@ -243,3 +243,45 @@ test('pnpm add --save-peer', async () => {
)
}
})
test('pnpm add - with save-prefix set to empty string should save package version without prefix', async () => {
prepare()
await add.handler({
...DEFAULT_OPTIONS,
dir: process.cwd(),
linkWorkspacePackages: false,
savePrefix: '',
}, ['is-positive@1.0.0'])
{
const manifest = await loadJsonFile(path.resolve('package.json'))
expect(
manifest
).toStrictEqual(
{
name: 'project',
version: '0.0.0',
dependencies: { 'is-positive': '1.0.0' },
}
)
}
})
test('pnpm add - should add prefix when set in .npmrc when a range is not specified explicitly', async () => {
prepare()
await add.handler({
...DEFAULT_OPTIONS,
dir: process.cwd(),
linkWorkspacePackages: false,
savePrefix: '~',
}, ['is-positive'])
{
const manifest = (await import(path.resolve('package.json')))
expect(
manifest.dependencies['is-positive']
).toMatch(/~([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/)
}
})