mirror of
https://github.com/trezor/trezor-suite.git
synced 2026-03-22 07:07:09 +01:00
- Added scripts directory to TypeScript configuration for better module inclusion. - Refactored buildData script to use consistent CommonJS require syntax. - Enhanced event loading and normalization logic for clarity and maintainability. - Updated platform and sorting types for improved type safety. - Adjusted styling in components for better layout and responsiveness.
116 lines
3.8 KiB
TypeScript
116 lines
3.8 KiB
TypeScript
import fs from 'node:fs';
|
|
import { createRequire } from 'node:module';
|
|
import path from 'node:path';
|
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
|
|
import type { EventDef } from '@suite-common/analytics';
|
|
|
|
import { EventDoc, platforms } from '../src/types';
|
|
import {
|
|
AttributeTypesByEventName,
|
|
extractAttributeTypesByEventName,
|
|
findPackageRoot,
|
|
findUp,
|
|
} from '../src/utils/extractAttributeTypes';
|
|
import { normalizeEvents } from '../src/utils/normalizeEvents';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
const cjsRequire = createRequire(import.meta.url);
|
|
const repoRoot = path.resolve(__dirname, '../../..');
|
|
|
|
const PACKAGES = [
|
|
'@suite-common/analytics',
|
|
'@suite/analytics',
|
|
'@suite-native/analytics',
|
|
] as const;
|
|
|
|
const loadEventsFromPackage = async (
|
|
packageName: string,
|
|
): Promise<Array<EventDef<unknown, string>>> => {
|
|
const packageRoot = path.dirname(cjsRequire.resolve(`${packageName}/package.json`));
|
|
const eventsPath = path.join(packageRoot, 'src', 'events', 'index.ts');
|
|
const module = await import(pathToFileURL(eventsPath).href);
|
|
|
|
return Object.values(module) as Array<EventDef<unknown, string>>;
|
|
};
|
|
|
|
const loadAllEvents = async (): Promise<
|
|
Array<EventDef<unknown, string> & { platform: string }>
|
|
> => {
|
|
const [shared, desktop, mobile] = await Promise.all(
|
|
PACKAGES.map(name => loadEventsFromPackage(name)),
|
|
);
|
|
const lists = [shared, desktop, mobile];
|
|
|
|
return lists.flatMap((list, i) => list.map(event => ({ ...event, platform: platforms[i] })));
|
|
};
|
|
|
|
const getTsConfigPath = (): string => {
|
|
const docgenPath = path.resolve(__dirname, '../tsconfig.docgen.json');
|
|
if (fs.existsSync(docgenPath)) return docgenPath;
|
|
const up = findUp('tsconfig.json', path.resolve(__dirname, '..'));
|
|
|
|
return up ?? path.resolve(repoRoot, 'tsconfig.json');
|
|
};
|
|
|
|
const getPackageRoots = (): string[] => {
|
|
const roots = PACKAGES.map(name =>
|
|
findPackageRoot(cjsRequire.resolve(`${name}/package.json`)),
|
|
).filter((x): x is string => Boolean(x));
|
|
|
|
return [...new Set(roots)];
|
|
};
|
|
|
|
const getEventFileGlobs = (packageRoots: string[]): string[] =>
|
|
packageRoots.flatMap(root => [
|
|
path.join(root, 'src/**/*.{ts,tsx}'),
|
|
path.join(root, 'dist/**/*.d.ts'),
|
|
]);
|
|
|
|
const mergeRuntimeTypes = (
|
|
events: Record<string, EventDoc>,
|
|
attributeTypesByEventName: AttributeTypesByEventName,
|
|
): void => {
|
|
for (const [eventName, eventDoc] of Object.entries(events)) {
|
|
const eventTypes = attributeTypesByEventName[eventName];
|
|
if (!eventTypes) continue;
|
|
|
|
for (const [attrName, attrDoc] of Object.entries(eventDoc.attributes)) {
|
|
const runtimeType = eventTypes[attrName];
|
|
if (runtimeType) attrDoc.runtimeType = runtimeType;
|
|
}
|
|
}
|
|
};
|
|
|
|
const writeOutput = (data: { events: Record<string, EventDoc> }, outputPath: string): void => {
|
|
const pretty = process.env.PRETTY_ANALYTICS_JSON === '1';
|
|
const json = pretty ? JSON.stringify(data, null, 2) : JSON.stringify(data);
|
|
fs.writeFileSync(outputPath, json, 'utf-8');
|
|
};
|
|
|
|
const run = async () => {
|
|
const allEvents = await loadAllEvents();
|
|
const normalizedEvents = normalizeEvents(allEvents);
|
|
|
|
const tsConfigPath = getTsConfigPath();
|
|
const packageRoots = getPackageRoots();
|
|
const eventFileGlobs = getEventFileGlobs(packageRoots);
|
|
|
|
const attributeTypesByEventName = extractAttributeTypesByEventName({
|
|
tsConfigFilePath: tsConfigPath,
|
|
eventFileGlobs,
|
|
});
|
|
|
|
mergeRuntimeTypes(normalizedEvents, attributeTypesByEventName);
|
|
|
|
const outputPath = path.resolve(__dirname, '../src/analytics.json');
|
|
writeOutput({ events: normalizedEvents }, outputPath);
|
|
|
|
// eslint-disable-next-line no-console
|
|
console.log(
|
|
`[analytics-docs] analytics.json generated (${Object.keys(normalizedEvents).length} events)`,
|
|
);
|
|
};
|
|
|
|
void run();
|