Skip to content

Commit 59171f0

Browse files
nlfwraithgar
authored andcommitted
feat(config): add workspaces boolean to user-agent
PR-URL: #3187 Credit: @nlf Close: #3187 Reviewed-by: @wraithgar
1 parent 2c9b871 commit 59171f0

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

lib/utils/config/definitions.js

+7
Original file line numberDiff line numberDiff line change
@@ -1943,6 +1943,7 @@ define('user-agent', {
19431943
'node/{node-version} ' +
19441944
'{platform} ' +
19451945
'{arch} ' +
1946+
'workspaces/{workspaces} ' +
19461947
'{ci}',
19471948
type: String,
19481949
description: `
@@ -1953,17 +1954,23 @@ define('user-agent', {
19531954
* \`{node-version}\` - The Node.js version in use
19541955
* \`{platform}\` - The value of \`process.platform\`
19551956
* \`{arch}\` - The value of \`process.arch\`
1957+
* \`{workspaces}\` - Set to \`true\` if the \`workspaces\` or \`workspace\`
1958+
options are set.
19561959
* \`{ci}\` - The value of the \`ci-name\` config, if set, prefixed with
19571960
\`ci/\`, or an empty string if \`ci-name\` is empty.
19581961
`,
19591962
flatten (key, obj, flatOptions) {
19601963
const value = obj[key]
19611964
const ciName = obj['ci-name']
1965+
let inWorkspaces = false
1966+
if (obj.workspaces || obj.workspace && obj.workspace.length)
1967+
inWorkspaces = true
19621968
flatOptions.userAgent =
19631969
value.replace(/\{node-version\}/gi, obj['node-version'])
19641970
.replace(/\{npm-version\}/gi, obj['npm-version'])
19651971
.replace(/\{platform\}/gi, process.platform)
19661972
.replace(/\{arch\}/gi, process.arch)
1973+
.replace(/\{workspaces\}/gi, inWorkspaces)
19671974
.replace(/\{ci\}/gi, ciName ? `ci/${ciName}` : '')
19681975
.trim()
19691976
// user-agent is a unique kind of config item that gets set from a template

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,8 @@ Show short usage output about the command specified.
11321132
11331133
#### \`user-agent\`
11341134
1135-
* Default: "npm/{npm-version} node/{node-version} {platform} {arch} {ci}"
1135+
* Default: "npm/{npm-version} node/{node-version} {platform} {arch}
1136+
workspaces/{workspaces} {ci}"
11361137
* Type: String
11371138
11381139
Sets the User-Agent request header. The following fields are replaced with
@@ -1142,6 +1143,8 @@ their actual counterparts:
11421143
* \`{node-version}\` - The Node.js version in use
11431144
* \`{platform}\` - The value of \`process.platform\`
11441145
* \`{arch}\` - The value of \`process.arch\`
1146+
* \`{workspaces}\` - Set to \`true\` if the \`workspaces\` or \`workspace\` options
1147+
are set.
11451148
* \`{ci}\` - The value of the \`ci-name\` config, if set, prefixed with \`ci/\`, or
11461149
an empty string if \`ci-name\` is empty.
11471150

test/lib/utils/config/definitions.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ t.test('user-agent', t => {
729729
}
730730
const flat = {}
731731
const expectNoCI = `npm/1.2.3 node/9.8.7 ` +
732-
`${process.platform} ${process.arch}`
732+
`${process.platform} ${process.arch} workspaces/false`
733733
definitions['user-agent'].flatten('user-agent', obj, flat)
734734
t.equal(flat.userAgent, expectNoCI)
735735
t.equal(process.env.npm_config_user_agent, flat.userAgent, 'npm_user_config environment is set')
@@ -742,6 +742,23 @@ t.test('user-agent', t => {
742742
t.equal(flat.userAgent, expectCI)
743743
t.equal(process.env.npm_config_user_agent, flat.userAgent, 'npm_user_config environment is set')
744744
t.equal(obj['user-agent'], flat.userAgent, 'config user-agent template is translated')
745+
746+
delete obj['ci-name']
747+
obj.workspaces = true
748+
obj['user-agent'] = definitions['user-agent'].default
749+
const expectWorkspaces = expectNoCI.replace('workspaces/false', 'workspaces/true')
750+
definitions['user-agent'].flatten('user-agent', obj, flat)
751+
t.equal(flat.userAgent, expectWorkspaces)
752+
t.equal(process.env.npm_config_user_agent, flat.userAgent, 'npm_user_config environment is set')
753+
t.equal(obj['user-agent'], flat.userAgent, 'config user-agent template is translated')
754+
755+
delete obj.workspaces
756+
obj.workspace = ['foo']
757+
obj['user-agent'] = definitions['user-agent'].default
758+
definitions['user-agent'].flatten('user-agent', obj, flat)
759+
t.equal(flat.userAgent, expectWorkspaces)
760+
t.equal(process.env.npm_config_user_agent, flat.userAgent, 'npm_user_config environment is set')
761+
t.equal(obj['user-agent'], flat.userAgent, 'config user-agent template is translated')
745762
t.end()
746763
})
747764

0 commit comments

Comments
 (0)