fix(config): the --location=global CLI option should work (#5843)

ref #5841
This commit is contained in:
Zoltan Kochan
2022-12-27 19:46:22 +02:00
committed by GitHub
parent b3dfa3ba8a
commit 83a627a25c
4 changed files with 71 additions and 2 deletions

View File

@@ -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).

View File

@@ -5,4 +5,7 @@ export type ConfigCommandOptions = Pick<Config,
| 'dir'
| 'global'
| 'rawConfig'
> & { json?: boolean }
> & {
json?: boolean
location?: 'global' | 'project'
}

View File

@@ -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] ?? '')

View File

@@ -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',
})
})