diff options
Diffstat (limited to 'packages/telemetry/src')
-rw-r--r-- | packages/telemetry/src/index.ts | 32 | ||||
-rw-r--r-- | packages/telemetry/src/project-info.ts | 32 |
2 files changed, 43 insertions, 21 deletions
diff --git a/packages/telemetry/src/index.ts b/packages/telemetry/src/index.ts index 0bba53e5a..583d7cae2 100644 --- a/packages/telemetry/src/index.ts +++ b/packages/telemetry/src/index.ts @@ -9,15 +9,12 @@ import { getSystemInfo, SystemInfo } from './system-info.js'; export type AstroTelemetryOptions = { astroVersion: string; viteVersion: string }; export type TelemetryEvent = { eventName: string; payload: Record<string, any> }; -interface EventContext { + +interface EventMeta extends SystemInfo {} +interface EventContext extends ProjectInfo { anonymousId: string; - anonymousProjectId: string; anonymousSessionId: string; } - -interface EventMeta extends SystemInfo { - isGit: boolean; -} export class AstroTelemetry { private _anonymousSessionId: string | undefined; private _anonymousProjectInfo: ProjectInfo | undefined; @@ -118,29 +115,19 @@ export class AstroTelemetry { return Promise.resolve(); } - if (this.debug.enabled) { - // Print to standard error to simplify selecting the output - events.forEach(({ eventName, payload }) => - this.debug(JSON.stringify({ eventName, payload }, null, 2)) - ); - // Do not send the telemetry data if debugging. Users may use this feature - // to preview what data would be sent. - return Promise.resolve(); - } - // Skip recording telemetry if the feature is disabled if (this.isDisabled) { + this.debug('telemetry disabled'); return Promise.resolve(); } const meta: EventMeta = { ...getSystemInfo({ astroVersion: this.astroVersion, viteVersion: this.viteVersion }), - isGit: this.anonymousProjectInfo.isGit, }; const context: EventContext = { + ...this.anonymousProjectInfo, anonymousId: this.anonymousId, - anonymousProjectId: this.anonymousProjectInfo.anonymousProjectId, anonymousSessionId: this.anonymousSessionId, }; @@ -150,6 +137,15 @@ export class AstroTelemetry { context.anonymousId = `CI.${meta.ciName || 'UNKNOWN'}`; } + if (this.debug.enabled) { + // Print to standard error to simplify selecting the output + this.debug({ context, meta }); + this.debug(JSON.stringify(events, null, 2)); + // Do not send the telemetry data if debugging. Users may use this feature + // to preview what data would be sent. + return Promise.resolve(); + } + return post({ context, meta, diff --git a/packages/telemetry/src/project-info.ts b/packages/telemetry/src/project-info.ts index 7666a8145..5f95d1b4b 100644 --- a/packages/telemetry/src/project-info.ts +++ b/packages/telemetry/src/project-info.ts @@ -1,6 +1,7 @@ import { execSync } from 'child_process'; import type { BinaryLike } from 'node:crypto'; import { createHash } from 'node:crypto'; +import detectPackageManager from 'which-pm-runs'; /** * Astro Telemetry -- Project Info @@ -46,9 +47,13 @@ import { createHash } from 'node:crypto'; export interface ProjectInfo { /* Your unique project identifier. This will be hashed again before sending. */ - anonymousProjectId: string; + anonymousProjectId: string | undefined; /* true if your project is connected to a git repository. false otherwise. */ isGit: boolean; + /* The package manager used to run Astro */ + packageManager: string | undefined; + /* The version of the package manager used to run Astro */ + packageManagerVersion: string | undefined; } function createAnonymousValue(payload: BinaryLike): string { @@ -75,7 +80,7 @@ function getProjectIdFromGit(): string | null { } } -export function getProjectInfo(isCI: boolean): ProjectInfo { +function getProjectId(isCI: boolean): Pick<ProjectInfo, 'isGit' | 'anonymousProjectId'> { const projectIdFromGit = getProjectIdFromGit(); if (projectIdFromGit) { return { @@ -83,8 +88,29 @@ export function getProjectInfo(isCI: boolean): ProjectInfo { anonymousProjectId: createAnonymousValue(projectIdFromGit), }; } + // If we're running in CI, the current working directory is not unique. + // If the cwd is a single level deep (ex: '/app'), it's probably not unique. + const cwd = process.cwd(); + const isCwdGeneric = (cwd.match(/[\/|\\]/g) || []).length === 1; + if (isCI || isCwdGeneric) { + return { + isGit: false, + anonymousProjectId: undefined, + }; + } return { isGit: false, - anonymousProjectId: isCI ? '' : createAnonymousValue(process.cwd()), + anonymousProjectId: createAnonymousValue(cwd), + }; +} + +export function getProjectInfo(isCI: boolean): ProjectInfo { + const projectId = getProjectId(isCI); + const packageManager = detectPackageManager(); + return { + ...projectId, + packageManager: packageManager?.name, + packageManagerVersion: packageManager?.version, }; } +// |