Skip to content

Commit 300ff77

Browse files
committed
Move user-agent template populating into userAgent flattener
1 parent 3122142 commit 300ff77

File tree

6 files changed

+34
-28
lines changed

6 files changed

+34
-28
lines changed

lib/utils/config/definitions.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ define('all', {
142142
all outdated or installed packages, rather than only those directly
143143
depended upon by the current project.
144144
`,
145+
flatten,
145146
})
146147

147148
define('allow-same-version', {
@@ -688,7 +689,7 @@ define('force', {
688689
flatten,
689690
})
690691

691-
define('foreground-script', {
692+
define('foreground-scripts', {
692693
default: false,
693694
type: Boolean,
694695
description: `
@@ -1326,6 +1327,7 @@ define('parseable', {
13261327
Output parseable results from commands that write to standard output. For
13271328
\`npm search\`, this will be tab-separated table format.
13281329
`,
1330+
flatten,
13291331
})
13301332

13311333
define('prefer-offline', {
@@ -1940,7 +1942,17 @@ define('user-agent', {
19401942
* \`{ci}\` - The value of the \`ci-name\` config, if set, prefixed with
19411943
\`ci/\`, or an empty string if \`ci-name\` is empty.
19421944
`,
1943-
flatten,
1945+
flatten (key, obj, flatOptions) {
1946+
const value = obj[key]
1947+
const ciName = obj['ci-name']
1948+
flatOptions.userAgent =
1949+
value.replace(/\{node-version\}/gi, obj['node-version'])
1950+
.replace(/\{npm-version\}/gi, obj['npm-version'])
1951+
.replace(/\{platform\}/gi, process.platform)
1952+
.replace(/\{arch\}/gi, process.arch)
1953+
.replace(/\{ci\}/gi, ciName ? `ci/${ciName}` : '')
1954+
.trim()
1955+
},
19441956
})
19451957

19461958
define('userconfig', {

node_modules/@npmcli/config/lib/get-user-agent.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

node_modules/@npmcli/config/lib/index.js

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Array [
4949
"fetch-retry-mintimeout",
5050
"fetch-timeout",
5151
"force",
52-
"foreground-script",
52+
"foreground-scripts",
5353
"format-package-lock",
5454
"fund",
5555
"git",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ mistakes, unnecessary performance degradation, and malicious input.
385385
If you don't have a clear idea of what you want to do, it is strongly
386386
recommended that you do not use this option!
387387
388-
#### \`foreground-script\`
388+
#### \`foreground-scripts\`
389389
390390
* Default: false
391391
* Type: Boolean

test/lib/utils/config/definitions.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,3 +677,21 @@ t.test('detect CI', t => {
677677
t.equal(defnCIFoo['ci-name'].default, 'foo', 'name of CI when in CI')
678678
t.end()
679679
})
680+
681+
t.test('user-agent', t => {
682+
const obj = {
683+
'user-agent': definitions['user-agent'].default,
684+
'npm-version': '1.2.3',
685+
'node-version': '9.8.7',
686+
}
687+
const flat = {}
688+
const expectNoCI = `npm/1.2.3 node/9.8.7 ` +
689+
`${process.platform} ${process.arch}`
690+
definitions['user-agent'].flatten('user-agent', obj, flat)
691+
t.equal(flat.userAgent, expectNoCI)
692+
obj['ci-name'] = 'foo'
693+
const expectCI = `${expectNoCI} ci/foo`
694+
definitions['user-agent'].flatten('user-agent', obj, flat)
695+
t.equal(flat.userAgent, expectCI)
696+
t.end()
697+
})

0 commit comments

Comments
 (0)