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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 25 additions & 0 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 11 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

0 commit comments

Comments
 (0)