Skip to content

Commit 98905ae

Browse files
nlfruyadorno
authored andcommitted
feat(config): introduce 'location' parameter
PR-URL: #3471 Credit: @nlf Close: #3471 Reviewed-by: @wraithgar
1 parent 801a523 commit 98905ae

File tree

12 files changed

+113
-24
lines changed

12 files changed

+113
-24
lines changed

docs/content/commands/npm-config.md

+8
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ folder instead of the current working directory. See
131131
132132
The command to run for `npm edit` and `npm config edit`.
133133
134+
#### `location`
135+
136+
* Default: "user" unless `--global` is passed, which will also set this value
137+
to "global"
138+
* Type: "global", "user", or "project"
139+
140+
When passed to `npm config` this refers to which config file to use.
141+
134142
#### `long`
135143
136144
* Default: false

docs/content/using-npm/config.md

+9
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ The following shorthands are parsed on the command-line:
6767
* `--desc`: `--description`
6868
* `-f`: `--force`
6969
* `-g`: `--global`
70+
* `-L`: `--location`
7071
* `-d`: `--loglevel info`
7172
* `-s`: `--loglevel silent`
7273
* `--silent`: `--loglevel silent`
@@ -758,6 +759,14 @@ Used with `npm ls`, limiting output to only those packages that are linked.
758759
The IP address of the local interface to use when making connections to the
759760
npm registry. Must be IPv4 in versions of Node prior to 0.12.
760761

762+
#### `location`
763+
764+
* Default: "user" unless `--global` is passed, which will also set this value
765+
to "global"
766+
* Type: "global", "user", or "project"
767+
768+
When passed to `npm config` this refers to which config file to use.
769+
761770
#### `loglevel`
762771

763772
* Default: "notice"

lib/config.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class Config extends BaseCommand {
5656
'json',
5757
'global',
5858
'editor',
59+
'location',
5960
'long',
6061
]
6162
}
@@ -137,7 +138,7 @@ class Config extends BaseCommand {
137138
if (!args.length)
138139
throw this.usageError()
139140

140-
const where = this.npm.config.get('global') ? 'global' : 'user'
141+
const where = this.npm.config.get('location')
141142
for (const [key, val] of Object.entries(keyValues(args))) {
142143
this.npm.log.info('config', 'set %j %j', key, val)
143144
this.npm.config.set(key, val || '', where)
@@ -167,16 +168,15 @@ class Config extends BaseCommand {
167168
if (!keys.length)
168169
throw this.usageError()
169170

170-
const where = this.npm.config.get('global') ? 'global' : 'user'
171+
const where = this.npm.config.get('location')
171172
for (const key of keys)
172173
this.npm.config.delete(key, where)
173174
await this.npm.config.save(where)
174175
}
175176

176177
async edit () {
177-
const global = this.npm.config.get('global')
178178
const e = this.npm.config.get('editor')
179-
const where = global ? 'global' : 'user'
179+
const where = this.npm.config.get('location')
180180
const file = this.npm.config.data.get(where).source
181181

182182
// save first, just to make sure it's synced up

lib/utils/config/definitions.js

+25
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,31 @@ define('local-address', {
11081108
flatten,
11091109
})
11101110

1111+
define('location', {
1112+
default: 'user',
1113+
short: 'L',
1114+
type: [
1115+
'global',
1116+
'user',
1117+
'project',
1118+
],
1119+
defaultDescription: `
1120+
"user" unless \`--global\` is passed, which will also set this value to "global"
1121+
`,
1122+
description: `
1123+
When passed to \`npm config\` this refers to which config file to use.
1124+
`,
1125+
// NOTE: the flattener here deliberately does not alter the value of global
1126+
// for now, this is to avoid inadvertently causing any breakage. the value of
1127+
// global, however, does modify this flag.
1128+
flatten (key, obj, flatOptions) {
1129+
// if global is set, we override ourselves
1130+
if (obj.global)
1131+
obj.location = 'global'
1132+
flatOptions.location = obj.location
1133+
},
1134+
})
1135+
11111136
define('loglevel', {
11121137
default: 'notice',
11131138
type: [

tap-snapshots/test/lib/config.js.test.cjs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Make sure to inspect the output below. Do not ignore changes!
66
*/
77
'use strict'
8-
exports[`test/lib/config.js TAP config edit --global > should write global config file 1`] = `
8+
exports[`test/lib/config.js TAP config edit --location=global > should write global config file 1`] = `
99
;;;;
1010
; npm globalconfig file: /etc/npmrc
1111
; this is a simple ini-formatted file
@@ -92,8 +92,8 @@ cat = true
9292
chai = true
9393
dog = true
9494
editor = "vi"
95-
global = false
9695
json = false
96+
location = "user"
9797
long = false
9898
9999
; node bin location = /path/to/node
@@ -116,8 +116,8 @@ cat = true
116116
chai = true
117117
dog = true
118118
editor = "vi"
119-
global = false
120119
json = false
120+
location = "user"
121121
long = true
122122
`
123123

@@ -128,8 +128,8 @@ cat = true
128128
chai = true
129129
dog = true
130130
editor = "vi"
131-
global = false
132131
json = false
132+
location = "user"
133133
long = false
134134
135135
; node bin location = /path/to/node
@@ -145,9 +145,9 @@ cat = true
145145
chai = true
146146
dog = true
147147
editor = "vi"
148-
global = false
149148
init.author.name = "Bar"
150149
json = false
150+
location = "user"
151151
long = false
152152
153153
; "user" config from ~/.npmrc

tap-snapshots/test/lib/load-all-commands.js.test.cjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ npm config list [--json]
151151
npm config edit
152152
153153
Options:
154-
[--json] [-g|--global] [--editor <editor>] [-l|--long]
154+
[--json] [-g|--global] [--editor <editor>] [-L|--location <global|user|project>]
155+
[-l|--long]
155156
156157
alias: c
157158

tap-snapshots/test/lib/utils/config/definitions.js.test.cjs

+11
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Array [
8181
"legacy-peer-deps",
8282
"link",
8383
"local-address",
84+
"location",
8485
"loglevel",
8586
"logs-max",
8687
"long",
@@ -1024,6 +1025,16 @@ The IP address of the local interface to use when making connections to the
10241025
npm registry. Must be IPv4 in versions of Node prior to 0.12.
10251026
`
10261027

1028+
exports[`test/lib/utils/config/definitions.js TAP > config description for location 1`] = `
1029+
#### \`location\`
1030+
1031+
* Default: "user" unless \`--global\` is passed, which will also set this value
1032+
to "global"
1033+
* Type: "global", "user", or "project"
1034+
1035+
When passed to \`npm config\` this refers to which config file to use.
1036+
`
1037+
10271038
exports[`test/lib/utils/config/definitions.js TAP > config description for loglevel 1`] = `
10281039
#### \`loglevel\`
10291040

tap-snapshots/test/lib/utils/config/describe-all.js.test.cjs

+8
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,14 @@ Used with \`npm ls\`, limiting output to only those packages that are linked.
637637
The IP address of the local interface to use when making connections to the
638638
npm registry. Must be IPv4 in versions of Node prior to 0.12.
639639
640+
#### \`location\`
641+
642+
* Default: "user" unless \`--global\` is passed, which will also set this value
643+
to "global"
644+
* Type: "global", "user", or "project"
645+
646+
When passed to \`npm config\` this refers to which config file to use.
647+
640648
#### \`loglevel\`
641649
642650
* Default: "notice"

tap-snapshots/test/lib/utils/config/index.js.test.cjs

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ Object {
6464
"l": Array [
6565
"--long",
6666
],
67+
"L": Array [
68+
"--location",
69+
],
6770
"local": Array [
6871
"--no-global",
6972
],

tap-snapshots/test/lib/utils/npm-usage.js.test.cjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ All commands:
294294
npm config edit
295295
296296
Options:
297-
[--json] [-g|--global] [--editor <editor>] [-l|--long]
297+
[--json] [-g|--global] [--editor <editor>] [-L|--location <global|user|project>]
298+
[-l|--long]
298299
299300
alias: c
300301

test/lib/config.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ const defaults = {
4747
const cliConfig = {
4848
editor: 'vi',
4949
json: false,
50+
location: 'user',
5051
long: false,
51-
global: false,
5252
cat: true,
5353
chai: true,
5454
dog: true,
@@ -198,8 +198,8 @@ t.test('config list --json', t => {
198198
{
199199
editor: 'vi',
200200
json: true,
201+
location: 'user',
201202
long: false,
202-
global: false,
203203
cat: true,
204204
chai: true,
205205
dog: true,
@@ -265,7 +265,7 @@ t.test('config delete multiple key', t => {
265265
})
266266
})
267267

268-
t.test('config delete key --global', t => {
268+
t.test('config delete key --location=global', t => {
269269
t.plan(4)
270270

271271
npm.config.delete = (key, where) => {
@@ -277,13 +277,13 @@ t.test('config delete key --global', t => {
277277
t.equal(where, 'global', 'should save global config post-delete')
278278
}
279279

280-
cliConfig.global = true
280+
cliConfig.location = 'global'
281281
config.exec(['delete', 'foo'], (err) => {
282-
t.error(err, 'npm config delete key --global')
282+
t.error(err, 'npm config delete key --location=global')
283283
})
284284

285285
t.teardown(() => {
286-
cliConfig.global = false
286+
cliConfig.location = 'user'
287287
delete npm.config.delete
288288
delete npm.config.save
289289
})
@@ -419,7 +419,7 @@ t.test('config set invalid key', t => {
419419
})
420420
})
421421

422-
t.test('config set key --global', t => {
422+
t.test('config set key --location=global', t => {
423423
t.plan(5)
424424

425425
npm.config.set = (key, val, where) => {
@@ -432,13 +432,13 @@ t.test('config set key --global', t => {
432432
t.equal(where, 'global', 'should save global config')
433433
}
434434

435-
cliConfig.global = true
435+
cliConfig.location = 'global'
436436
config.exec(['set', 'foo', 'bar'], (err) => {
437-
t.error(err, 'npm config set key --global')
437+
t.error(err, 'npm config set key --location=global')
438438
})
439439

440440
t.teardown(() => {
441-
cliConfig.global = false
441+
cliConfig.location = 'user'
442442
delete npm.config.set
443443
delete npm.config.save
444444
})
@@ -583,10 +583,10 @@ sign-git-commit=true`
583583
})
584584
})
585585

586-
t.test('config edit --global', t => {
586+
t.test('config edit --location=global', t => {
587587
t.plan(6)
588588

589-
cliConfig.global = true
589+
cliConfig.location = 'global'
590590
const npmrc = 'init.author.name=Foo'
591591
npm.config.data.set('global', {
592592
source: '/etc/npmrc',
@@ -626,7 +626,7 @@ t.test('config edit --global', t => {
626626
})
627627

628628
t.teardown(() => {
629-
cliConfig.global = false
629+
cliConfig.location = 'user'
630630
npm.config.data.delete('user')
631631
delete npm.config.save
632632
})

test/lib/utils/config/definitions.js

+23
Original file line numberDiff line numberDiff line change
@@ -805,3 +805,26 @@ t.test('save-exact', t => {
805805
t.strictSame(flat, { savePrefix: '~1.2.3' })
806806
t.end()
807807
})
808+
809+
t.test('location', t => {
810+
const obj = {
811+
global: true,
812+
location: 'user',
813+
}
814+
const flat = {}
815+
definitions.location.flatten('location', obj, flat)
816+
// global = true sets location in both places to global
817+
t.strictSame(flat, { location: 'global' })
818+
t.strictSame(obj, { global: true, location: 'global' })
819+
820+
obj.global = false
821+
obj.location = 'user'
822+
delete flat.global
823+
delete flat.location
824+
825+
definitions.location.flatten('location', obj, flat)
826+
// global = false leaves location unaltered
827+
t.strictSame(flat, { location: 'user' })
828+
t.strictSame(obj, { global: false, location: 'user' })
829+
t.end()
830+
})

0 commit comments

Comments
 (0)