From 6d977398906bb6a46bcd524032cba8abf93a8fc8 Mon Sep 17 00:00:00 2001 From: await-ovo <13152410380@163.com> Date: Tue, 19 Sep 2023 06:41:49 +0800 Subject: [PATCH] feat(plugin-commands-config): throw meaningful error for config sub commands (#7108) close #7106 --- .changeset/four-carpets-itch.md | 6 ++++ config/plugin-commands-config/src/config.ts | 31 ++++++++++++------- .../test/configGet.test.ts | 23 ++++++++++++++ .../test/configSet.test.ts | 24 ++++++++++++++ 4 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 .changeset/four-carpets-itch.md diff --git a/.changeset/four-carpets-itch.md b/.changeset/four-carpets-itch.md new file mode 100644 index 0000000000..95c2164b15 --- /dev/null +++ b/.changeset/four-carpets-itch.md @@ -0,0 +1,6 @@ +--- +"@pnpm/plugin-commands-config": patch +"pnpm": patch +--- + +Throw meaningful error for config sub commands.[#7106](https://github.com/pnpm/pnpm/issues/7106) diff --git a/config/plugin-commands-config/src/config.ts b/config/plugin-commands-config/src/config.ts index b312cadd49..14e40da244 100644 --- a/config/plugin-commands-config/src/config.ts +++ b/config/plugin-commands-config/src/config.ts @@ -87,20 +87,29 @@ export async function handler (opts: ConfigCommandOptions, params: string[]) { opts.global = true } switch (params[0]) { - case 'set': { - let [key, value] = params.slice(1) - if (value == null) { - const parts = key.split('=') - key = parts.shift()! - value = parts.join('=') + case 'set': + case 'delete': { + if (!params[1]) { + throw new PnpmError('CONFIG_NO_PARAMS', `\`pnpm config ${params[0]}\` requires the config key`) + } + if (params[0] === 'set') { + let [key, value] = params.slice(1) + if (value == null) { + const parts = key.split('=') + key = parts.shift()! + value = parts.join('=') + } + return configSet(opts, key, value ?? '') + } else { + return configSet(opts, params[1], null) } - return configSet(opts, key, value ?? '') } case 'get': { - return configGet(opts, params[1]) - } - case 'delete': { - return configSet(opts, params[1], null) + if (params[1]) { + return configGet(opts, params[1]) + } else { + return configList(opts) + } } case 'list': { return configList(opts) diff --git a/config/plugin-commands-config/test/configGet.test.ts b/config/plugin-commands-config/test/configGet.test.ts index 818f5a4043..9c579eba0f 100644 --- a/config/plugin-commands-config/test/configGet.test.ts +++ b/config/plugin-commands-config/test/configGet.test.ts @@ -27,3 +27,26 @@ test('config get a boolean should return string format', async () => { expect(configKey).toEqual('true') }) + +test('config get without key show list all settings ', async () => { + const rawConfig = { + 'store-dir': '~/store', + 'fetch-retries': '2', + } + const getOutput = await config.handler({ + dir: process.cwd(), + cliOptions: {}, + configDir: process.cwd(), + global: true, + rawConfig, + }, ['get']) + + const listOutput = await config.handler({ + dir: process.cwd(), + cliOptions: {}, + configDir: process.cwd(), + rawConfig, + }, ['list']) + + expect(getOutput).toEqual(listOutput) +}) diff --git a/config/plugin-commands-config/test/configSet.test.ts b/config/plugin-commands-config/test/configSet.test.ts index 989bf7cce0..bb5e620804 100644 --- a/config/plugin-commands-config/test/configSet.test.ts +++ b/config/plugin-commands-config/test/configSet.test.ts @@ -1,5 +1,6 @@ import fs from 'fs' import path from 'path' +import { PnpmError } from '@pnpm/error' import { tempDir } from '@pnpm/prepare' import { config } from '@pnpm/plugin-commands-config' import { readIniFileSync } from 'read-ini-file' @@ -123,3 +124,26 @@ test('config set key=value, when value contains a "="', async () => { foo: 'bar=qar', }) }) + +test('config set or delete throws missing params error', async () => { + const tmp = tempDir() + const configDir = path.join(tmp, 'global-config') + fs.mkdirSync(configDir, { recursive: true }) + fs.writeFileSync(path.join(tmp, '.npmrc'), 'store-dir=~/store') + + await expect(config.handler({ + dir: process.cwd(), + cliOptions: {}, + configDir, + location: 'project', + rawConfig: {}, + }, ['set'])).rejects.toThrow(new PnpmError('CONFIG_NO_PARAMS', '`pnpm config set` requires the config key')) + + await expect(config.handler({ + dir: process.cwd(), + cliOptions: {}, + configDir, + location: 'project', + rawConfig: {}, + }, ['delete'])).rejects.toThrow(new PnpmError('CONFIG_NO_PARAMS', '`pnpm config delete` requires the config key')) +})