mirror of
https://github.com/pnpm/pnpm.git
synced 2026-04-27 10:30:58 -04:00
fix(config): config get returns auth related settings properly (#9346)
close #9345
This commit is contained in:
6
.changeset/quiet-bikes-ask.md
Normal file
6
.changeset/quiet-bikes-ask.md
Normal 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).
|
||||
@@ -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(),
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
|
||||
@@ -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] === '@' ||
|
||||
|
||||
@@ -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])
|
||||
}
|
||||
|
||||
@@ -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])
|
||||
}
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -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
|
||||
`)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user