fix(config): config get returns auth related settings properly (#9346)

close #9345
This commit is contained in:
Zoltan Kochan
2025-03-31 03:34:17 +02:00
committed by GitHub
parent e57f1df3cf
commit 1b1ed10e1a
8 changed files with 29 additions and 15 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-config": patch
"pnpm": patch
---
`pnpm config get` should read auth related settings via npm CLI [#9345](https://github.com/pnpm/pnpm/issues/9345).

View File

@@ -75,7 +75,9 @@ export function help (): string {
})
}
export async function handler (opts: ConfigCommandOptions, params: string[]): Promise<string | undefined> {
export type ConfigHandlerResult = string | undefined | { output: string, exitCode: number }
export async function handler (opts: ConfigCommandOptions, params: string[]): Promise<ConfigHandlerResult> {
if (params.length === 0) {
throw new PnpmError('CONFIG_NO_SUBCOMMAND', 'Please specify the subcommand', {
hint: help(),

View File

@@ -1,7 +1,13 @@
import kebabCase from 'lodash.kebabcase'
import { runNpm } from '@pnpm/run-npm'
import { type ConfigCommandOptions } from './ConfigCommandOptions'
import { settingShouldFallBackToNpm } from './configSet'
export function configGet (opts: ConfigCommandOptions, key: string): string {
export function configGet (opts: ConfigCommandOptions, key: string): { output: string, exitCode: number } {
if (opts.global && settingShouldFallBackToNpm(key)) {
const { status: exitCode } = runNpm(opts.npmPath, ['config', 'get', key])
return { output: '', exitCode: exitCode ?? 0 }
}
const config = opts.rawConfig[kebabCase(key)]
return Array.isArray(config) ? config.join(',') : String(config)
return { output: Array.isArray(config) ? config.join(',') : String(config), exitCode: 0 }
}

View File

@@ -38,7 +38,7 @@ export async function configSet (opts: ConfigCommandOptions, key: string, value:
})
}
function settingShouldFallBackToNpm (key: string): boolean {
export function settingShouldFallBackToNpm (key: string): boolean {
return (
['registry', '_auth', '_authToken', 'username', '_password'].includes(key) ||
key[0] === '@' ||

View File

@@ -7,6 +7,6 @@ export const help = configCmd.help
export const commandNames = ['get']
export async function handler (opts: ConfigCommandOptions, params: string[]): Promise<string | undefined> {
export async function handler (opts: ConfigCommandOptions, params: string[]): Promise<configCmd.ConfigHandlerResult> {
return configCmd.handler(opts, ['get', ...params])
}

View File

@@ -7,6 +7,6 @@ export const help = configCmd.help
export const commandNames = ['set']
export async function handler (opts: ConfigCommandOptions, params: string[]): Promise<string | undefined> {
export async function handler (opts: ConfigCommandOptions, params: string[]): Promise<configCmd.ConfigHandlerResult> {
return configCmd.handler(opts, ['set', ...params])
}

View File

@@ -1,7 +1,7 @@
import { config } from '@pnpm/plugin-commands-config'
test('config get', async () => {
const configKey = await config.handler({
const getResult = await config.handler({
dir: process.cwd(),
cliOptions: {},
configDir: process.cwd(),
@@ -11,11 +11,11 @@ test('config get', async () => {
},
}, ['get', 'store-dir'])
expect(configKey).toEqual('~/store')
expect(typeof getResult === 'object' && 'output' in getResult && getResult.output).toEqual('~/store')
})
test('config get works with camelCase', async () => {
const configKey = await config.handler({
const getResult = await config.handler({
dir: process.cwd(),
cliOptions: {},
configDir: process.cwd(),
@@ -25,11 +25,11 @@ test('config get works with camelCase', async () => {
},
}, ['get', 'storeDir'])
expect(configKey).toEqual('~/store')
expect(typeof getResult === 'object' && 'output' in getResult && getResult.output).toEqual('~/store')
})
test('config get a boolean should return string format', async () => {
const configKey = await config.handler({
const getResult = await config.handler({
dir: process.cwd(),
cliOptions: {},
configDir: process.cwd(),
@@ -39,11 +39,11 @@ test('config get a boolean should return string format', async () => {
},
}, ['get', 'update-notifier'])
expect(configKey).toEqual('true')
expect(typeof getResult === 'object' && 'output' in getResult && getResult.output).toEqual('true')
})
test('config get on array should return a comma-separated list', async () => {
const configKey = await config.handler({
const getResult = await config.handler({
dir: process.cwd(),
cliOptions: {},
configDir: process.cwd(),
@@ -56,7 +56,7 @@ test('config get on array should return a comma-separated list', async () => {
},
}, ['get', 'public-hoist-pattern'])
expect(configKey).toBe('*eslint*,*prettier*')
expect(typeof getResult === 'object' && 'output' in getResult && getResult.output).toBe('*eslint*,*prettier*')
})
test('config get without key show list all settings ', async () => {

View File

@@ -17,7 +17,7 @@ test('config list', async () => {
},
}, ['list'])
expect(normalizeNewlines(output!)).toEqual(`fetch-retries=2
expect(typeof output === 'string' && normalizeNewlines(output!)).toEqual(`fetch-retries=2
store-dir=~/store
`)
})