diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index ae29303456..75ba478f10 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -29,7 +29,10 @@ yargs default: 'stylish' as OutputFormat, }, }), - handleStats + (argv) => { + process.env.REDOCLY_CLI_COMMAND = 'stats'; + handleStats(argv); + } ) .command( 'split [api]', @@ -54,7 +57,10 @@ yargs }, }) .demandOption('api'), - handleSplit + (argv) => { + process.env.REDOCLY_CLI_COMMAND = 'split'; + handleSplit(argv); + } ) .command( 'join [apis...]', @@ -89,6 +95,7 @@ yargs }, }), (argv) => { + process.env.REDOCLY_CLI_COMMAND = 'join'; handleJoin(argv, version); } ) @@ -127,7 +134,10 @@ yargs }) .implies('batch-id', 'batch-size') .implies('batch-size', 'batch-id'), - transformPush(handlePush) + (argv) => { + process.env.REDOCLY_CLI_COMMAND = 'push'; + transformPush(handlePush)(argv); + } ) .command( 'lint [apis...]', @@ -183,6 +193,7 @@ yargs }, }), (argv) => { + process.env.REDOCLY_CLI_COMMAND = 'lint'; handleLint(argv, version); } ) @@ -259,6 +270,7 @@ yargs }, }), (argv) => { + process.env.REDOCLY_CLI_COMMAND = 'bundle'; handleBundle(argv, version); } ) @@ -277,13 +289,17 @@ yargs choices: regionChoices, }, }), - handleLogin + (argv) => { + process.env.REDOCLY_CLI_COMMAND = 'login'; + handleLogin(argv); + } ) .command( 'logout', 'Clear your stored credentials for the Redocly API registry.', (yargs) => yargs, async () => { + process.env.REDOCLY_CLI_COMMAND = 'logout'; const client = new RedoclyClient(); client.logout(); process.stdout.write('Logged out from the Redocly account. ✋\n'); @@ -330,7 +346,10 @@ yargs type: 'string', }, }), - previewDocs + (argv) => { + process.env.REDOCLY_CLI_COMMAND = 'preview-docs'; + previewDocs(argv); + } ) .completion('completion', 'Generate completion script.') .demandCommand(1) diff --git a/packages/core/src/redocly/registry-api.ts b/packages/core/src/redocly/registry-api.ts index 5c752ffa39..b9dd00db98 100644 --- a/packages/core/src/redocly/registry-api.ts +++ b/packages/core/src/redocly/registry-api.ts @@ -10,6 +10,9 @@ import { DEFAULT_REGION, DOMAINS } from '../config/config'; import { isNotEmptyObject } from '../utils'; const version = require('../../package.json').version; +export const currentCommand = + typeof process !== 'undefined' ? process.env?.REDOCLY_CLI_COMMAND || '' : ''; + export class RegistryApi { constructor(private accessTokens: AccessTokens, private region: Region) {} @@ -27,7 +30,10 @@ export class RegistryApi { } private async request(path = '', options: RequestInit = {}, region?: Region) { - const headers = Object.assign({}, options.headers || {}, { 'x-redocly-cli-version': version }); + const headers = Object.assign({}, options.headers || {}, { + 'x-redocly-cli-version': version, + 'user-agent': `redocly-cli / ${version} ${currentCommand}`, + }); if (!headers.hasOwnProperty('authorization')) { throw new Error('Unauthorized');