@eventra_dev/eventra-cli 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/.github/workflows/release.yml +3 -0
  2. package/README.md +125 -44
  3. package/dist/commands/init.js +4 -1
  4. package/dist/commands/send.js +41 -7
  5. package/dist/commands/sync.js +35 -194
  6. package/dist/index.js +0 -0
  7. package/dist/types.js +2 -0
  8. package/dist/utils/config.js +4 -1
  9. package/dist/utils/extract.js +44 -0
  10. package/dist/utils/parsers/astro.js +6 -0
  11. package/dist/utils/parsers/router.js +12 -0
  12. package/dist/utils/parsers/svelte.js +6 -0
  13. package/dist/utils/parsers/vue.js +18 -0
  14. package/dist/utils/scanners/component-wrappers.js +55 -0
  15. package/dist/utils/scanners/function-wrappers.js +45 -0
  16. package/dist/utils/scanners/track.js +42 -0
  17. package/package.json +6 -4
  18. package/src/commands/init.ts +4 -1
  19. package/src/commands/send.ts +101 -17
  20. package/src/commands/sync.ts +66 -345
  21. package/src/types.ts +20 -0
  22. package/src/utils/config.ts +24 -7
  23. package/src/utils/extract.ts +74 -0
  24. package/src/utils/parsers/astro.ts +5 -0
  25. package/src/utils/parsers/router.ts +14 -0
  26. package/src/utils/parsers/svelte.ts +5 -0
  27. package/src/utils/parsers/vue.ts +25 -0
  28. package/src/utils/scanners/component-wrappers.ts +108 -0
  29. package/src/utils/scanners/function-wrappers.ts +98 -0
  30. package/src/utils/scanners/track.ts +84 -0
  31. package/tests/fixtures/backend/express/app.ts +78 -0
  32. package/tests/fixtures/backend/nest/service.ts +70 -0
  33. package/tests/fixtures/backend/node/index.ts +63 -0
  34. package/tests/fixtures/frontend/next/page.tsx +101 -0
  35. package/tests/fixtures/frontend/react/App.tsx +104 -0
  36. package/tests/fixtures/frontend/vue/App.vue +83 -0
  37. package/tests/fixtures/wrappers/component/test.tsx +62 -0
  38. package/tests/fixtures/wrappers/function/test.ts +60 -0
  39. package/tests/run.ts +120 -0
  40. package/tsconfig.json +2 -0
@@ -0,0 +1,108 @@
1
+ import {
2
+ SourceFile,
3
+ SyntaxKind,
4
+ } from "ts-morph";
5
+
6
+ import { ComponentWrapper } from "../../types";
7
+
8
+ export function scanComponentWrappers(
9
+ source: SourceFile,
10
+ wrappers: ComponentWrapper[]
11
+ ) {
12
+ const events = new Set<string>();
13
+
14
+ const elements = [
15
+ ...source.getDescendantsOfKind(
16
+ SyntaxKind.JsxOpeningElement
17
+ ),
18
+ ...source.getDescendantsOfKind(
19
+ SyntaxKind.JsxSelfClosingElement
20
+ )
21
+ ];
22
+
23
+ for (const el of elements) {
24
+ const name = el
25
+ .getTagNameNode()
26
+ .getText()
27
+ .toLowerCase();
28
+
29
+ for (const wrapper of wrappers) {
30
+ if (
31
+ name !==
32
+ wrapper.name.toLowerCase()
33
+ )
34
+ continue;
35
+
36
+ const attrs =
37
+ el.getAttributes();
38
+
39
+ for (const attr of attrs) {
40
+ const attrNode =
41
+ attr.asKind(
42
+ SyntaxKind.JsxAttribute
43
+ );
44
+
45
+ if (!attrNode) continue;
46
+
47
+ const key =
48
+ attrNode
49
+ .getNameNode()
50
+ .getText()
51
+ .toLowerCase();
52
+
53
+ if (
54
+ key !==
55
+ wrapper.prop.toLowerCase()
56
+ )
57
+ continue;
58
+
59
+ const init =
60
+ attrNode.getInitializer();
61
+
62
+ if (!init) continue;
63
+
64
+ if (
65
+ init.getKind() ===
66
+ SyntaxKind.StringLiteral
67
+ ) {
68
+ const value =
69
+ init.asKindOrThrow(
70
+ SyntaxKind.StringLiteral
71
+ );
72
+
73
+ events.add(
74
+ value.getLiteralText()
75
+ );
76
+ }
77
+
78
+ if (
79
+ init.getKind() ===
80
+ SyntaxKind.JsxExpression
81
+ ) {
82
+ const expr =
83
+ init
84
+ .asKindOrThrow(
85
+ SyntaxKind.JsxExpression
86
+ )
87
+ .getExpression();
88
+
89
+ if (
90
+ expr?.getKind() ===
91
+ SyntaxKind.StringLiteral
92
+ ) {
93
+ const value =
94
+ expr.asKindOrThrow(
95
+ SyntaxKind.StringLiteral
96
+ );
97
+
98
+ events.add(
99
+ value.getLiteralText()
100
+ );
101
+ }
102
+ }
103
+ }
104
+ }
105
+ }
106
+
107
+ return events;
108
+ }
@@ -0,0 +1,98 @@
1
+ import {
2
+ SourceFile,
3
+ SyntaxKind,
4
+ Node,
5
+ CallExpression,
6
+ PropertyAccessExpression,
7
+ } from "ts-morph";
8
+
9
+ import { extractEvent } from "../extract";
10
+ import { FunctionWrapper } from "../../types";
11
+
12
+ export function scanFunctionWrappers(
13
+ source: SourceFile,
14
+ wrappers: FunctionWrapper[]
15
+ ) {
16
+ const events = new Set<string>();
17
+
18
+ const calls =
19
+ source.getDescendantsOfKind(
20
+ SyntaxKind.CallExpression
21
+ );
22
+
23
+ for (const call of calls) {
24
+ const name =
25
+ getFunctionName(call);
26
+
27
+ if (!name) continue;
28
+
29
+ for (const wrapper of wrappers) {
30
+ if (wrapper.name !== name)
31
+ continue;
32
+
33
+ const event =
34
+ extractEvent(
35
+ call,
36
+ wrapper.path ?? "0"
37
+ );
38
+
39
+ if (event)
40
+ events.add(event);
41
+ }
42
+ }
43
+
44
+ return events;
45
+ }
46
+
47
+ function getFunctionName(
48
+ call: CallExpression
49
+ ): string | null {
50
+ const expression =
51
+ call.getExpression();
52
+
53
+ // trackFeature()
54
+ if (
55
+ Node.isIdentifier(
56
+ expression
57
+ )
58
+ ) {
59
+ return expression.getText();
60
+ }
61
+
62
+ // analytics.trackFeature()
63
+ if (
64
+ Node.isPropertyAccessExpression(
65
+ expression
66
+ )
67
+ ) {
68
+ return getDeepName(
69
+ expression
70
+ );
71
+ }
72
+
73
+ return null;
74
+ }
75
+
76
+ function getDeepName(
77
+ node: PropertyAccessExpression
78
+ ): string {
79
+ let current:
80
+ | Node
81
+ | undefined = node;
82
+
83
+ let name = "";
84
+
85
+ while (
86
+ Node.isPropertyAccessExpression(
87
+ current
88
+ )
89
+ ) {
90
+ name =
91
+ current.getName();
92
+
93
+ current =
94
+ current.getExpression();
95
+ }
96
+
97
+ return name;
98
+ }
@@ -0,0 +1,84 @@
1
+ import {
2
+ SourceFile,
3
+ SyntaxKind
4
+ } from "ts-morph";
5
+
6
+ export function scanTrack(
7
+ source: SourceFile
8
+ ) {
9
+ const events =
10
+ new Set<string>();
11
+
12
+ const calls =
13
+ source.getDescendantsOfKind(
14
+ SyntaxKind.CallExpression
15
+ );
16
+
17
+ for (const call of calls) {
18
+ const expr =
19
+ call.getExpression();
20
+
21
+ let isTrack = false;
22
+
23
+ // track()
24
+ if (
25
+ expr.getKind() ===
26
+ SyntaxKind.Identifier
27
+ ) {
28
+ isTrack =
29
+ expr.getText() === "track";
30
+ }
31
+
32
+ // analytics.track()
33
+ if (
34
+ expr.getKind() ===
35
+ SyntaxKind.PropertyAccessExpression
36
+ ) {
37
+ const prop =
38
+ expr.asKindOrThrow(
39
+ SyntaxKind.PropertyAccessExpression
40
+ );
41
+
42
+ isTrack =
43
+ prop.getName() === "track";
44
+ }
45
+
46
+ if (!isTrack) continue;
47
+
48
+ const arg =
49
+ call.getArguments()[0];
50
+
51
+ if (!arg) continue;
52
+
53
+ if (
54
+ arg.getKind() ===
55
+ SyntaxKind.StringLiteral
56
+ ) {
57
+ const value =
58
+ arg.asKindOrThrow(
59
+ SyntaxKind.StringLiteral
60
+ );
61
+
62
+ events.add(
63
+ value.getLiteralText()
64
+ );
65
+ }
66
+
67
+ // template literal
68
+ if (
69
+ arg.getKind() ===
70
+ SyntaxKind.NoSubstitutionTemplateLiteral
71
+ ) {
72
+ const value =
73
+ arg.asKindOrThrow(
74
+ SyntaxKind.NoSubstitutionTemplateLiteral
75
+ );
76
+
77
+ events.add(
78
+ value.getLiteralText()
79
+ );
80
+ }
81
+ }
82
+
83
+ return events;
84
+ }
@@ -0,0 +1,78 @@
1
+ /* eslint-disable */
2
+
3
+ declare function track(event: string): void;
4
+
5
+ const express = (...args: any[]) => ({
6
+ get: (...args: any[]) => {},
7
+ post: (...args: any[]) => {},
8
+ put: (...args: any[]) => {},
9
+ delete: (...args: any[]) => {},
10
+ use: (...args: any[]) => {},
11
+ listen: (...args: any[]) => {}
12
+ });
13
+
14
+ const app = express();
15
+
16
+ // ===== direct =====
17
+ track("express_event");
18
+
19
+ // ===== middleware =====
20
+ app.use(() => {
21
+ track("middleware_event");
22
+ });
23
+
24
+ // ===== get =====
25
+ app.get("/", () => {
26
+ track("get_event");
27
+ });
28
+
29
+ // ===== post =====
30
+ app.post("/users", () => {
31
+ track("post_event");
32
+ });
33
+
34
+ // ===== put =====
35
+ app.put("/users", () => {
36
+ track("put_event");
37
+ });
38
+
39
+ // ===== delete =====
40
+ app.delete("/users", () => {
41
+ track("delete_event");
42
+ });
43
+
44
+ // ===== nested =====
45
+ app.get("/nested", () => {
46
+ if (true) {
47
+ track("nested_event");
48
+ }
49
+ });
50
+
51
+ // ===== async =====
52
+ app.get("/async", async () => {
53
+ track("async_event");
54
+ });
55
+
56
+ // ===== arrow =====
57
+ const handler = () => {
58
+ track("arrow_event");
59
+ };
60
+
61
+ app.get("/arrow", handler);
62
+
63
+ // ===== function =====
64
+ function service() {
65
+ track("function_event");
66
+ }
67
+
68
+ // ===== class =====
69
+ class Service {
70
+ run() {
71
+ track("class_event");
72
+ }
73
+ }
74
+
75
+ // ===== listen =====
76
+ app.listen(3000, () => {
77
+ track("listen_event");
78
+ });
@@ -0,0 +1,70 @@
1
+ /* eslint-disable */
2
+
3
+ declare function track(event: string): void;
4
+
5
+ function Injectable(): any {
6
+ return () => {};
7
+ }
8
+
9
+ function Controller(): any {
10
+ return () => {};
11
+ }
12
+
13
+ function Get(): any {
14
+ return () => {};
15
+ }
16
+
17
+ function Post(): any {
18
+ return () => {};
19
+ }
20
+
21
+ // ===== service =====
22
+ @Injectable()
23
+ export class TestService {
24
+ run() {
25
+ track("nest_event");
26
+ }
27
+
28
+ async asyncRun() {
29
+ track("async_event");
30
+ }
31
+
32
+ arrow = () => {
33
+ track("arrow_event");
34
+ };
35
+ }
36
+
37
+ // ===== controller =====
38
+ @Controller()
39
+ export class TestController {
40
+ @Get()
41
+ get() {
42
+ track("get_event");
43
+ }
44
+
45
+ @Post()
46
+ post() {
47
+ track("post_event");
48
+ }
49
+
50
+ nested() {
51
+ if (true) {
52
+ track("nested_event");
53
+ }
54
+ }
55
+ }
56
+
57
+ // ===== function =====
58
+ function service() {
59
+ track("function_event");
60
+ }
61
+
62
+ // ===== class =====
63
+ class AnotherService {
64
+ run() {
65
+ track("class_event");
66
+ }
67
+ }
68
+
69
+ // ===== direct =====
70
+ track("direct_event");
@@ -0,0 +1,63 @@
1
+ /* eslint-disable */
2
+
3
+ declare function track(event: string): void;
4
+
5
+ const eventName = "variable_event";
6
+
7
+ // ===== direct =====
8
+ track("node_event");
9
+
10
+ // ===== multiline =====
11
+ track(
12
+ "multiline_event"
13
+ );
14
+
15
+ // ===== template string =====
16
+ track(`template_event`);
17
+
18
+ // ===== conditional =====
19
+ if (true) {
20
+ track("conditional_event");
21
+ }
22
+
23
+ // ===== ternary =====
24
+ true
25
+ ? track("ternary_a")
26
+ : track("ternary_b");
27
+
28
+ // ===== variable =====
29
+ track(eventName);
30
+
31
+ // ===== function =====
32
+ function run() {
33
+ track("function_event");
34
+ }
35
+
36
+ // ===== arrow =====
37
+ const handler = () => {
38
+ track("arrow_event");
39
+ };
40
+
41
+ // ===== class =====
42
+ class Service {
43
+ run() {
44
+ track("class_event");
45
+ }
46
+ }
47
+
48
+ // ===== nested =====
49
+ function outer() {
50
+ function inner() {
51
+ track("nested_event");
52
+ }
53
+ }
54
+
55
+ // ===== async =====
56
+ async function asyncRun() {
57
+ track("async_event");
58
+ }
59
+
60
+ // ===== promise =====
61
+ Promise.resolve().then(() => {
62
+ track("promise_event");
63
+ });
@@ -0,0 +1,101 @@
1
+ /* eslint-disable */
2
+
3
+ declare function track(event: string): void;
4
+ declare function trackFeature(event: string): void;
5
+
6
+ declare const analytics: any;
7
+ declare const Button: any;
8
+ declare const TrackedButton: any;
9
+ declare const MyButton: any;
10
+
11
+ const eventName = "variable_event";
12
+
13
+ export default function Page() {
14
+ // ===== direct =====
15
+ track("next_event");
16
+
17
+ // ===== multiline =====
18
+ track(
19
+ "multiline_event"
20
+ );
21
+
22
+ // ===== template string =====
23
+ track(`template_event`);
24
+
25
+ // ===== wrapper function =====
26
+ trackFeature("wrapper_function");
27
+
28
+ // ===== object =====
29
+ analytics.track("object_track");
30
+
31
+ // ===== nested object =====
32
+ analytics.events.track("nested_track");
33
+
34
+ // ===== variable =====
35
+ track(eventName);
36
+
37
+ // ===== conditional =====
38
+ if (true) {
39
+ track("conditional_event");
40
+ }
41
+
42
+ // ===== ternary =====
43
+ true
44
+ ? track("ternary_a")
45
+ : track("ternary_b");
46
+
47
+ // ===== arrow function =====
48
+ const run = () => {
49
+ track("arrow_event");
50
+ };
51
+
52
+ // ===== function =====
53
+ function test() {
54
+ track("function_event");
55
+ }
56
+
57
+ // ===== class =====
58
+ class Service {
59
+ run() {
60
+ track("class_event");
61
+ }
62
+ }
63
+
64
+ return (
65
+ <>
66
+ {/* basic */}
67
+ <Button event="next_button" />
68
+
69
+ {/* expression */}
70
+ <Button event={"expression_event"} />
71
+
72
+ {/* wrapper component */}
73
+ <TrackedButton event="tracked_button" />
74
+
75
+ {/* another wrapper */}
76
+ <MyButton event="my_button" />
77
+
78
+ {/* nested */}
79
+ <div>
80
+ <Button event="nested_button" />
81
+ </div>
82
+
83
+ {/* conditional */}
84
+ {true && (
85
+ <Button event="conditional_button" />
86
+ )}
87
+
88
+ {/* ternary */}
89
+ {true
90
+ ? <Button event="ternary_a_button" />
91
+ : <Button event="ternary_b_button" />
92
+ }
93
+
94
+ {/* array */}
95
+ {[
96
+ <Button key="1" event="array_1" />,
97
+ <Button key="2" event="array_2" />
98
+ ]}
99
+ </>
100
+ );
101
+ }
@@ -0,0 +1,104 @@
1
+ /* eslint-disable */
2
+
3
+ declare function track(event: string): void;
4
+ declare function trackFeature(event: string): void;
5
+
6
+ declare const analytics: any;
7
+ declare const Button: any;
8
+ declare const TrackedButton: any;
9
+ declare const MyButton: any;
10
+
11
+ const eventName = "variable_event";
12
+
13
+ // ===== direct =====
14
+ track("direct_event");
15
+
16
+ // ===== multiline =====
17
+ track(
18
+ "multiline_event"
19
+ );
20
+
21
+ // ===== template string =====
22
+ track(`template_event`);
23
+
24
+ // ===== conditional =====
25
+ if (true) {
26
+ track("conditional_event");
27
+ }
28
+
29
+ // ===== ternary =====
30
+ true
31
+ ? track("ternary_a")
32
+ : track("ternary_b");
33
+
34
+ // ===== wrapper function =====
35
+ trackFeature("wrapper_function");
36
+
37
+ // ===== object function =====
38
+ analytics.track("object_track");
39
+
40
+ // ===== nested object =====
41
+ analytics.events.track("nested_track");
42
+
43
+ // ===== variable (optional support) =====
44
+ track(eventName);
45
+
46
+ // ===== function =====
47
+ function test() {
48
+ track("function_event");
49
+ }
50
+
51
+ // ===== arrow function =====
52
+ const run = () => {
53
+ track("arrow_event");
54
+ };
55
+
56
+ // ===== class =====
57
+ class TestService {
58
+ run() {
59
+ track("class_event");
60
+ }
61
+ }
62
+
63
+ // ===== component return =====
64
+ export default function Page() {
65
+ track("page_event");
66
+
67
+ return (
68
+ <>
69
+ {/* basic */}
70
+ <Button event="button_event" />
71
+
72
+ {/* wrapper component */}
73
+ <TrackedButton event="tracked_button" />
74
+
75
+ {/* another wrapper */}
76
+ <MyButton event="my_button" />
77
+
78
+ {/* expression */}
79
+ <Button event={"expression_event"} />
80
+
81
+ {/* nested */}
82
+ <div>
83
+ <Button event="nested_button" />
84
+ </div>
85
+
86
+ {/* conditional */}
87
+ {true && (
88
+ <Button event="conditional_button" />
89
+ )}
90
+
91
+ {/* ternary */}
92
+ {true
93
+ ? <Button event="ternary_a_button" />
94
+ : <Button event="ternary_b_button" />
95
+ }
96
+
97
+ {/* array */}
98
+ {[
99
+ <Button key="1" event="array_1" />,
100
+ <Button key="2" event="array_2" />
101
+ ]}
102
+ </>
103
+ );
104
+ }