mirror of
https://github.com/withastro/astro.git
synced 2024-12-30 22:03:56 -05:00
feat: Add configuration option to disable CLI telemetry
This commit is contained in:
parent
55e9cd8855
commit
f690a58375
6 changed files with 60 additions and 5 deletions
18
.changeset/rich-rivers-cheer.md
Normal file
18
.changeset/rich-rivers-cheer.md
Normal file
|
@ -0,0 +1,18 @@
|
|||
---
|
||||
'@astrojs/telemetry': minor
|
||||
'astro': minor
|
||||
---
|
||||
|
||||
Adds a configuration option to disable CLI telemetry for the project
|
||||
|
||||
Astro collects optional anonymous telemetry to help us understand how people use the CLI. This change adds a new way to disable telemetry on a per-project basis. Currently you can disable telemetry globally per machine by running `astro telemetry disable` or by setting the environment variable `ASTRO_TELEMETRY_DISABLE`. This change adds a new configuration option to disable telemetry for a specific project, which applies to all users of the project.
|
||||
|
||||
```js
|
||||
// astro.config.mjs
|
||||
import { defineConfig } from 'astro/config';
|
||||
|
||||
export default defineConfig({
|
||||
disableTelemetry: true
|
||||
});
|
||||
|
||||
```
|
|
@ -148,6 +148,11 @@ export async function resolveConfig(
|
|||
|
||||
const userConfig = await loadConfig(root, inlineOnlyConfig.configFile, fsMod);
|
||||
const mergedConfig = mergeConfig(userConfig, inlineUserConfig);
|
||||
|
||||
if (mergedConfig.disableTelemetry) {
|
||||
telemetry.disableForSession();
|
||||
}
|
||||
|
||||
// First-Pass Validation
|
||||
let astroConfig: AstroConfig;
|
||||
try {
|
||||
|
|
|
@ -87,6 +87,7 @@ export const ASTRO_CONFIG_DEFAULTS = {
|
|||
security: {
|
||||
checkOrigin: true,
|
||||
},
|
||||
disableTelemetry: false,
|
||||
env: {
|
||||
schema: {},
|
||||
validateSecrets: false,
|
||||
|
@ -509,6 +510,7 @@ export const AstroConfigSchema = z.object({
|
|||
.strict()
|
||||
.optional()
|
||||
.default(ASTRO_CONFIG_DEFAULTS.env),
|
||||
disableTelemetry: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.disableTelemetry),
|
||||
experimental: z
|
||||
.object({
|
||||
clientPrerender: z
|
||||
|
@ -526,10 +528,7 @@ export const AstroConfigSchema = z.object({
|
|||
.default({}),
|
||||
legacy: z
|
||||
.object({
|
||||
collections: z
|
||||
.boolean()
|
||||
.optional()
|
||||
.default(ASTRO_CONFIG_DEFAULTS.legacy.collections),
|
||||
collections: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.legacy.collections),
|
||||
})
|
||||
.default({}),
|
||||
});
|
||||
|
|
|
@ -899,6 +899,22 @@ export interface AstroUserConfig {
|
|||
defaultStrategy?: 'tap' | 'hover' | 'viewport' | 'load';
|
||||
};
|
||||
|
||||
/**
|
||||
* @docs
|
||||
* @name disableTelemetry
|
||||
* @type {boolean}
|
||||
* @version 5.0.0
|
||||
* @default `false`
|
||||
* @description
|
||||
* Disable anonymous telemetry collection for this project.
|
||||
*
|
||||
* You can also disable telemetry globally by running `astro telemetry disable`.
|
||||
*
|
||||
* Visit https://astro.build/telemetry/ for more information about our approach to anonymous telemetry in Astro.
|
||||
*/
|
||||
|
||||
disableTelemetry?: boolean;
|
||||
|
||||
/**
|
||||
* @docs
|
||||
* @kind heading
|
||||
|
|
|
@ -25,6 +25,7 @@ export class AstroTelemetry {
|
|||
private debug = debug('astro:telemetry');
|
||||
private isCI = isCI;
|
||||
private env = process.env;
|
||||
private disabledForSession = false;
|
||||
|
||||
private get astroVersion() {
|
||||
return this.opts.astroVersion;
|
||||
|
@ -85,7 +86,7 @@ export class AstroTelemetry {
|
|||
}
|
||||
|
||||
private get isDisabled(): boolean {
|
||||
if (Boolean(this.ASTRO_TELEMETRY_DISABLED || this.TELEMETRY_DISABLED)) {
|
||||
if (this.ASTRO_TELEMETRY_DISABLED || this.TELEMETRY_DISABLED || this.disabledForSession) {
|
||||
return true;
|
||||
}
|
||||
return this.enabled === false;
|
||||
|
@ -95,6 +96,11 @@ export class AstroTelemetry {
|
|||
this.config.set(KEY.TELEMETRY_ENABLED, value);
|
||||
}
|
||||
|
||||
disableForSession() {
|
||||
this.debug('[notify] telemetry has been disabled for this session');
|
||||
this.disabledForSession = true;
|
||||
}
|
||||
|
||||
clear() {
|
||||
return this.config.clear();
|
||||
}
|
||||
|
|
|
@ -81,4 +81,15 @@ describe('AstroTelemetry', () => {
|
|||
assert.notEqual(log, undefined);
|
||||
assert.match(logs.join(''), /enabled/);
|
||||
});
|
||||
|
||||
it('respects disabling per session', async () => {
|
||||
const { telemetry, logs } = setup();
|
||||
telemetry.disableForSession();
|
||||
assert.equal(telemetry.isDisabled, true);
|
||||
const result = await telemetry.record(['TEST']);
|
||||
assert.equal(result, undefined);
|
||||
const [log] = logs;
|
||||
assert.notEqual(log, undefined);
|
||||
assert.match(logs.join(''), /disabled/);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue