Skip to content

Commit

Permalink
feat(cli): improve message shown when DO_NOT_TRACK is used
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 authored and bjoerge committed Dec 12, 2023
1 parent 6086b96 commit 5d7901d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
22 changes: 15 additions & 7 deletions packages/@sanity/cli/src/actions/telemetry/setTelemetryConsent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {ClientError, ServerError} from '@sanity/client'
import {type CliCommandAction} from '../../types'
import {debug} from '../../debug'
import {getUserConfig} from '../../util/getUserConfig'
import {TELEMETRY_CONSENT_CONFIG_KEY, resolveConsent} from '../../util/createTelemetryStore'
import {
ConsentInformation,
TELEMETRY_CONSENT_CONFIG_KEY,
resolveConsent,
} from '../../util/createTelemetryStore'
import {
telemetryLearnMoreMessage,
telemetryStatusMessage,
Expand All @@ -25,7 +29,7 @@ type Mock = () => Promise<SetConsentResponse<'telemetry'>>
interface ResultMessage {
success: () => string
failure: (message?: string) => string
unchanged: () => string
unchanged: (consentInformation: ConsentInformation) => string
}

const resultMessages: Record<SettableConsentStatus, ResultMessage> = {
Expand All @@ -42,8 +46,12 @@ const resultMessages: Record<SettableConsentStatus, ResultMessage> = {
denied: {
success: () =>
`You've opted out of telemetry data collection.\nNo data will be collected from your Sanity account.`,
unchanged: () =>
`You've already opted out of telemetry data collection.\nNo data is collected from your Sanity account.`,
unchanged: ({reason}) => {
if (reason === 'localOverride') {
return `You've already opted out of telemetry data collection.\nNo data is collected from your machine.\n\nUsing DO_NOT_TRACK environment variable.`
}
return `You've already opted out of telemetry data collection.\nNo data is collected from your Sanity account.`
},
failure: () => 'Failed to disable telemetry',
},
}
Expand Down Expand Up @@ -124,13 +132,13 @@ export function createSetTelemetryConsentAction(status: SettableConsentStatus):
const mock = getMock()

// eslint-disable-next-line no-process-env
const {status: currentStatus} = await resolveConsent({env: process.env})
const isChanged = currentStatus !== status
const currentInformation = await resolveConsent({env: process.env})
const isChanged = currentInformation.status !== status

if (!isChanged) {
debug('Telemetry consent is already "%s"', status)
output.print(`${telemetryStatusMessage(status, context)}\n`)
output.print(resultMessages[status].unchanged())
output.print(resultMessages[status].unchanged(currentInformation))
}

if (isChanged) {
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/cli/src/util/createTelemetryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ interface Options {
env: Env
}

type ConsentInformation =
export type ConsentInformation =
| {
status: Extract<ConsentStatus, 'granted'>
reason?: never
Expand Down
4 changes: 3 additions & 1 deletion packages/@sanity/cli/test/telemetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ https://sanity.io/telemetry
"Status: Disabled
You've already opted out of telemetry data collection.
No data is collected from your Sanity account.
No data is collected from your machine.
Using DO_NOT_TRACK environment variable.
Learn more here:
https://sanity.io/telemetry
Expand Down

0 comments on commit 5d7901d

Please sign in to comment.