Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add workspaces boolean to user-agent #3187

Merged
merged 1 commit into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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