Skip to content

Commit 6c720eb

Browse files
leosvelperezFrozenPandaz
authored andcommitted
cleanup(misc): improve check for whether stats should be recorded (#23234)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> ## Current Behavior <!-- This is the behavior we have today --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes # (cherry picked from commit fd71b6b)
1 parent 759becf commit 6c720eb

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

packages/create-nx-workspace/src/utils/nx/ab-testing.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { execSync } from 'node:child_process';
12
import { isCI } from '../ci/is-ci';
3+
import { getPackageManagerCommand } from '../package-manager';
24

35
export const NxCloudChoices = ['yes', 'github', 'circleci', 'skip'];
46

@@ -93,11 +95,9 @@ export async function recordStat(opts: {
9395
meta: string[];
9496
}) {
9597
try {
96-
const major = Number(opts.nxVersion.split('.')[0]);
97-
if (process.env.NX_VERBOSE_LOGGING === 'true') {
98-
console.log(`Record stat. Major: ${major}`);
98+
if (!shouldRecordStats()) {
99+
return;
99100
}
100-
if (major < 10 || major > 19) return; // test version, skip it
101101
const axios = require('axios');
102102
await (axios['default'] ?? axios)
103103
.create({
@@ -116,3 +116,17 @@ export async function recordStat(opts: {
116116
}
117117
}
118118
}
119+
120+
function shouldRecordStats(): boolean {
121+
const pmc = getPackageManagerCommand();
122+
try {
123+
const stdout = execSync(pmc.getRegistryUrl, { encoding: 'utf-8' });
124+
const url = new URL(stdout.trim());
125+
126+
// don't record stats when testing locally
127+
return url.hostname !== 'localhost';
128+
} catch {
129+
// fallback to true if we can't detect the registry
130+
return true;
131+
}
132+
}

packages/create-nx-workspace/src/utils/package-manager.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export function getPackageManagerCommand(
3838
exec: string;
3939
preInstall?: string;
4040
globalAdd: string;
41+
getRegistryUrl: string;
4142
} {
4243
const pmVersion = getPackageManagerVersion(packageManager);
4344
const [pmMajor, pmMinor] = pmVersion.split('.');
@@ -54,6 +55,9 @@ export function getPackageManagerCommand(
5455
// using npx is necessary to avoid yarn classic manipulating the version detection when using berry
5556
exec: useBerry ? 'npx' : 'yarn',
5657
globalAdd: 'yarn global add',
58+
getRegistryUrl: useBerry
59+
? 'yarn config get npmRegistryServer'
60+
: 'yarn config get registry',
5761
};
5862

5963
case 'pnpm':
@@ -65,13 +69,15 @@ export function getPackageManagerCommand(
6569
install: 'pnpm install --no-frozen-lockfile --silent --ignore-scripts',
6670
exec: useExec ? 'pnpm exec' : 'pnpx',
6771
globalAdd: 'pnpm add -g',
72+
getRegistryUrl: 'pnpm config get registry',
6873
};
6974

7075
case 'npm':
7176
return {
7277
install: 'npm install --silent --ignore-scripts',
7378
exec: 'npx',
7479
globalAdd: 'npm i -g',
80+
getRegistryUrl: 'npm config get registry',
7581
};
7682
}
7783
}

packages/nx/src/utils/ab-testing.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { execSync } from 'node:child_process';
12
import { isCI } from './is-ci';
3+
import { getPackageManagerCommand } from './package-manager';
24

35
export type MessageOptionKey = 'yes' | 'skip';
46

@@ -75,11 +77,9 @@ export async function recordStat(opts: {
7577
meta: string;
7678
}) {
7779
try {
78-
const major = Number(opts.nxVersion.split('.')[0]);
79-
if (process.env.NX_VERBOSE_LOGGING === 'true') {
80-
console.log(`Record stat. Major: ${major}`);
80+
if (!shouldRecordStats()) {
81+
return;
8182
}
82-
if (major < 10 || major > 19) return; // test version, skip it
8383
const axios = require('axios');
8484
await (axios['default'] ?? axios)
8585
.create({
@@ -98,3 +98,17 @@ export async function recordStat(opts: {
9898
}
9999
}
100100
}
101+
102+
function shouldRecordStats(): boolean {
103+
const pmc = getPackageManagerCommand();
104+
try {
105+
const stdout = execSync(pmc.getRegistryUrl, { encoding: 'utf-8' });
106+
const url = new URL(stdout.trim());
107+
108+
// don't record stats when testing locally
109+
return url.hostname !== 'localhost';
110+
} catch {
111+
// fallback to true if we can't detect the registry
112+
return true;
113+
}
114+
}

packages/nx/src/utils/package-manager.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface PackageManagerCommands {
2727
dlx: string;
2828
list: string;
2929
run: (script: string, args: string) => string;
30+
getRegistryUrl: string;
3031
}
3132

3233
/**
@@ -101,6 +102,9 @@ export function getPackageManagerCommand(
101102
dlx: useBerry ? 'yarn dlx' : 'yarn',
102103
run: (script: string, args: string) => `yarn ${script} ${args}`,
103104
list: useBerry ? 'yarn info --name-only' : 'yarn list',
105+
getRegistryUrl: useBerry
106+
? 'yarn config get npmRegistryServer'
107+
: 'yarn config get registry',
104108
};
105109
},
106110
pnpm: () => {
@@ -123,6 +127,7 @@ export function getPackageManagerCommand(
123127
? `pnpm run ${script} -- ${args}`
124128
: `pnpm run ${script} ${args}`,
125129
list: 'pnpm ls --depth 100',
130+
getRegistryUrl: 'pnpm config get registry',
126131
};
127132
},
128133
npm: () => {
@@ -140,6 +145,7 @@ export function getPackageManagerCommand(
140145
dlx: 'npx',
141146
run: (script: string, args: string) => `npm run ${script} -- ${args}`,
142147
list: 'npm ls',
148+
getRegistryUrl: 'npm config get registry',
143149
};
144150
},
145151
};

0 commit comments

Comments
 (0)