From 1b1ed10e1aed4161a0fed982cc73a794086da700 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Mon, 31 Mar 2025 03:34:17 +0200 Subject: [PATCH] fix(config): config get returns auth related settings properly (#9346) close #9345 --- .changeset/quiet-bikes-ask.md | 6 ++++++ config/plugin-commands-config/src/config.ts | 4 +++- config/plugin-commands-config/src/configGet.ts | 10 ++++++++-- config/plugin-commands-config/src/configSet.ts | 2 +- config/plugin-commands-config/src/get.ts | 2 +- config/plugin-commands-config/src/set.ts | 2 +- .../test/configGet.test.ts | 16 ++++++++-------- .../test/configList.test.ts | 2 +- 8 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 .changeset/quiet-bikes-ask.md diff --git a/.changeset/quiet-bikes-ask.md b/.changeset/quiet-bikes-ask.md new file mode 100644 index 0000000000..596b6dfdfd --- /dev/null +++ b/.changeset/quiet-bikes-ask.md @@ -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). diff --git a/config/plugin-commands-config/src/config.ts b/config/plugin-commands-config/src/config.ts index 421615d2bc..f14e060814 100644 --- a/config/plugin-commands-config/src/config.ts +++ b/config/plugin-commands-config/src/config.ts @@ -75,7 +75,9 @@ export function help (): string { }) } -export async function handler (opts: ConfigCommandOptions, params: string[]): Promise { +export type ConfigHandlerResult = string | undefined | { output: string, exitCode: number } + +export async function handler (opts: ConfigCommandOptions, params: string[]): Promise { if (params.length === 0) { throw new PnpmError('CONFIG_NO_SUBCOMMAND', 'Please specify the subcommand', { hint: help(), diff --git a/config/plugin-commands-config/src/configGet.ts b/config/plugin-commands-config/src/configGet.ts index 72e4ed5bad..e373a8a08b 100644 --- a/config/plugin-commands-config/src/configGet.ts +++ b/config/plugin-commands-config/src/configGet.ts @@ -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 } } diff --git a/config/plugin-commands-config/src/configSet.ts b/config/plugin-commands-config/src/configSet.ts index ba1f1d00b5..b5d38fe47b 100644 --- a/config/plugin-commands-config/src/configSet.ts +++ b/config/plugin-commands-config/src/configSet.ts @@ -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] === '@' || diff --git a/config/plugin-commands-config/src/get.ts b/config/plugin-commands-config/src/get.ts index 62347779ad..5159ed685b 100644 --- a/config/plugin-commands-config/src/get.ts +++ b/config/plugin-commands-config/src/get.ts @@ -7,6 +7,6 @@ export const help = configCmd.help export const commandNames = ['get'] -export async function handler (opts: ConfigCommandOptions, params: string[]): Promise { +export async function handler (opts: ConfigCommandOptions, params: string[]): Promise { return configCmd.handler(opts, ['get', ...params]) } diff --git a/config/plugin-commands-config/src/set.ts b/config/plugin-commands-config/src/set.ts index 6b7665fd0e..aed34ae40c 100644 --- a/config/plugin-commands-config/src/set.ts +++ b/config/plugin-commands-config/src/set.ts @@ -7,6 +7,6 @@ export const help = configCmd.help export const commandNames = ['set'] -export async function handler (opts: ConfigCommandOptions, params: string[]): Promise { +export async function handler (opts: ConfigCommandOptions, params: string[]): Promise { return configCmd.handler(opts, ['set', ...params]) } diff --git a/config/plugin-commands-config/test/configGet.test.ts b/config/plugin-commands-config/test/configGet.test.ts index 0ecb73fd08..09cfcbacf8 100644 --- a/config/plugin-commands-config/test/configGet.test.ts +++ b/config/plugin-commands-config/test/configGet.test.ts @@ -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 () => { diff --git a/config/plugin-commands-config/test/configList.test.ts b/config/plugin-commands-config/test/configList.test.ts index b68cd8cfe5..4b985920ab 100644 --- a/config/plugin-commands-config/test/configList.test.ts +++ b/config/plugin-commands-config/test/configList.test.ts @@ -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 `) })