fix: config set should save the settings in the right format (#9357)

close #9355
This commit is contained in:
Zoltan Kochan
2025-04-01 12:06:58 +02:00
parent 1b1ed10e1a
commit e059d998cc
3 changed files with 59 additions and 1 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-config": patch
"pnpm": patch
---
`pnpm config set` should convert the settings to their correct type before adding them to `pnpm-workspace.yaml` [#9355](https://github.com/pnpm/pnpm/issues/9355).

View File

@@ -1,6 +1,7 @@
import fs from 'fs'
import path from 'path'
import util from 'util'
import { types } from '@pnpm/config'
import { runNpm } from '@pnpm/run-npm'
import { updateWorkspaceManifest } from '@pnpm/workspace.manifest-writer'
import camelCase from 'camelcase'
@@ -34,10 +35,43 @@ export async function configSet (opts: ConfigCommandOptions, key: string, value:
}
key = camelCase(key)
await updateWorkspaceManifest(opts.workspaceDir ?? opts.dir, {
[key]: value,
[key]: castField(value, kebabCase(key)),
})
}
function castField (value: unknown, key: string) {
if (typeof value !== 'string') {
return value
}
const type = types[key as keyof typeof types] as (string | number | boolean | null | NumberConstructor)
const typeList = Array.isArray(type) ? type : [type]
const isNumber = typeList.includes(Number)
value = value.trim()
switch (value) {
case 'true': {
return true
}
case 'false': {
return false
}
case 'null': {
return null
}
case 'undefined': {
return undefined
}
}
if (isNumber && !isNaN(value as number)) {
value = Number(value)
}
return value
}
export function settingShouldFallBackToNpm (key: string): boolean {
return (
['registry', '_auth', '_authToken', 'username', '_password'].includes(key) ||

View File

@@ -112,6 +112,24 @@ test('config set using the location=project option', async () => {
})
})
test('config set saves the setting in the right format to pnpm-workspace.yaml', async () => {
const tmp = tempDir()
const configDir = path.join(tmp, 'global-config')
fs.mkdirSync(configDir, { recursive: true })
await config.handler({
dir: process.cwd(),
cliOptions: {},
configDir,
location: 'project',
rawConfig: {},
}, ['set', 'fetch-timeout', '1000'])
expect(readYamlFile(path.join(tmp, 'pnpm-workspace.yaml'))).toEqual({
fetchTimeout: 1000,
})
})
test('config set in project .npmrc file', async () => {
const tmp = tempDir()
const configDir = path.join(tmp, 'global-config')