From 6c720ebff0f17e408ba062d608605e4f4d1359d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Fri, 10 May 2024 17:22:06 +0200 Subject: [PATCH] cleanup(misc): improve check for whether stats should be recorded (#23234) ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes # (cherry picked from commit fd71b6bcab9e3ccfb20b2682902cdc8da9fe5c66) --- .../src/utils/nx/ab-testing.ts | 22 +++++++++++++++---- .../src/utils/package-manager.ts | 6 +++++ packages/nx/src/utils/ab-testing.ts | 22 +++++++++++++++---- packages/nx/src/utils/package-manager.ts | 6 +++++ 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/packages/create-nx-workspace/src/utils/nx/ab-testing.ts b/packages/create-nx-workspace/src/utils/nx/ab-testing.ts index 754b1242b9c4e..a5f06fdd242b0 100644 --- a/packages/create-nx-workspace/src/utils/nx/ab-testing.ts +++ b/packages/create-nx-workspace/src/utils/nx/ab-testing.ts @@ -1,4 +1,6 @@ +import { execSync } from 'node:child_process'; import { isCI } from '../ci/is-ci'; +import { getPackageManagerCommand } from '../package-manager'; export const NxCloudChoices = ['yes', 'github', 'circleci', 'skip']; @@ -93,11 +95,9 @@ export async function recordStat(opts: { meta: string[]; }) { try { - const major = Number(opts.nxVersion.split('.')[0]); - if (process.env.NX_VERBOSE_LOGGING === 'true') { - console.log(`Record stat. Major: ${major}`); + if (!shouldRecordStats()) { + return; } - if (major < 10 || major > 19) return; // test version, skip it const axios = require('axios'); await (axios['default'] ?? axios) .create({ @@ -116,3 +116,17 @@ export async function recordStat(opts: { } } } + +function shouldRecordStats(): boolean { + const pmc = getPackageManagerCommand(); + try { + const stdout = execSync(pmc.getRegistryUrl, { encoding: 'utf-8' }); + const url = new URL(stdout.trim()); + + // don't record stats when testing locally + return url.hostname !== 'localhost'; + } catch { + // fallback to true if we can't detect the registry + return true; + } +} diff --git a/packages/create-nx-workspace/src/utils/package-manager.ts b/packages/create-nx-workspace/src/utils/package-manager.ts index 5966089e91b5a..6aae339376c32 100644 --- a/packages/create-nx-workspace/src/utils/package-manager.ts +++ b/packages/create-nx-workspace/src/utils/package-manager.ts @@ -38,6 +38,7 @@ export function getPackageManagerCommand( exec: string; preInstall?: string; globalAdd: string; + getRegistryUrl: string; } { const pmVersion = getPackageManagerVersion(packageManager); const [pmMajor, pmMinor] = pmVersion.split('.'); @@ -54,6 +55,9 @@ export function getPackageManagerCommand( // using npx is necessary to avoid yarn classic manipulating the version detection when using berry exec: useBerry ? 'npx' : 'yarn', globalAdd: 'yarn global add', + getRegistryUrl: useBerry + ? 'yarn config get npmRegistryServer' + : 'yarn config get registry', }; case 'pnpm': @@ -65,6 +69,7 @@ export function getPackageManagerCommand( install: 'pnpm install --no-frozen-lockfile --silent --ignore-scripts', exec: useExec ? 'pnpm exec' : 'pnpx', globalAdd: 'pnpm add -g', + getRegistryUrl: 'pnpm config get registry', }; case 'npm': @@ -72,6 +77,7 @@ export function getPackageManagerCommand( install: 'npm install --silent --ignore-scripts', exec: 'npx', globalAdd: 'npm i -g', + getRegistryUrl: 'npm config get registry', }; } } diff --git a/packages/nx/src/utils/ab-testing.ts b/packages/nx/src/utils/ab-testing.ts index 416f299206888..09ea28ff7d308 100644 --- a/packages/nx/src/utils/ab-testing.ts +++ b/packages/nx/src/utils/ab-testing.ts @@ -1,4 +1,6 @@ +import { execSync } from 'node:child_process'; import { isCI } from './is-ci'; +import { getPackageManagerCommand } from './package-manager'; export type MessageOptionKey = 'yes' | 'skip'; @@ -75,11 +77,9 @@ export async function recordStat(opts: { meta: string; }) { try { - const major = Number(opts.nxVersion.split('.')[0]); - if (process.env.NX_VERBOSE_LOGGING === 'true') { - console.log(`Record stat. Major: ${major}`); + if (!shouldRecordStats()) { + return; } - if (major < 10 || major > 19) return; // test version, skip it const axios = require('axios'); await (axios['default'] ?? axios) .create({ @@ -98,3 +98,17 @@ export async function recordStat(opts: { } } } + +function shouldRecordStats(): boolean { + const pmc = getPackageManagerCommand(); + try { + const stdout = execSync(pmc.getRegistryUrl, { encoding: 'utf-8' }); + const url = new URL(stdout.trim()); + + // don't record stats when testing locally + return url.hostname !== 'localhost'; + } catch { + // fallback to true if we can't detect the registry + return true; + } +} diff --git a/packages/nx/src/utils/package-manager.ts b/packages/nx/src/utils/package-manager.ts index eebfc8d33fedf..92fb698d7b730 100644 --- a/packages/nx/src/utils/package-manager.ts +++ b/packages/nx/src/utils/package-manager.ts @@ -27,6 +27,7 @@ export interface PackageManagerCommands { dlx: string; list: string; run: (script: string, args: string) => string; + getRegistryUrl: string; } /** @@ -101,6 +102,9 @@ export function getPackageManagerCommand( dlx: useBerry ? 'yarn dlx' : 'yarn', run: (script: string, args: string) => `yarn ${script} ${args}`, list: useBerry ? 'yarn info --name-only' : 'yarn list', + getRegistryUrl: useBerry + ? 'yarn config get npmRegistryServer' + : 'yarn config get registry', }; }, pnpm: () => { @@ -123,6 +127,7 @@ export function getPackageManagerCommand( ? `pnpm run ${script} -- ${args}` : `pnpm run ${script} ${args}`, list: 'pnpm ls --depth 100', + getRegistryUrl: 'pnpm config get registry', }; }, npm: () => { @@ -140,6 +145,7 @@ export function getPackageManagerCommand( dlx: 'npx', run: (script: string, args: string) => `npm run ${script} -- ${args}`, list: 'npm ls', + getRegistryUrl: 'npm config get registry', }; }, };