diff --git a/.changeset/brave-coats-beam.md b/.changeset/brave-coats-beam.md new file mode 100644 index 00000000000..db00ba84fdc --- /dev/null +++ b/.changeset/brave-coats-beam.md @@ -0,0 +1,6 @@ +--- +"@pnpm/plugin-commands-config": patch +"pnpm": patch +--- + +The config command should work with the `--location=global` CLI option [#5841](https://github.com/pnpm/pnpm/issues/5841). diff --git a/config/plugin-commands-config/src/ConfigCommandOptions.ts b/config/plugin-commands-config/src/ConfigCommandOptions.ts index 661b0815d53..a4bed578bc5 100644 --- a/config/plugin-commands-config/src/ConfigCommandOptions.ts +++ b/config/plugin-commands-config/src/ConfigCommandOptions.ts @@ -5,4 +5,7 @@ export type ConfigCommandOptions = Pick & { json?: boolean } +> & { + json?: boolean + location?: 'global' | 'project' +} diff --git a/config/plugin-commands-config/src/config.ts b/config/plugin-commands-config/src/config.ts index 8a72533c071..7d5b584ceda 100644 --- a/config/plugin-commands-config/src/config.ts +++ b/config/plugin-commands-config/src/config.ts @@ -13,6 +13,7 @@ export function rcOptionsTypes () { export function cliOptionsTypes () { return { global: Boolean, + location: ['global', 'project'], json: Boolean, } } @@ -71,6 +72,9 @@ export async function handler (opts: ConfigCommandOptions, params: string[]) { hint: help(), }) } + if (opts.location) { + opts.global = opts.location === 'global' + } switch (params[0]) { case 'set': { return configSet(opts, params[1], params[2] ?? '') diff --git a/config/plugin-commands-config/test/configSet.test.ts b/config/plugin-commands-config/test/configSet.test.ts index feeb471a48a..1a77238ec55 100644 --- a/config/plugin-commands-config/test/configSet.test.ts +++ b/config/plugin-commands-config/test/configSet.test.ts @@ -4,7 +4,7 @@ import { tempDir } from '@pnpm/prepare' import { config } from '@pnpm/plugin-commands-config' import { readIniFileSync } from 'read-ini-file' -test('config set', async () => { +test('config set using the global option', async () => { const tmp = tempDir() const configDir = path.join(tmp, 'global-config') fs.mkdirSync(configDir, { recursive: true }) @@ -22,3 +22,59 @@ test('config set', async () => { 'fetch-retries': '1', }) }) + +test('config set using the location=global option', async () => { + const tmp = tempDir() + const configDir = path.join(tmp, 'global-config') + fs.mkdirSync(configDir, { recursive: true }) + fs.writeFileSync(path.join(configDir, 'rc'), 'store-dir=~/store') + + await config.handler({ + dir: process.cwd(), + configDir, + location: 'global', + rawConfig: {}, + }, ['set', 'fetch-retries', '1']) + + expect(readIniFileSync(path.join(configDir, 'rc'))).toEqual({ + 'store-dir': '~/store', + 'fetch-retries': '1', + }) +}) + +test('config set using the location=project option', 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 config.handler({ + dir: process.cwd(), + configDir, + location: 'project', + rawConfig: {}, + }, ['set', 'fetch-retries', '1']) + + expect(readIniFileSync(path.join(tmp, '.npmrc'))).toEqual({ + 'store-dir': '~/store', + 'fetch-retries': '1', + }) +}) + +test('config set in project .npmrc file', async () => { + const tmp = tempDir() + const configDir = path.join(tmp, 'global-config') + fs.writeFileSync(path.join(tmp, '.npmrc'), 'store-dir=~/store') + + await config.handler({ + dir: process.cwd(), + configDir, + global: false, + rawConfig: {}, + }, ['set', 'fetch-retries', '1']) + + expect(readIniFileSync(path.join(tmp, '.npmrc'))).toEqual({ + 'store-dir': '~/store', + 'fetch-retries': '1', + }) +})