Skip to content

Commit

Permalink
Move user-agent template populating into userAgent flattener
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Mar 16, 2021
1 parent 3122142 commit 300ff77
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 28 deletions.
16 changes: 14 additions & 2 deletions lib/utils/config/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ define('all', {
all outdated or installed packages, rather than only those directly
depended upon by the current project.
`,
flatten,
})

define('allow-same-version', {
Expand Down Expand Up @@ -688,7 +689,7 @@ define('force', {
flatten,
})

define('foreground-script', {
define('foreground-scripts', {
default: false,
type: Boolean,
description: `
Expand Down Expand Up @@ -1326,6 +1327,7 @@ define('parseable', {
Output parseable results from commands that write to standard output. For
\`npm search\`, this will be tab-separated table format.
`,
flatten,
})

define('prefer-offline', {
Expand Down Expand Up @@ -1940,7 +1942,17 @@ define('user-agent', {
* \`{ci}\` - The value of the \`ci-name\` config, if set, prefixed with
\`ci/\`, or an empty string if \`ci-name\` is empty.
`,
flatten,
flatten (key, obj, flatOptions) {
const value = obj[key]
const ciName = obj['ci-name']
flatOptions.userAgent =
value.replace(/\{node-version\}/gi, obj['node-version'])
.replace(/\{npm-version\}/gi, obj['npm-version'])
.replace(/\{platform\}/gi, process.platform)
.replace(/\{arch\}/gi, process.arch)
.replace(/\{ci\}/gi, ciName ? `ci/${ciName}` : '')
.trim()
},
})

define('userconfig', {
Expand Down
13 changes: 0 additions & 13 deletions node_modules/@npmcli/config/lib/get-user-agent.js

This file was deleted.

11 changes: 0 additions & 11 deletions node_modules/@npmcli/config/lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Array [
"fetch-retry-mintimeout",
"fetch-timeout",
"force",
"foreground-script",
"foreground-scripts",
"format-package-lock",
"fund",
"git",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ mistakes, unnecessary performance degradation, and malicious input.
If you don't have a clear idea of what you want to do, it is strongly
recommended that you do not use this option!
#### \`foreground-script\`
#### \`foreground-scripts\`
* Default: false
* Type: Boolean
Expand Down
18 changes: 18 additions & 0 deletions test/lib/utils/config/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,3 +677,21 @@ t.test('detect CI', t => {
t.equal(defnCIFoo['ci-name'].default, 'foo', 'name of CI when in CI')
t.end()
})

t.test('user-agent', t => {
const obj = {
'user-agent': definitions['user-agent'].default,
'npm-version': '1.2.3',
'node-version': '9.8.7',
}
const flat = {}
const expectNoCI = `npm/1.2.3 node/9.8.7 ` +
`${process.platform} ${process.arch}`
definitions['user-agent'].flatten('user-agent', obj, flat)
t.equal(flat.userAgent, expectNoCI)
obj['ci-name'] = 'foo'
const expectCI = `${expectNoCI} ci/foo`
definitions['user-agent'].flatten('user-agent', obj, flat)
t.equal(flat.userAgent, expectCI)
t.end()
})

0 comments on commit 300ff77

Please sign in to comment.