mirror of
https://github.com/pnpm/pnpm.git
synced 2026-04-27 18:46:18 -04:00
fix: config set should save the settings in the right format (#9357)
close #9355
This commit is contained in:
6
.changeset/breezy-clouds-kick.md
Normal file
6
.changeset/breezy-clouds-kick.md
Normal 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).
|
||||
@@ -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) ||
|
||||
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user