Skip to content

Commit

Permalink
feat(config): add workspaces boolean to user-agent
Browse files Browse the repository at this point in the history
PR-URL: #3187
Credit: @nlf
Close: #3187
Reviewed-by: @wraithgar
  • Loading branch information
nlf authored and wraithgar committed May 6, 2021
1 parent 2c9b871 commit 59171f0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/utils/config/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1943,6 +1943,7 @@ define('user-agent', {
'node/{node-version} ' +
'{platform} ' +
'{arch} ' +
'workspaces/{workspaces} ' +
'{ci}',
type: String,
description: `
Expand All @@ -1953,17 +1954,23 @@ define('user-agent', {
* \`{node-version}\` - The Node.js version in use
* \`{platform}\` - The value of \`process.platform\`
* \`{arch}\` - The value of \`process.arch\`
* \`{workspaces}\` - Set to \`true\` if the \`workspaces\` or \`workspace\`
options are set.
* \`{ci}\` - The value of the \`ci-name\` config, if set, prefixed with
\`ci/\`, or an empty string if \`ci-name\` is empty.
`,
flatten (key, obj, flatOptions) {
const value = obj[key]
const ciName = obj['ci-name']
let inWorkspaces = false
if (obj.workspaces || obj.workspace && obj.workspace.length)
inWorkspaces = true
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(/\{workspaces\}/gi, inWorkspaces)
.replace(/\{ci\}/gi, ciName ? `ci/${ciName}` : '')
.trim()
// user-agent is a unique kind of config item that gets set from a template
Expand Down
5 changes: 4 additions & 1 deletion tap-snapshots/test/lib/utils/config/describe-all.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,8 @@ Show short usage output about the command specified.
#### \`user-agent\`
* Default: "npm/{npm-version} node/{node-version} {platform} {arch} {ci}"
* Default: "npm/{npm-version} node/{node-version} {platform} {arch}
workspaces/{workspaces} {ci}"
* Type: String
Sets the User-Agent request header. The following fields are replaced with
Expand All @@ -1142,6 +1143,8 @@ their actual counterparts:
* \`{node-version}\` - The Node.js version in use
* \`{platform}\` - The value of \`process.platform\`
* \`{arch}\` - The value of \`process.arch\`
* \`{workspaces}\` - Set to \`true\` if the \`workspaces\` or \`workspace\` options
are set.
* \`{ci}\` - The value of the \`ci-name\` config, if set, prefixed with \`ci/\`, or
an empty string if \`ci-name\` is empty.
Expand Down
19 changes: 18 additions & 1 deletion test/lib/utils/config/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ t.test('user-agent', t => {
}
const flat = {}
const expectNoCI = `npm/1.2.3 node/9.8.7 ` +
`${process.platform} ${process.arch}`
`${process.platform} ${process.arch} workspaces/false`
definitions['user-agent'].flatten('user-agent', obj, flat)
t.equal(flat.userAgent, expectNoCI)
t.equal(process.env.npm_config_user_agent, flat.userAgent, 'npm_user_config environment is set')
Expand All @@ -742,6 +742,23 @@ t.test('user-agent', t => {
t.equal(flat.userAgent, expectCI)
t.equal(process.env.npm_config_user_agent, flat.userAgent, 'npm_user_config environment is set')
t.equal(obj['user-agent'], flat.userAgent, 'config user-agent template is translated')

delete obj['ci-name']
obj.workspaces = true
obj['user-agent'] = definitions['user-agent'].default
const expectWorkspaces = expectNoCI.replace('workspaces/false', 'workspaces/true')
definitions['user-agent'].flatten('user-agent', obj, flat)
t.equal(flat.userAgent, expectWorkspaces)
t.equal(process.env.npm_config_user_agent, flat.userAgent, 'npm_user_config environment is set')
t.equal(obj['user-agent'], flat.userAgent, 'config user-agent template is translated')

delete obj.workspaces
obj.workspace = ['foo']
obj['user-agent'] = definitions['user-agent'].default
definitions['user-agent'].flatten('user-agent', obj, flat)
t.equal(flat.userAgent, expectWorkspaces)
t.equal(process.env.npm_config_user_agent, flat.userAgent, 'npm_user_config environment is set')
t.equal(obj['user-agent'], flat.userAgent, 'config user-agent template is translated')
t.end()
})

Expand Down

0 comments on commit 59171f0

Please sign in to comment.