Skip to content

Commit

Permalink
fix(@astrojs/telemetry): add optional integrations field (#3614)
Browse files Browse the repository at this point in the history
* fix: filter out falsy integration from telemetry

Falsy integrations are now ignored in `@astrojs/telemetry`

This error should no longer occur,
```ts
 error   Cannot read properties of null (reading 'name')
    at file:///workspaces/bundle/node_modules/.pnpm/@astrojs+telemetry@0.1.2/node_modules/@astrojs/telemetry/dist/events/session.js:53:117
    ...
```

* ci: add tests for optional integrations

* ci: add changeset

* fix(@astrojs/telemetry): count number of optional integrations in use

* ci: add test for counting the total number of optional integrations in use

* ci: update changeset

* chore: make the changes @tony-sull sugested

* revert(@astrojs/webapi): mod.d.ts -> a4c78b5: [ci] format

* ci: remove `@astrojs/webapi` patch change

* chore(@astrojs/telemetry): remove totalIntegrations payload field

* fix(@astrojs/telemetry): add optional integrations field

* ci: add changeset
  • Loading branch information
okikio authored Jun 16, 2022
1 parent 493441f commit 9c8a7c0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/four-numbers-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astrojs/telemetry': patch
---

Fix telemetry crashing astro build/dev when using optional integrations

Telemetry will now ignore falsy integration values but will gather a count of how many integrations out of the total are now optional integrations
5 changes: 5 additions & 0 deletions .changeset/funny-terms-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/telemetry': patch
---

Add's optional integrations field to `@astrojs/telemetry`'s payload
13 changes: 9 additions & 4 deletions packages/telemetry/src/events/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ interface EventCliSessionInternal extends EventCliSession {
config?: ConfigInfo;
configKeys?: string[];
flags?: string[];
optionalIntegrations?: number;
}

function getViteVersion() {
Expand Down Expand Up @@ -89,24 +90,26 @@ export function eventCliSession(
userConfig?: AstroUserConfig,
flags?: Record<string, any>
): { eventName: string; payload: EventCliSessionInternal }[] {
// Filter out falsy integrations
const integrations = userConfig?.integrations?.filter?.(Boolean) ?? [];
const configValues = userConfig
? {
markdownPlugins: [
userConfig?.markdown?.remarkPlugins ?? [],
userConfig?.markdown?.rehypePlugins ?? [],
].flat(1),
adapter: userConfig?.adapter?.name ?? null,
integrations: userConfig?.integrations?.map((i: any) => i.name) ?? [],
integrations: integrations?.map?.((i: any) => i?.name) ?? [],
trailingSlash: userConfig?.trailingSlash,
build: userConfig?.build
? {
format: userConfig?.build?.format,
format: userConfig?.build?.format,
}
: undefined,
markdown: userConfig?.markdown
? {
mode: userConfig?.markdown?.mode,
syntaxHighlight: userConfig.markdown?.syntaxHighlight,
mode: userConfig?.markdown?.mode,
syntaxHighlight: userConfig.markdown?.syntaxHighlight,
}
: undefined,
}
Expand All @@ -125,6 +128,8 @@ export function eventCliSession(
// Config Values
config: configValues,
flags: cliFlags,
// Optional integrations
optionalIntegrations: userConfig?.integrations?.length - integrations?.length
};
return [{ eventName: EVENT_SESSION, payload }];
}
42 changes: 42 additions & 0 deletions packages/telemetry/test/session-event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,48 @@ describe('Session event', () => {
});
});

describe('config.integrations + optionalIntegrations', () => {
it('optional/conditional integrations', () => {
const config = {
srcDir: 1,
integrations: [
null,
undefined,
{ name: "example-integration" }
]
};
const [{ payload }] = events.eventCliSession(
{
cliCommand: 'dev',
astroVersion: '0.0.0',
},
config
);
expect(payload.config.integrations).deep.equal(["example-integration"]);
expect(payload.optionalIntegrations).to.equal(2);
});

it('falsy integrations', () => {
const config = {
srcDir: 1,
integrations: [
null,
undefined,
false
]
};
const [{ payload }] = events.eventCliSession(
{
cliCommand: 'dev',
astroVersion: '0.0.0',
},
config
);
expect(payload.config.integrations.length).to.equal(0);
expect(payload.optionalIntegrations).to.equal(3);
});
});

describe('flags', () => {
it('includes cli flags in payload', () => {
const config = {};
Expand Down

0 comments on commit 9c8a7c0

Please sign in to comment.