summaryrefslogtreecommitdiff
path: root/packages/telemetry
diff options
context:
space:
mode:
Diffstat (limited to 'packages/telemetry')
-rw-r--r--packages/telemetry/src/events/session.ts98
-rw-r--r--packages/telemetry/test/session-event.test.js588
2 files changed, 441 insertions, 245 deletions
diff --git a/packages/telemetry/src/events/session.ts b/packages/telemetry/src/events/session.ts
index ee1daca89..abe7704a9 100644
--- a/packages/telemetry/src/events/session.ts
+++ b/packages/telemetry/src/events/session.ts
@@ -6,25 +6,34 @@ const require = createRequire(import.meta.url);
const EVENT_SESSION = 'ASTRO_CLI_SESSION_STARTED';
+// :( We can't import the type because of TurboRepo circular dep limitation
+type AstroUserConfig = Record<string, any>;
+
interface EventCliSession {
astroVersion: string;
cliCommand: string;
}
interface ConfigInfo {
- hasViteConfig: boolean;
- hasBase: boolean;
- viteKeys: string[];
markdownPlugins: string[];
adapter: string | null;
integrations: string[];
- experimentalFeatures: string[];
+ trailingSlash: undefined | 'always' | 'never' | 'ignore';
+ build: undefined | {
+ format: undefined | 'file' | 'directory'
+ };
+ markdown: undefined | {
+ mode: undefined | 'md' | 'mdx';
+ syntaxHighlight: undefined | 'shiki' | 'prism' | false;
+ };
}
interface EventCliSessionInternal extends EventCliSession {
nodeVersion: string;
viteVersion: string;
config?: ConfigInfo;
+ configKeys?: string[];
+ flags?: string[];
}
function getViteVersion() {
@@ -35,29 +44,24 @@ function getViteVersion() {
return undefined;
}
-function getExperimentalFeatures(astroConfig?: Record<string, any>): string[] | undefined {
- if (!astroConfig) return undefined;
- return Object.entries(astroConfig.experimental || []).reduce((acc, [key, value]) => {
- if (value) {
- acc.push(key);
- }
- return acc;
- }, [] as string[]);
-}
-
-const secondLevelViteKeys = new Set([
- 'resolve',
- 'css',
- 'json',
- 'server',
- 'server.fs',
+const multiLevelKeys = new Set([
'build',
- 'preview',
- 'optimizeDeps',
- 'ssr',
- 'worker',
+ 'markdown',
+ 'markdown.shikiConfig',
+ 'server',
+ 'vite',
+ 'vite.resolve',
+ 'vite.css',
+ 'vite.json',
+ 'vite.server',
+ 'vite.server.fs',
+ 'vite.build',
+ 'vite.preview',
+ 'vite.optimizeDeps',
+ 'vite.ssr',
+ 'vite.worker',
]);
-function viteConfigKeys(obj: Record<string, any> | undefined, parentKey: string): string[] {
+function configKeys(obj: Record<string, any> | undefined, parentKey: string): string[] {
if (!obj) {
return [];
}
@@ -66,8 +70,8 @@ function viteConfigKeys(obj: Record<string, any> | undefined, parentKey: string)
.map(([key, value]) => {
if (typeof value === 'object' && !Array.isArray(value)) {
const localKey = parentKey ? parentKey + '.' + key : key;
- if (secondLevelViteKeys.has(localKey)) {
- let keys = viteConfigKeys(value, localKey).map((subkey) => key + '.' + subkey);
+ if (multiLevelKeys.has(localKey)) {
+ let keys = configKeys(value, localKey).map((subkey) => key + '.' + subkey);
keys.unshift(key);
return keys;
}
@@ -80,29 +84,39 @@ function viteConfigKeys(obj: Record<string, any> | undefined, parentKey: string)
export function eventCliSession(
event: EventCliSession,
- astroConfig?: Record<string, any>
+ userConfig?: AstroUserConfig,
+ flags?: Record<string, any>,
): { eventName: string; payload: EventCliSessionInternal }[] {
+ 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) ?? [],
+ trailingSlash: userConfig?.trailingSlash,
+ build: userConfig?.build ? {
+ format: userConfig?.build?.format
+ } : undefined,
+ markdown: userConfig?.markdown ? {
+ mode: userConfig?.markdown?.mode,
+ syntaxHighlight: userConfig.markdown?.syntaxHighlight
+ } : undefined,
+ } : undefined;
+
+ // Filter out yargs default `_` flag which is the cli command
+ const cliFlags = flags ? Object.keys(flags).filter(name => name != '_'): undefined;
+
const payload: EventCliSessionInternal = {
cliCommand: event.cliCommand,
// Versions
astroVersion: event.astroVersion,
viteVersion: getViteVersion(),
nodeVersion: process.version.replace(/^v?/, ''),
+ configKeys: userConfig ? configKeys(userConfig, '') : undefined,
// Config Values
- config: astroConfig
- ? {
- hasViteConfig: Object.keys(astroConfig?.vite).length > 0,
- markdownPlugins: [
- astroConfig?.markdown?.remarkPlugins ?? [],
- astroConfig?.markdown?.rehypePlugins ?? [],
- ].flat(1),
- hasBase: astroConfig?.base !== '/',
- viteKeys: viteConfigKeys(astroConfig?.vite, ''),
- adapter: astroConfig?.adapter?.name ?? null,
- integrations: astroConfig?.integrations?.map((i: any) => i.name) ?? [],
- experimentalFeatures: getExperimentalFeatures(astroConfig) ?? [],
- }
- : undefined,
+ config: configValues,
+ flags: cliFlags,
};
return [{ eventName: EVENT_SESSION, payload }];
}
diff --git a/packages/telemetry/test/session-event.test.js b/packages/telemetry/test/session-event.test.js
index 354ed2878..c3196787f 100644
--- a/packages/telemetry/test/session-event.test.js
+++ b/packages/telemetry/test/session-event.test.js
@@ -1,241 +1,423 @@
import { expect } from 'chai';
import * as events from '../dist/events/index.js';
-import { resolveConfig } from '../../astro/dist/core/config.js';
-
-async function mockConfig(userConfig) {
- return await resolveConfig(userConfig, import.meta.url, {}, 'dev');
-}
describe('Session event', () => {
- it('top-level keys are captured', async () => {
- const config = await mockConfig({
- vite: {
- css: { modules: [] },
- base: 'a',
- mode: 'b',
- define: {
- a: 'b',
- },
- publicDir: 'some/dir',
- },
+ describe('top-level', () => {
+ it('All top-level keys added', () => {
+ const config = {
+ root: 1,
+ srcDir: 2,
+ publicDir: 3,
+ outDir: 4,
+ site: 5,
+ base: 6,
+ trailingSlash: 7,
+ experimental: 8,
+ };
+ const expected = Object.keys(config);
+ const [{ payload }] = events.eventCliSession(
+ {
+ cliCommand: 'dev',
+ astroVersion: '0.0.0',
+ },
+ config
+ );
+ expect(payload.configKeys).to.deep.equal(expected);
});
-
- const [{ payload }] = events.eventCliSession(
- {
- cliCommand: 'dev',
- astroVersion: '0.0.0',
- },
- config
- );
- expect(payload.config.viteKeys).is.deep.equal([
- 'css',
- 'css.modules',
- 'base',
- 'mode',
- 'define',
- 'publicDir',
- ]);
});
- it('vite.resolve keys are captured', async () => {
- const config = await mockConfig({
- vite: {
- resolve: {
- alias: {
- a: 'b',
- },
- dedupe: ['one', 'two'],
+ describe('config.build', () => {
+ it('configKeys includes format', () => {
+ const config = {
+ srcDir: 1,
+ build: {
+ format: 'file',
+ }
+ };
+ const [{ payload }] = events.eventCliSession(
+ {
+ cliCommand: 'dev',
+ astroVersion: '0.0.0'
},
- },
+ config
+ );
+ expect(payload.configKeys).to.deep.equal(['srcDir', 'build', 'build.format']);
});
- const [{ payload }] = events.eventCliSession(
- {
- cliCommand: 'dev',
- astroVersion: '0.0.0',
- },
- config
- );
- expect(payload.config.viteKeys).is.deep.equal(['resolve', 'resolve.alias', 'resolve.dedupe']);
- });
-
- it('vite.css keys are captured', async () => {
- const config = await mockConfig({
- vite: {
- resolve: {
- dedupe: ['one', 'two'],
- },
- css: {
- modules: [],
- postcss: {},
+ it('config.build.format', () => {
+ const config = {
+ srcDir: 1,
+ build: {
+ format: 'file',
+ }
+ };
+ const [{ payload }] = events.eventCliSession(
+ {
+ cliCommand: 'dev',
+ astroVersion: '0.0.0'
},
- },
+ config
+ );
+ expect(payload.config.build.format).to.equal('file');
});
-
- const [{ payload }] = events.eventCliSession(
- {
- cliCommand: 'dev',
- astroVersion: '0.0.0',
- },
- config
- );
- expect(payload.config.viteKeys).is.deep.equal([
- 'resolve',
- 'resolve.dedupe',
- 'css',
- 'css.modules',
- 'css.postcss',
- ]);
});
- it('vite.server keys are captured', async () => {
- const config = await mockConfig({
- vite: {
+ describe('config.server', () => {
+ it('configKeys includes server props', () => {
+ const config = {
+ srcDir: 1,
server: {
host: 'example.com',
- open: true,
- fs: {
- strict: true,
- allow: ['a', 'b'],
- },
+ port: 8033
+ }
+ };
+ const [{ payload }] = events.eventCliSession(
+ {
+ cliCommand: 'dev',
+ astroVersion: '0.0.0'
},
- },
+ config
+ );
+ expect(payload.configKeys).to.deep.equal(['srcDir', 'server', 'server.host', 'server.port']);
});
-
- const [{ payload }] = events.eventCliSession(
- {
- cliCommand: 'dev',
- astroVersion: '0.0.0',
- },
- config
- );
- expect(payload.config.viteKeys).is.deep.equal([
- 'server',
- 'server.host',
- 'server.open',
- 'server.fs',
- 'server.fs.strict',
- 'server.fs.allow',
- ]);
});
- it('vite.build keys are captured', async () => {
- const config = await mockConfig({
- vite: {
- build: {
- target: 'one',
- outDir: 'some/dir',
- cssTarget: {
- one: 'two',
+ describe('config.markdown', () => {
+ it('configKeys is deep', () => {
+ const config = {
+ publicDir: 1,
+ markdown: {
+ drafts: true,
+ mode: 'mdx',
+ shikiConfig: {
+ lang: 1,
+ theme: 2,
+ wrap: 3
},
+ syntaxHighlight: 'shiki',
+ remarkPlugins: [],
+ rehypePlugins: [],
+ }
+ };
+ const [{ payload }] = events.eventCliSession(
+ {
+ cliCommand: 'dev',
+ astroVersion: '0.0.0'
+ },
+ config
+ );
+ expect(payload.configKeys).to.deep.equal(['publicDir', 'markdown',
+ 'markdown.drafts', 'markdown.mode', 'markdown.shikiConfig',
+ 'markdown.shikiConfig.lang', 'markdown.shikiConfig.theme', 'markdown.shikiConfig.wrap',
+ 'markdown.syntaxHighlight', 'markdown.remarkPlugins', 'markdown.rehypePlugins']);
+ });
+
+ it('mode', () => {
+ const config = {
+ markdown: {
+ mode: 'mdx',
+ }
+ };
+ const [{ payload }] = events.eventCliSession(
+ {
+ cliCommand: 'dev',
+ astroVersion: '0.0.0'
},
- },
+ config
+ );
+ expect(payload.config.markdown.mode).to.equal('mdx');
});
- const [{ payload }] = events.eventCliSession(
- {
- cliCommand: 'dev',
- astroVersion: '0.0.0',
- },
- config
- );
- expect(payload.config.viteKeys).is.deep.equal([
- 'build',
- 'build.target',
- 'build.outDir',
- 'build.cssTarget',
- ]);
+ it('syntaxHighlight', () => {
+ const config = {
+ markdown: {
+ syntaxHighlight: 'shiki',
+ }
+ };
+ const [{ payload }] = events.eventCliSession(
+ {
+ cliCommand: 'dev',
+ astroVersion: '0.0.0'
+ },
+ config
+ );
+ expect(payload.config.markdown.syntaxHighlight).to.equal('shiki');
+ });
});
- it('vite.preview keys are captured', async () => {
- const config = await mockConfig({
- vite: {
- preview: {
- host: 'example.com',
- port: 8080,
- another: {
+ describe('config.vite', () => {
+ it('top-level keys are captured', async () => {
+ const config = {
+ root: 'some/thing',
+ vite: {
+ css: { modules: [] },
+ base: 'a',
+ mode: 'b',
+ define: {
a: 'b',
},
+ publicDir: 'some/dir',
+ },
+ };
+
+ const [{ payload }] = events.eventCliSession(
+ {
+ cliCommand: 'dev',
+ astroVersion: '0.0.0',
},
- },
+ config
+ );
+ expect(payload.configKeys).is.deep.equal([
+ 'root',
+ 'vite',
+ 'vite.css',
+ 'vite.css.modules',
+ 'vite.base',
+ 'vite.mode',
+ 'vite.define',
+ 'vite.publicDir',
+ ]);
});
-
- const [{ payload }] = events.eventCliSession(
- {
- cliCommand: 'dev',
- astroVersion: '0.0.0',
- },
- config
- );
- expect(payload.config.viteKeys).is.deep.equal([
- 'preview',
- 'preview.host',
- 'preview.port',
- 'preview.another',
- ]);
- });
-
- it('vite.optimizeDeps keys are captured', async () => {
- const config = await mockConfig({
- vite: {
- optimizeDeps: {
- entries: ['one', 'two'],
- exclude: ['secret', 'name'],
+
+ it('vite.resolve keys are captured', async () => {
+ const config = {
+ vite: {
+ resolve: {
+ alias: {
+ a: 'b',
+ },
+ dedupe: ['one', 'two'],
+ },
},
- },
+ };
+
+ const [{ payload }] = events.eventCliSession(
+ {
+ cliCommand: 'dev',
+ astroVersion: '0.0.0',
+ },
+ config
+ );
+ expect(payload.configKeys).is.deep.equal(['vite', 'vite.resolve', 'vite.resolve.alias', 'vite.resolve.dedupe']);
});
-
- const [{ payload }] = events.eventCliSession(
- {
- cliCommand: 'dev',
- astroVersion: '0.0.0',
- },
- config
- );
- expect(payload.config.viteKeys).is.deep.equal([
- 'optimizeDeps',
- 'optimizeDeps.entries',
- 'optimizeDeps.exclude',
- ]);
- });
-
- it('vite.ssr keys are captured', async () => {
- const config = await mockConfig({
- vite: {
- ssr: {
- external: ['a'],
- target: { one: 'two' },
+
+ it('vite.css keys are captured', async () => {
+ const config = {
+ vite: {
+ resolve: {
+ dedupe: ['one', 'two'],
+ },
+ css: {
+ modules: [],
+ postcss: {},
+ },
+ },
+ };
+
+ const [{ payload }] = events.eventCliSession(
+ {
+ cliCommand: 'dev',
+ astroVersion: '0.0.0',
},
- },
+ config
+ );
+ expect(payload.configKeys).is.deep.equal([
+ 'vite',
+ 'vite.resolve',
+ 'vite.resolve.dedupe',
+ 'vite.css',
+ 'vite.css.modules',
+ 'vite.css.postcss',
+ ]);
});
-
- const [{ payload }] = events.eventCliSession(
- {
- cliCommand: 'dev',
- astroVersion: '0.0.0',
- },
- config
- );
- expect(payload.config.viteKeys).is.deep.equal(['ssr', 'ssr.external', 'ssr.target']);
- });
-
- it('vite.worker keys are captured', async () => {
- const config = await mockConfig({
- vite: {
- worker: {
- format: { a: 'b' },
- plugins: ['a', 'b'],
+
+ it('vite.server keys are captured', async () => {
+ const config = {
+ vite: {
+ server: {
+ host: 'example.com',
+ open: true,
+ fs: {
+ strict: true,
+ allow: ['a', 'b'],
+ },
+ },
},
- },
+ };
+
+ const [{ payload }] = events.eventCliSession(
+ {
+ cliCommand: 'dev',
+ astroVersion: '0.0.0',
+ },
+ config
+ );
+ expect(payload.configKeys).is.deep.equal([
+ 'vite',
+ 'vite.server',
+ 'vite.server.host',
+ 'vite.server.open',
+ 'vite.server.fs',
+ 'vite.server.fs.strict',
+ 'vite.server.fs.allow',
+ ]);
+ });
+
+ it('vite.build keys are captured', async () => {
+ const config = {
+ vite: {
+ build: {
+ target: 'one',
+ outDir: 'some/dir',
+ cssTarget: {
+ one: 'two',
+ },
+ },
+ },
+ };
+
+ const [{ payload }] = events.eventCliSession(
+ {
+ cliCommand: 'dev',
+ astroVersion: '0.0.0',
+ },
+ config
+ );
+ expect(payload.configKeys).is.deep.equal([
+ 'vite',
+ 'vite.build',
+ 'vite.build.target',
+ 'vite.build.outDir',
+ 'vite.build.cssTarget',
+ ]);
+ });
+
+ it('vite.preview keys are captured', async () => {
+ const config = {
+ vite: {
+ preview: {
+ host: 'example.com',
+ port: 8080,
+ another: {
+ a: 'b',
+ },
+ },
+ },
+ };
+
+ const [{ payload }] = events.eventCliSession(
+ {
+ cliCommand: 'dev',
+ astroVersion: '0.0.0',
+ },
+ config
+ );
+ expect(payload.configKeys).is.deep.equal([
+ 'vite',
+ 'vite.preview',
+ 'vite.preview.host',
+ 'vite.preview.port',
+ 'vite.preview.another',
+ ]);
+ });
+
+ it('vite.optimizeDeps keys are captured', async () => {
+ const config = {
+ vite: {
+ optimizeDeps: {
+ entries: ['one', 'two'],
+ exclude: ['secret', 'name'],
+ },
+ },
+ };
+
+ const [{ payload }] = events.eventCliSession(
+ {
+ cliCommand: 'dev',
+ astroVersion: '0.0.0',
+ },
+ config
+ );
+ expect(payload.configKeys).is.deep.equal([
+ 'vite',
+ 'vite.optimizeDeps',
+ 'vite.optimizeDeps.entries',
+ 'vite.optimizeDeps.exclude',
+ ]);
+ });
+
+ it('vite.ssr keys are captured', async () => {
+ const config = {
+ vite: {
+ ssr: {
+ external: ['a'],
+ target: { one: 'two' },
+ },
+ },
+ };
+
+ const [{ payload }] = events.eventCliSession(
+ {
+ cliCommand: 'dev',
+ astroVersion: '0.0.0',
+ },
+ config
+ );
+ expect(payload.configKeys).is.deep.equal(['vite', 'vite.ssr', 'vite.ssr.external', 'vite.ssr.target']);
+ });
+
+ it('vite.worker keys are captured', async () => {
+ const config = {
+ vite: {
+ worker: {
+ format: { a: 'b' },
+ plugins: ['a', 'b'],
+ },
+ },
+ };
+
+ const [{ payload }] = events.eventCliSession(
+ {
+ cliCommand: 'dev',
+ astroVersion: '0.0.0',
+ },
+ config
+ );
+ expect(payload.configKeys).is.deep.equal(['vite', 'vite.worker', 'vite.worker.format', 'vite.worker.plugins']);
});
-
- const [{ payload }] = events.eventCliSession(
- {
- cliCommand: 'dev',
- astroVersion: '0.0.0',
- },
- config
- );
- expect(payload.config.viteKeys).is.deep.equal(['worker', 'worker.format', 'worker.plugins']);
});
+
+ describe('flags', () => {
+ it('includes cli flags in payload', () => {
+ const config = {};
+ const flags = {
+ root: 'root',
+ site: 'http://example.com',
+ host: true,
+ port: 8080,
+ config: 'path/to/config.mjs',
+ experimentalSsr: true,
+ experimentalIntegrations: true,
+ drafts: true,
+ }
+ const [{ payload }] = events.eventCliSession(
+ {
+ cliCommand: 'dev',
+ astroVersion: '0.0.0',
+ },
+ config,
+ flags
+ );
+ expect(payload.flags).to.deep.equal([
+ 'root',
+ 'site',
+ 'host',
+ 'port',
+ 'config',
+ 'experimentalSsr',
+ 'experimentalIntegrations',
+ 'drafts'
+ ]);
+ })
+ })
});