From 1edb5e01121f44e9fa107710273460e3e1eb08a9 Mon Sep 17 00:00:00 2001 From: Ty Hopp Date: Mon, 17 Jan 2022 17:30:20 +0800 Subject: [PATCH] fix(create-gatsby): Respect telemetry disable (#34495) (cherry picked from commit 44b2ef5905801d1b40a15313966867bd3d410be7) --- .../create-gatsby/src/__tests__/tracking.ts | 61 +++++++++++++++++++ packages/create-gatsby/src/is-truthy.ts | 23 +++++++ packages/create-gatsby/src/tracking.ts | 32 ++++++++++ 3 files changed, 116 insertions(+) create mode 100644 packages/create-gatsby/src/__tests__/tracking.ts create mode 100644 packages/create-gatsby/src/is-truthy.ts diff --git a/packages/create-gatsby/src/__tests__/tracking.ts b/packages/create-gatsby/src/__tests__/tracking.ts new file mode 100644 index 0000000000000..42bd45bb0abcb --- /dev/null +++ b/packages/create-gatsby/src/__tests__/tracking.ts @@ -0,0 +1,61 @@ +let isTrackingEnabled: () => boolean + +const get = jest.fn() +const set = jest.fn() + +jest.doMock(`../get-config-store`, () => { + return { + getConfigStore: (): unknown => { + return { + get, + set, + } + }, + } +}) + +describe(`isTrackingEnabled`, () => { + beforeEach(() => { + jest.resetModules() + isTrackingEnabled = require(`../tracking`).isTrackingEnabled + }) + + it(`is enabled by default`, () => { + const enabled = isTrackingEnabled() + expect(enabled).toBeTrue() + }) + + it(`respects the setting of the config store`, () => { + get.mockImplementationOnce(key => { + if (key === `telemetry.enabled`) { + return false + } else { + return true + } + }) + + const enabled = isTrackingEnabled() + expect(enabled).toBeFalse() + + const cachedEnabled = isTrackingEnabled() + expect(cachedEnabled).toBeFalse() + }) + + describe(`process.env.GATSBY_TELEMETRY_DISABLED`, () => { + beforeAll(() => { + process.env.GATSBY_TELEMETRY_DISABLED = `true` + }) + + it(`respects the setting of the environment variable`, () => { + const enabled = isTrackingEnabled() + expect(enabled).toBeFalse() + + const cachedEnabled = isTrackingEnabled() + expect(cachedEnabled).toBeFalse() + }) + + afterAll(() => { + process.env.GATSBY_TELEMETRY_DISABLED = undefined + }) + }) +}) diff --git a/packages/create-gatsby/src/is-truthy.ts b/packages/create-gatsby/src/is-truthy.ts new file mode 100644 index 0000000000000..25b0c67e77c18 --- /dev/null +++ b/packages/create-gatsby/src/is-truthy.ts @@ -0,0 +1,23 @@ +// Copied from gatsby-core-utils to avoid depending on it, similar to get-config-store +// +// Returns true for `true`, true, positive numbers +// Returns false for `false`, false, 0, negative integers and anything else +export function isTruthy(value: any): boolean { + // Return if Boolean + if (typeof value === `boolean`) return value + + // Return false if null or undefined + if (value === undefined || value === null) return false + + // If the String is true or false + if (value.toLowerCase() === `true`) return true + if (value.toLowerCase() === `false`) return false + + // Now check if it's a number + const number = parseInt(value, 10) + if (isNaN(number)) return false + if (number > 0) return true + + // Default to false + return false +} diff --git a/packages/create-gatsby/src/tracking.ts b/packages/create-gatsby/src/tracking.ts index d0c2d60801895..712119a7935da 100644 --- a/packages/create-gatsby/src/tracking.ts +++ b/packages/create-gatsby/src/tracking.ts @@ -1,11 +1,16 @@ import fetch from "node-fetch" import { v4 as uuidv4 } from "@lukeed/uuid" import { getConfigStore } from "./get-config-store" +import { isTruthy } from "./is-truthy" const store = getConfigStore() const gatsbyCliVersion = require(`../package.json`).version const analyticsApi = process.env.GATSBY_TELEMETRY_API || `https://analytics.gatsbyjs.com/events` +let trackingEnabled: boolean | undefined +const trackingDisabledFromEnvVar: boolean | undefined = isTruthy( + process.env.GATSBY_TELEMETRY_DISABLED +) const getMachineId = (): string => { let machineId = store.get(`telemetry.machineId`) @@ -28,7 +33,34 @@ export interface ITrackCliArgs { const sessionId = uuidv4() +// Adapted from gatsby-telemetry +export function isTrackingEnabled(): boolean { + // Cache the result + if (trackingEnabled !== undefined) { + return trackingEnabled + } + + let enabled = store.get(`telemetry.enabled`) as boolean | null + + if (enabled === undefined || enabled === null) { + enabled = true + store.set(`telemetry.enabled`, enabled) + } + + if (trackingDisabledFromEnvVar) { + enabled = false + } + + trackingEnabled = enabled + + return enabled +} + export const trackCli = (eventType: string, args?: ITrackCliArgs): void => { + if (!isTrackingEnabled()) { + return + } + fetch(analyticsApi, { method: `POST`, headers: {