summaryrefslogtreecommitdiff
path: root/packages/telemetry/src/events/session.ts
blob: e8c222bf1c81d33825dd2858bba712c24f838087 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { createRequire } from 'node:module';

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 {
	markdownPlugins: string[];
	adapter: string | null;
	integrations: 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[];
	optionalIntegrations?: number;
}

function getViteVersion() {
	try {
		const { version } = require('vite/package.json');
		return version;
	} catch (e) {}
	return undefined;
}

const multiLevelKeys = new Set([
	'build',
	'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 configKeys(obj: Record<string, any> | undefined, parentKey: string): string[] {
	if (!obj) {
		return [];
	}

	return Object.entries(obj)
		.map(([key, value]) => {
			if (typeof value === 'object' && !Array.isArray(value)) {
				const localKey = parentKey ? parentKey + '.' + key : key;
				if (multiLevelKeys.has(localKey)) {
					let keys = configKeys(value, localKey).map((subkey) => key + '.' + subkey);
					keys.unshift(key);
					return keys;
				}
			}

			return key;
		})
		.flat(1);
}

export function eventCliSession(
	event: 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: 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: configValues,
		flags: cliFlags,
		// Optional integrations
		optionalIntegrations: userConfig?.integrations?.length - integrations?.length,
	};
	return [{ eventName: EVENT_SESSION, payload }];
}