Skip to content

Commit

Permalink
fix(core): record stats for more commands
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Jul 19, 2024
1 parent 9410164 commit c7ca776
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 30 deletions.
8 changes: 8 additions & 0 deletions packages/nx/src/command-line/connect/command-object.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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);
},
};
Expand Down
22 changes: 16 additions & 6 deletions packages/nx/src/command-line/connect/connect-to-nx-cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ export async function connectToNxCloudCommand(
return true;
}

export async function connectExistingRepoToNxCloudPrompt(
command = 'init',
key: MessageKey = 'setupNxCloud'
): Promise<boolean> {
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 =
Expand All @@ -100,12 +116,6 @@ export async function connectToNxCloudWithPrompt(command: string) {
});
}

export async function connectExistingRepoToNxCloudPrompt(
key: MessageKey = 'setupNxCloud'
): Promise<boolean> {
return nxCloudPrompt(key).then((value: MessageOptionKey) => value === 'yes');
}

async function nxCloudPrompt(key: MessageKey): Promise<MessageOptionKey> {
const { message, choices, initial, footer, hint } = messages.getPrompt(key);

Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/command-line/connect/view-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export async function viewLogs(): Promise<number> {
}

const setupNxCloud = await connectExistingRepoToNxCloudPrompt(
'view-logs',
'setupViewLogs'
);
if (!setupNxCloud) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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';
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';
Expand All @@ -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 {
Expand All @@ -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`,
{
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions packages/nx/src/nx-cloud/utilities/get-cloud-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
13 changes: 3 additions & 10 deletions packages/nx/src/nx-cloud/utilities/url-shorten.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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' {
Expand Down
5 changes: 3 additions & 2 deletions packages/nx/src/utils/ab-testing.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -74,7 +75,7 @@ export async function recordStat(opts: {
command: string;
nxVersion: string;
useCloud: boolean;
meta: string;
meta?: string;
}) {
try {
if (!shouldRecordStats()) {
Expand All @@ -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', {
Expand Down

0 comments on commit c7ca776

Please sign in to comment.