From c6b3509075c78e08b27c42b5abde69116c0f6fb4 Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Mon, 22 Jul 2024 08:45:55 -0500 Subject: [PATCH] fix(core): record stats for more commands (#27017) ## Current Behavior Stats are not recorded for `nx connect`, `nx view-logs`, and `nx init`. ## Expected Behavior Stats are recorded for those commands and they get sent to the right URL ## Related Issue(s) Fixes # (cherry picked from commit e1cced3016a5cd3459081649f66d8c002a157767) --- .../command-line/connect/command-object.ts | 8 +++++++ .../connect/connect-to-nx-cloud.ts | 22 ++++++++++++++----- .../nx/src/command-line/connect/view-logs.ts | 1 + .../connect-to-nx-cloud.ts | 15 +++---------- .../nx-cloud/utilities/get-cloud-options.ts | 10 +++++++++ .../nx/src/nx-cloud/utilities/url-shorten.ts | 13 +++-------- packages/nx/src/utils/ab-testing.ts | 5 +++-- 7 files changed, 44 insertions(+), 30 deletions(-) diff --git a/packages/nx/src/command-line/connect/command-object.ts b/packages/nx/src/command-line/connect/command-object.ts index 2d220be37c9f6..6825f83201e5f 100644 --- a/packages/nx/src/command-line/connect/command-object.ts +++ b/packages/nx/src/command-line/connect/command-object.ts @@ -1,5 +1,6 @@ import { CommandModule } from 'yargs'; import { linkToNxDevAndExamples } from '../yargs-utils/documentation'; +import { nxVersion } from '../../utils/versions'; export const yargsConnectCommand: CommandModule = { command: 'connect', @@ -8,6 +9,13 @@ export const yargsConnectCommand: CommandModule = { builder: (yargs) => linkToNxDevAndExamples(yargs, 'connect-to-nx-cloud'), handler: async () => { await (await import('./connect-to-nx-cloud')).connectToNxCloudCommand(); + await ( + await import('../../utils/ab-testing') + ).recordStat({ + command: 'connect', + nxVersion, + useCloud: true, + }); process.exit(0); }, }; diff --git a/packages/nx/src/command-line/connect/connect-to-nx-cloud.ts b/packages/nx/src/command-line/connect/connect-to-nx-cloud.ts index 17f5a5378b558..6547d842129e2 100644 --- a/packages/nx/src/command-line/connect/connect-to-nx-cloud.ts +++ b/packages/nx/src/command-line/connect/connect-to-nx-cloud.ts @@ -88,6 +88,22 @@ export async function connectToNxCloudCommand( return true; } +export async function connectExistingRepoToNxCloudPrompt( + command = 'init', + key: MessageKey = 'setupNxCloud' +): Promise { + const res = await nxCloudPrompt(key).then( + (value: MessageOptionKey) => value === 'yes' + ); + await recordStat({ + command, + nxVersion, + useCloud: res, + meta: messages.codeOfSelectedPromptMessage(key), + }); + return res; +} + export async function connectToNxCloudWithPrompt(command: string) { const setNxCloud = await nxCloudPrompt('setupNxCloud'); const useCloud = @@ -100,12 +116,6 @@ export async function connectToNxCloudWithPrompt(command: string) { }); } -export async function connectExistingRepoToNxCloudPrompt( - key: MessageKey = 'setupNxCloud' -): Promise { - return nxCloudPrompt(key).then((value: MessageOptionKey) => value === 'yes'); -} - async function nxCloudPrompt(key: MessageKey): Promise { const { message, choices, initial, footer, hint } = messages.getPrompt(key); diff --git a/packages/nx/src/command-line/connect/view-logs.ts b/packages/nx/src/command-line/connect/view-logs.ts index 567b4ade7f51c..aa77ec5390232 100644 --- a/packages/nx/src/command-line/connect/view-logs.ts +++ b/packages/nx/src/command-line/connect/view-logs.ts @@ -19,6 +19,7 @@ export async function viewLogs(): Promise { } const setupNxCloud = await connectExistingRepoToNxCloudPrompt( + 'view-logs', 'setupViewLogs' ); if (!setupNxCloud) { diff --git a/packages/nx/src/nx-cloud/generators/connect-to-nx-cloud/connect-to-nx-cloud.ts b/packages/nx/src/nx-cloud/generators/connect-to-nx-cloud/connect-to-nx-cloud.ts index 139a41f1f9e73..7d6c4dd81859b 100644 --- a/packages/nx/src/nx-cloud/generators/connect-to-nx-cloud/connect-to-nx-cloud.ts +++ b/packages/nx/src/nx-cloud/generators/connect-to-nx-cloud/connect-to-nx-cloud.ts @@ -1,5 +1,4 @@ import { execSync } from 'child_process'; -import { URL } from 'node:url'; import { output } from '../../../utils/output'; import { Tree } from '../../../generators/tree'; import { readJson } from '../../../generators/utils/json'; @@ -7,6 +6,7 @@ import { NxJsonConfiguration } from '../../../config/nx-json'; import { readNxJson, updateNxJson } from '../../../generators/utils/nx-json'; import { formatChangedFilesWithPrettierIfAvailable } from '../../../generators/internal-utils/format-changed-files-with-prettier-if-available'; import { repoUsesGithub, shortenedCloudUrl } from '../../utilities/url-shorten'; +import { getCloudUrl } from '../../utilities/get-cloud-options'; import { commitChanges } from '../../../utils/git-utils'; import * as ora from 'ora'; import * as open from 'open'; @@ -31,11 +31,6 @@ function getRootPackageName(tree: Tree): string { } catch (e) {} return packageJson?.name ?? 'my-workspace'; } -function removeTrailingSlash(apiUrl: string) { - return apiUrl[apiUrl.length - 1] === '/' - ? apiUrl.substr(0, apiUrl.length - 1) - : apiUrl; -} function getNxInitDate(): string | null { try { @@ -57,9 +52,7 @@ async function createNxCloudWorkspace( installationSource: string, nxInitDate: string | null ): Promise<{ token: string; url: string }> { - const apiUrl = removeTrailingSlash( - process.env.NX_CLOUD_API || process.env.NRWL_API || `https://cloud.nx.app` - ); + const apiUrl = getCloudUrl(); const response = await require('axios').post( `${apiUrl}/nx-cloud/create-org-and-workspace`, { @@ -212,9 +205,7 @@ export async function connectToNxCloud( silent: schema.hideFormatLogs, }); } - const apiUrl = removeTrailingSlash( - process.env.NX_CLOUD_API || process.env.NRWL_API || `https://cloud.nx.app` - ); + const apiUrl = getCloudUrl(); return async () => await printSuccessMessage( responseFromCreateNxCloudWorkspace?.url ?? apiUrl, diff --git a/packages/nx/src/nx-cloud/utilities/get-cloud-options.ts b/packages/nx/src/nx-cloud/utilities/get-cloud-options.ts index 3aa652c3b6ae4..6bca82e6af38e 100644 --- a/packages/nx/src/nx-cloud/utilities/get-cloud-options.ts +++ b/packages/nx/src/nx-cloud/utilities/get-cloud-options.ts @@ -8,3 +8,13 @@ export function getCloudOptions(): CloudTaskRunnerOptions { // TODO: The default is not always cloud? But it's not handled at the moment return getRunnerOptions('default', nxJson, {}, true); } + +export function getCloudUrl() { + return removeTrailingSlash( + process.env.NX_CLOUD_API || process.env.NRWL_API || `https://cloud.nx.app` + ); +} + +export function removeTrailingSlash(apiUrl: string) { + return apiUrl[apiUrl.length - 1] === '/' ? apiUrl.slice(0, -1) : apiUrl; +} diff --git a/packages/nx/src/nx-cloud/utilities/url-shorten.ts b/packages/nx/src/nx-cloud/utilities/url-shorten.ts index b28ad1ba35d96..e9210169cebaa 100644 --- a/packages/nx/src/nx-cloud/utilities/url-shorten.ts +++ b/packages/nx/src/nx-cloud/utilities/url-shorten.ts @@ -1,5 +1,6 @@ import { logger } from '../../devkit-exports'; import { getGithubSlugOrNull } from '../../utils/git-utils'; +import { getCloudUrl } from './get-cloud-options'; export async function shortenedCloudUrl( installationSource: string, @@ -8,9 +9,7 @@ export async function shortenedCloudUrl( ) { const githubSlug = getGithubSlugOrNull(); - const apiUrl = removeTrailingSlash( - process.env.NX_CLOUD_API || process.env.NRWL_API || `https://cloud.nx.app` - ); + const apiUrl = getCloudUrl(); try { const version = await getNxCloudVersion(apiUrl); @@ -62,9 +61,7 @@ export async function shortenedCloudUrl( export async function repoUsesGithub(github?: boolean) { const githubSlug = getGithubSlugOrNull(); - const apiUrl = removeTrailingSlash( - process.env.NX_CLOUD_API || process.env.NRWL_API || `https://cloud.nx.app` - ); + const apiUrl = getCloudUrl(); const installationSupportsGitHub = await getInstallationSupportsGitHub( apiUrl @@ -78,10 +75,6 @@ export async function repoUsesGithub(github?: boolean) { ); } -export function removeTrailingSlash(apiUrl: string) { - return apiUrl[apiUrl.length - 1] === '/' ? apiUrl.slice(0, -1) : apiUrl; -} - function getSource( installationSource: string ): 'nx-init' | 'nx-connect' | 'create-nx-workspace' | 'other' { diff --git a/packages/nx/src/utils/ab-testing.ts b/packages/nx/src/utils/ab-testing.ts index 3f5d25eba540b..e3f82e551df21 100644 --- a/packages/nx/src/utils/ab-testing.ts +++ b/packages/nx/src/utils/ab-testing.ts @@ -1,6 +1,7 @@ import { execSync } from 'node:child_process'; import { isCI } from './is-ci'; import { getPackageManagerCommand } from './package-manager'; +import { getCloudUrl } from '../nx-cloud/utilities/get-cloud-options'; export type MessageOptionKey = 'yes' | 'skip'; @@ -74,7 +75,7 @@ export async function recordStat(opts: { command: string; nxVersion: string; useCloud: boolean; - meta: string; + meta?: string; }) { try { if (!shouldRecordStats()) { @@ -83,7 +84,7 @@ export async function recordStat(opts: { const axios = require('axios'); await (axios['default'] ?? axios) .create({ - baseURL: 'https://cloud.nx.app', + baseURL: getCloudUrl(), timeout: 400, }) .post('/nx-cloud/stats', {