-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathenricher.ts
More file actions
72 lines (63 loc) · 2.24 KB
/
enricher.ts
File metadata and controls
72 lines (63 loc) · 2.24 KB
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
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { PostHogDetector } from "./detector.js";
import { EXT_TO_LANG_ID } from "./languages.js";
import { warn } from "./log.js";
import { ParseResult } from "./parse-result.js";
import type { DetectionConfig } from "./types.js";
export class PostHogEnricher {
private detector = new PostHogDetector();
updateConfig(config: DetectionConfig): void {
this.detector.updateConfig(config);
}
isSupported(langId: string): boolean {
return this.detector.isSupported(langId);
}
get supportedLanguages(): string[] {
return this.detector.supportedLanguages;
}
async parse(source: string, languageId: string): Promise<ParseResult> {
const results = await Promise.allSettled([
this.detector.findPostHogCalls(source, languageId),
this.detector.findInitCalls(source, languageId),
this.detector.findFlagAssignments(source, languageId),
this.detector.findVariantBranches(source, languageId),
this.detector.findFunctions(source, languageId),
]);
const settled = results.map((r, i) => {
if (r.status === "fulfilled") {
return r.value;
}
const labels = [
"calls",
"initCalls",
"flagAssignments",
"variantBranches",
"functions",
];
warn(`enricher: ${labels[i]} detection failed`, r.reason);
return [];
});
return new ParseResult(
source,
languageId,
settled[0] as Awaited<ReturnType<PostHogDetector["findPostHogCalls"]>>,
settled[1] as Awaited<ReturnType<PostHogDetector["findInitCalls"]>>,
settled[2] as Awaited<ReturnType<PostHogDetector["findFlagAssignments"]>>,
settled[3] as Awaited<ReturnType<PostHogDetector["findVariantBranches"]>>,
settled[4] as Awaited<ReturnType<PostHogDetector["findFunctions"]>>,
);
}
async parseFile(filePath: string): Promise<ParseResult> {
const ext = path.extname(filePath).toLowerCase();
const languageId = EXT_TO_LANG_ID[ext];
if (!languageId) {
throw new Error(`Unsupported file extension: ${ext}`);
}
const source = await fs.readFile(filePath, "utf-8");
return this.parse(source, languageId);
}
dispose(): void {
this.detector.dispose();
}
}