@eventra_dev/eventra-cli 0.0.5 → 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.
@@ -33,6 +33,9 @@ jobs:
33
33
  - name: Build CLI
34
34
  run: pnpm build
35
35
 
36
+ - name: Run tests
37
+ run: pnpm test
38
+
36
39
  - name: Publish to npm
37
40
  run: npm publish --access public
38
41
  env:
package/README.md CHANGED
@@ -7,6 +7,8 @@
7
7
  [![npm version](https://img.shields.io/npm/v/@eventra_dev/eventra-cli.svg)](https://www.npmjs.com/package/@eventra_dev/eventra-cli)
8
8
  [![npm downloads](https://img.shields.io/npm/dm/@eventra_dev/eventra-cli.svg)](https://www.npmjs.com/package/@eventra_dev/eventra-cli)
9
9
  [![TypeScript](https://img.shields.io/badge/typescript-ready-blue.svg)](https://www.typescriptlang.org/)
10
+ [![Tests](https://github.com/and-1991/eventra-cli/actions/workflows/release.yml/badge.svg)]()
11
+ [![License](https://img.shields.io/npm/l/@eventra_dev/eventra-cli)]()
10
12
 
11
13
  Eventra CLI automatically discovers feature usage events in your codebase and syncs them with Eventra.
12
14
 
@@ -29,7 +29,9 @@ async function init() {
29
29
  wrappers: [],
30
30
  functionWrappers: [],
31
31
  sync: {
32
- include: ["**/*.{ts,tsx,js,jsx}"],
32
+ include: [
33
+ "**/*.{ts,tsx,js,jsx,vue,svelte,astro}"
34
+ ],
33
35
  exclude: [
34
36
  "node_modules",
35
37
  "dist",
@@ -28,6 +28,11 @@ async function sync() {
28
28
  const files = await (0, fast_glob_1.default)(config.sync.include, {
29
29
  ignore: config.sync.exclude
30
30
  });
31
+ const functionWrappers = (config.functionWrappers ?? []).map((w) => ({
32
+ ...w,
33
+ path: w.path ?? "0"
34
+ }));
35
+ const componentWrappers = config.wrappers ?? [];
31
36
  for (const file of files) {
32
37
  const parser = (0, router_1.detectParser)(file);
33
38
  let content = await promises_1.default.readFile(file, "utf-8");
@@ -37,10 +42,13 @@ async function sync() {
37
42
  content = (0, svelte_1.parseSvelte)(content);
38
43
  if (parser === "astro")
39
44
  content = (0, astro_1.parseAstro)(content);
40
- const source = project.createSourceFile(file, content, { overwrite: true });
45
+ const virtualFile = parser === "ts"
46
+ ? file
47
+ : file + ".tsx";
48
+ const source = project.createSourceFile(virtualFile, content, { overwrite: true });
41
49
  (0, track_1.scanTrack)(source).forEach((e) => events.add(e));
42
- (0, function_wrappers_1.scanFunctionWrappers)(source, config.functionWrappers ?? []).forEach((e) => events.add(e));
43
- (0, component_wrappers_1.scanComponentWrappers)(source, config.wrappers ?? []).forEach((e) => events.add(e));
50
+ (0, function_wrappers_1.scanFunctionWrappers)(source, functionWrappers).forEach((e) => events.add(e));
51
+ (0, component_wrappers_1.scanComponentWrappers)(source, componentWrappers).forEach((e) => events.add(e));
44
52
  }
45
53
  const list = [...events].sort();
46
54
  config.events = list;
@@ -18,7 +18,7 @@ function normalizeConfig(config) {
18
18
  functionWrappers: config.functionWrappers ?? [],
19
19
  sync: config.sync ?? {
20
20
  include: [
21
- "**/*.{ts,tsx,js,jsx}"
21
+ "**/*.{ts,tsx,js,jsx,vue,svelte,astro}"
22
22
  ],
23
23
  exclude: [
24
24
  "node_modules",
@@ -7,23 +7,38 @@ function extractEvent(call, path) {
7
7
  let node = call.getArguments()[Number(parts[0])];
8
8
  if (!node)
9
9
  return null;
10
+ node = unwrap(node);
10
11
  for (let i = 1; i < parts.length; i++) {
11
- if (ts_morph_1.Node.isObjectLiteralExpression(node)) {
12
- const obj = node;
13
- const prop = obj.getProperty(parts[i]);
14
- if (!prop)
15
- return null;
16
- if (ts_morph_1.Node.isPropertyAssignment(prop)) {
17
- const initializer = prop.getInitializer();
18
- if (!initializer)
19
- return null;
20
- node = initializer;
21
- }
12
+ if (!ts_morph_1.Node.isObjectLiteralExpression(node)) {
13
+ return null;
22
14
  }
15
+ const obj = node;
16
+ const prop = obj.getProperty(parts[i]);
17
+ if (!prop)
18
+ return null;
19
+ if (!ts_morph_1.Node.isPropertyAssignment(prop)) {
20
+ return null;
21
+ }
22
+ const initializer = prop.getInitializer();
23
+ if (!initializer)
24
+ return null;
25
+ node = unwrap(initializer);
23
26
  }
24
- if (node &&
25
- ts_morph_1.Node.isStringLiteral(node)) {
27
+ if (ts_morph_1.Node.isStringLiteral(node)) {
26
28
  return node.getLiteralText();
27
29
  }
30
+ if (node.getKind() ===
31
+ ts_morph_1.SyntaxKind.NoSubstitutionTemplateLiteral) {
32
+ return node
33
+ .getText()
34
+ .replace(/`/g, "");
35
+ }
28
36
  return null;
29
37
  }
38
+ function unwrap(node) {
39
+ let current = node;
40
+ while (ts_morph_1.Node.isParenthesizedExpression(current)) {
41
+ current = current.getExpression();
42
+ }
43
+ return current;
44
+ }
@@ -4,7 +4,15 @@ exports.parseVue = parseVue;
4
4
  function parseVue(content) {
5
5
  const template = content.match(/<template[\s\S]*?>([\s\S]*?)<\/template>/);
6
6
  const script = content.match(/<script[\s\S]*?>([\s\S]*?)<\/script>/);
7
- return ((template?.[1] ?? "") +
8
- "\n" +
9
- (script?.[1] ?? ""));
7
+ return `
8
+ ${script?.[1] ?? ""}
9
+
10
+ function __vue_template__() {
11
+ return (
12
+ <>
13
+ ${template?.[1] ?? ""}
14
+ </>
15
+ );
16
+ }
17
+ `;
10
18
  }
@@ -32,13 +32,11 @@ function scanComponentWrappers(source, wrappers) {
32
32
  const init = attrNode.getInitializer();
33
33
  if (!init)
34
34
  continue;
35
- // event="signup"
36
35
  if (init.getKind() ===
37
36
  ts_morph_1.SyntaxKind.StringLiteral) {
38
37
  const value = init.asKindOrThrow(ts_morph_1.SyntaxKind.StringLiteral);
39
38
  events.add(value.getLiteralText());
40
39
  }
41
- // event={"signup"}
42
40
  if (init.getKind() ===
43
41
  ts_morph_1.SyntaxKind.JsxExpression) {
44
42
  const expr = init
@@ -7,29 +7,39 @@ function scanFunctionWrappers(source, wrappers) {
7
7
  const events = new Set();
8
8
  const calls = source.getDescendantsOfKind(ts_morph_1.SyntaxKind.CallExpression);
9
9
  for (const call of calls) {
10
- const expression = call.getExpression();
11
- let name = null;
12
- // trackFeature()
13
- if (expression.getKind() ===
14
- ts_morph_1.SyntaxKind.Identifier) {
15
- name =
16
- expression.getText();
17
- }
18
- // analytics.trackFeature()
19
- if (expression.getKind() ===
20
- ts_morph_1.SyntaxKind.PropertyAccessExpression) {
21
- const prop = expression.asKindOrThrow(ts_morph_1.SyntaxKind.PropertyAccessExpression);
22
- name = prop.getName();
23
- }
10
+ const name = getFunctionName(call);
24
11
  if (!name)
25
12
  continue;
26
13
  for (const wrapper of wrappers) {
27
- if (name !== wrapper.name)
14
+ if (wrapper.name !== name)
28
15
  continue;
29
- const event = (0, extract_1.extractEvent)(call, wrapper.path);
16
+ const event = (0, extract_1.extractEvent)(call, wrapper.path ?? "0");
30
17
  if (event)
31
18
  events.add(event);
32
19
  }
33
20
  }
34
21
  return events;
35
22
  }
23
+ function getFunctionName(call) {
24
+ const expression = call.getExpression();
25
+ // trackFeature()
26
+ if (ts_morph_1.Node.isIdentifier(expression)) {
27
+ return expression.getText();
28
+ }
29
+ // analytics.trackFeature()
30
+ if (ts_morph_1.Node.isPropertyAccessExpression(expression)) {
31
+ return getDeepName(expression);
32
+ }
33
+ return null;
34
+ }
35
+ function getDeepName(node) {
36
+ let current = node;
37
+ let name = "";
38
+ while (ts_morph_1.Node.isPropertyAccessExpression(current)) {
39
+ name =
40
+ current.getName();
41
+ current =
42
+ current.getExpression();
43
+ }
44
+ return name;
45
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eventra_dev/eventra-cli",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "Eventra CLI",
5
5
  "type": "commonjs",
6
6
  "bin": {
@@ -15,7 +15,8 @@
15
15
  "license": "MIT",
16
16
  "scripts": {
17
17
  "build": "tsc",
18
- "dev": "tsx src/index.ts"
18
+ "dev": "tsx src/index.ts",
19
+ "test": "tsx tests/run.ts"
19
20
  },
20
21
  "dependencies": {
21
22
  "chalk": "^4.1.2",
@@ -29,6 +30,7 @@
29
30
  "@types/fs-extra": "^11.0.4",
30
31
  "@types/inquirer": "^9.0.9",
31
32
  "@types/node": "^20.0.0",
33
+ "cross-spawn": "^7.0.6",
32
34
  "tsx": "^4.7.0",
33
35
  "typescript": "^5.3.0"
34
36
  }
@@ -41,7 +41,9 @@ export async function init() {
41
41
  wrappers: [],
42
42
  functionWrappers: [],
43
43
  sync: {
44
- include: ["**/*.{ts,tsx,js,jsx}"],
44
+ include: [
45
+ "**/*.{ts,tsx,js,jsx,vue,svelte,astro}"
46
+ ],
45
47
  exclude: [
46
48
  "node_modules",
47
49
  "dist",
@@ -41,6 +41,17 @@ export async function sync() {
41
41
  }
42
42
  );
43
43
 
44
+ const functionWrappers =
45
+ (config.functionWrappers ?? []).map(
46
+ (w) => ({
47
+ ...w,
48
+ path: w.path ?? "0"
49
+ })
50
+ );
51
+
52
+ const componentWrappers =
53
+ config.wrappers ?? [];
54
+
44
55
  for (const file of files) {
45
56
  const parser =
46
57
  detectParser(file);
@@ -60,9 +71,14 @@ export async function sync() {
60
71
  if (parser === "astro")
61
72
  content = parseAstro(content);
62
73
 
74
+ const virtualFile =
75
+ parser === "ts"
76
+ ? file
77
+ : file + ".tsx";
78
+
63
79
  const source =
64
80
  project.createSourceFile(
65
- file,
81
+ virtualFile,
66
82
  content,
67
83
  { overwrite: true }
68
84
  );
@@ -71,16 +87,17 @@ export async function sync() {
71
87
  (e) => events.add(e)
72
88
  );
73
89
 
90
+
74
91
  scanFunctionWrappers(
75
92
  source,
76
- config.functionWrappers ?? []
93
+ functionWrappers
77
94
  ).forEach((e) =>
78
95
  events.add(e)
79
96
  );
80
97
 
81
98
  scanComponentWrappers(
82
99
  source,
83
- config.wrappers ?? []
100
+ componentWrappers
84
101
  ).forEach((e) =>
85
102
  events.add(e)
86
103
  );
package/src/types.ts CHANGED
@@ -5,7 +5,7 @@ export type ComponentWrapper = {
5
5
 
6
6
  export type FunctionWrapper = {
7
7
  name: string;
8
- path: string;
8
+ path?: string;
9
9
  };
10
10
 
11
11
  export type EventraConfig = {
@@ -15,7 +15,7 @@ export function normalizeConfig(
15
15
  config.functionWrappers ?? [],
16
16
  sync: config.sync ?? {
17
17
  include: [
18
- "**/*.{ts,tsx,js,jsx}"
18
+ "**/*.{ts,tsx,js,jsx,vue,svelte,astro}"
19
19
  ],
20
20
  exclude: [
21
21
  "node_modules",
@@ -2,6 +2,7 @@ import {
2
2
  CallExpression,
3
3
  Node,
4
4
  ObjectLiteralExpression,
5
+ SyntaxKind,
5
6
  } from "ts-morph";
6
7
 
7
8
  export function extractEvent(
@@ -15,37 +16,59 @@ export function extractEvent(
15
16
 
16
17
  if (!node) return null;
17
18
 
18
- for (let i = 1; i < parts.length; i++) {
19
- if (
20
- Node.isObjectLiteralExpression(node)
21
- ) {
22
- const obj: ObjectLiteralExpression =
23
- node;
19
+ node = unwrap(node);
24
20
 
25
- const prop =
26
- obj.getProperty(parts[i]);
21
+ for (let i = 1; i < parts.length; i++) {
22
+ if (!Node.isObjectLiteralExpression(node)) {
23
+ return null;
24
+ }
27
25
 
28
- if (!prop) return null;
26
+ const obj: ObjectLiteralExpression =
27
+ node;
29
28
 
30
- if (
31
- Node.isPropertyAssignment(prop)
32
- ) {
33
- const initializer =
34
- prop.getInitializer();
29
+ const prop =
30
+ obj.getProperty(parts[i]);
35
31
 
36
- if (!initializer) return null;
32
+ if (!prop) return null;
37
33
 
38
- node = initializer;
39
- }
34
+ if (!Node.isPropertyAssignment(prop)) {
35
+ return null;
40
36
  }
37
+
38
+ const initializer =
39
+ prop.getInitializer();
40
+
41
+ if (!initializer) return null;
42
+
43
+ node = unwrap(initializer);
44
+ }
45
+
46
+ if (Node.isStringLiteral(node)) {
47
+ return node.getLiteralText();
41
48
  }
42
49
 
43
50
  if (
44
- node &&
45
- Node.isStringLiteral(node)
51
+ node.getKind() ===
52
+ SyntaxKind.NoSubstitutionTemplateLiteral
46
53
  ) {
47
- return node.getLiteralText();
54
+ return node
55
+ .getText()
56
+ .replace(/`/g, "");
48
57
  }
49
58
 
50
59
  return null;
51
60
  }
61
+
62
+ function unwrap(node: Node): Node {
63
+ let current = node;
64
+
65
+ while (
66
+ Node.isParenthesizedExpression(
67
+ current
68
+ )
69
+ ) {
70
+ current = current.getExpression();
71
+ }
72
+
73
+ return current;
74
+ }
@@ -11,9 +11,15 @@ export function parseVue(
11
11
  /<script[\s\S]*?>([\s\S]*?)<\/script>/
12
12
  );
13
13
 
14
+ return `
15
+ ${script?.[1] ?? ""}
16
+
17
+ function __vue_template__() {
14
18
  return (
15
- (template?.[1] ?? "") +
16
- "\n" +
17
- (script?.[1] ?? "")
19
+ <>
20
+ ${template?.[1] ?? ""}
21
+ </>
18
22
  );
19
23
  }
24
+ `;
25
+ }
@@ -61,7 +61,6 @@ export function scanComponentWrappers(
61
61
 
62
62
  if (!init) continue;
63
63
 
64
- // event="signup"
65
64
  if (
66
65
  init.getKind() ===
67
66
  SyntaxKind.StringLiteral
@@ -76,7 +75,6 @@ export function scanComponentWrappers(
76
75
  );
77
76
  }
78
77
 
79
- // event={"signup"}
80
78
  if (
81
79
  init.getKind() ===
82
80
  SyntaxKind.JsxExpression
@@ -1,6 +1,9 @@
1
1
  import {
2
2
  SourceFile,
3
3
  SyntaxKind,
4
+ Node,
5
+ CallExpression,
6
+ PropertyAccessExpression,
4
7
  } from "ts-morph";
5
8
 
6
9
  import { extractEvent } from "../extract";
@@ -10,8 +13,7 @@ export function scanFunctionWrappers(
10
13
  source: SourceFile,
11
14
  wrappers: FunctionWrapper[]
12
15
  ) {
13
- const events =
14
- new Set<string>();
16
+ const events = new Set<string>();
15
17
 
16
18
  const calls =
17
19
  source.getDescendantsOfKind(
@@ -19,43 +21,19 @@ export function scanFunctionWrappers(
19
21
  );
20
22
 
21
23
  for (const call of calls) {
22
- const expression =
23
- call.getExpression();
24
-
25
- let name: string | null = null;
26
-
27
- // trackFeature()
28
- if (
29
- expression.getKind() ===
30
- SyntaxKind.Identifier
31
- ) {
32
- name =
33
- expression.getText();
34
- }
35
-
36
- // analytics.trackFeature()
37
- if (
38
- expression.getKind() ===
39
- SyntaxKind.PropertyAccessExpression
40
- ) {
41
- const prop =
42
- expression.asKindOrThrow(
43
- SyntaxKind.PropertyAccessExpression
44
- );
45
-
46
- name = prop.getName();
47
- }
24
+ const name =
25
+ getFunctionName(call);
48
26
 
49
27
  if (!name) continue;
50
28
 
51
29
  for (const wrapper of wrappers) {
52
- if (name !== wrapper.name)
30
+ if (wrapper.name !== name)
53
31
  continue;
54
32
 
55
33
  const event =
56
34
  extractEvent(
57
35
  call,
58
- wrapper.path
36
+ wrapper.path ?? "0"
59
37
  );
60
38
 
61
39
  if (event)
@@ -65,3 +43,56 @@ export function scanFunctionWrappers(
65
43
 
66
44
  return events;
67
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,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
+ }
@@ -0,0 +1,83 @@
1
+ <script setup lang="ts">
2
+ /* eslint-disable */
3
+
4
+ declare function track(event: string): void
5
+ declare function trackFeature(event: string): void
6
+
7
+ declare const analytics: any
8
+
9
+ const eventName = "variable_event"
10
+
11
+ // ===== direct =====
12
+ track("vue_event")
13
+
14
+ // ===== multiline =====
15
+ track(
16
+ "multiline_event"
17
+ )
18
+
19
+ // ===== template string =====
20
+ track(`template_event`)
21
+
22
+ // ===== wrapper function =====
23
+ trackFeature("wrapper_function")
24
+
25
+ // ===== object =====
26
+ analytics.track("object_track")
27
+
28
+ // ===== nested object =====
29
+ analytics.events.track("nested_track")
30
+
31
+ // ===== variable =====
32
+ track(eventName)
33
+
34
+ // ===== conditional =====
35
+ if (true) {
36
+ track("conditional_event")
37
+ }
38
+
39
+ // ===== ternary =====
40
+ true
41
+ ? track("ternary_a")
42
+ : track("ternary_b")
43
+
44
+ // ===== function =====
45
+ function test() {
46
+ track("function_event")
47
+ }
48
+
49
+ // ===== arrow =====
50
+ const run = () => {
51
+ track("arrow_event")
52
+ }
53
+
54
+ // ===== class =====
55
+ class Service {
56
+ run() {
57
+ track("class_event")
58
+ }
59
+ }
60
+ </script>
61
+
62
+ <template>
63
+ <!-- basic -->
64
+ <Button event="vue_button" />
65
+
66
+ <!-- expression -->
67
+ <Button event="expression_event" />
68
+
69
+ <!-- nested -->
70
+ <div>
71
+ <Button event="nested_button" />
72
+ </div>
73
+
74
+ <!-- conditional -->
75
+ <Button
76
+ v-if="true"
77
+ event="conditional_button"
78
+ />
79
+
80
+ <!-- array -->
81
+ <Button event="array_1" />
82
+ <Button event="array_2" />
83
+ </template>
@@ -0,0 +1,62 @@
1
+ /* eslint-disable */
2
+
3
+ declare const Button: any;
4
+ declare const TrackedButton: any;
5
+ declare const MyButton: any;
6
+
7
+ const eventName = "variable_event";
8
+
9
+ export default function WrapperTest() {
10
+ return (
11
+ <>
12
+ {/* basic */}
13
+ <Button event="wrapper_button" />
14
+
15
+ {/* expression */}
16
+ <Button event={"expression_button"} />
17
+
18
+ {/* nested */}
19
+ <div>
20
+ <Button event="nested_button" />
21
+ </div>
22
+
23
+ {/* fragment */}
24
+ <>
25
+ <Button event="fragment_button" />
26
+ </>
27
+
28
+ {/* conditional */}
29
+ {true && (
30
+ <Button event="conditional_button" />
31
+ )}
32
+
33
+ {/* ternary */}
34
+ {true
35
+ ? <Button event="ternary_a_button" />
36
+ : <Button event="ternary_b_button" />
37
+ }
38
+
39
+ {/* array */}
40
+ {[
41
+ <Button key="1" event="array_1" />,
42
+ <Button key="2" event="array_2" />
43
+ ]}
44
+
45
+ {/* wrapper components */}
46
+ <TrackedButton event="tracked_button" />
47
+ <MyButton event="my_button" />
48
+
49
+ {/* multiline */}
50
+ <Button
51
+ event="multiline_button"
52
+ />
53
+
54
+ {/* deep nested */}
55
+ <div>
56
+ <section>
57
+ <Button event="deep_nested" />
58
+ </section>
59
+ </div>
60
+ </>
61
+ );
62
+ }
@@ -0,0 +1,60 @@
1
+ /* eslint-disable */
2
+
3
+ declare function trackFeature(event: string): void;
4
+ declare const analytics: any;
5
+
6
+ const eventName = "variable_event";
7
+
8
+ // ===== basic =====
9
+ trackFeature("wrapper_function");
10
+
11
+ // ===== multiline =====
12
+ trackFeature(
13
+ "multiline_function"
14
+ );
15
+
16
+ // ===== template string =====
17
+ trackFeature(`template_function`);
18
+
19
+ // ===== conditional =====
20
+ if (true) {
21
+ trackFeature("conditional_function");
22
+ }
23
+
24
+ // ===== ternary =====
25
+ true
26
+ ? trackFeature("ternary_a")
27
+ : trackFeature("ternary_b");
28
+
29
+ // ===== variable =====
30
+ trackFeature(eventName);
31
+
32
+ // ===== function =====
33
+ function run() {
34
+ trackFeature("function_event");
35
+ }
36
+
37
+ // ===== arrow =====
38
+ const test = () => {
39
+ trackFeature("arrow_event");
40
+ };
41
+
42
+ // ===== class =====
43
+ class Service {
44
+ run() {
45
+ trackFeature("class_event");
46
+ }
47
+ }
48
+
49
+ // ===== nested function =====
50
+ function outer() {
51
+ function inner() {
52
+ trackFeature("nested_function");
53
+ }
54
+ }
55
+
56
+ // ===== object wrapper =====
57
+ analytics.trackFeature("object_wrapper");
58
+
59
+ // ===== nested object wrapper =====
60
+ analytics.events.trackFeature("nested_object_wrapper");
package/tests/run.ts ADDED
@@ -0,0 +1,120 @@
1
+ import { spawnSync } from "child_process";
2
+ import * as path from "path";
3
+ import * as fs from "fs";
4
+
5
+ const ROOT = path.resolve(__dirname, "..");
6
+ const CLI = path.resolve(ROOT, "dist/index.js");
7
+
8
+ const fixtures = [
9
+ "frontend/react",
10
+ "frontend/vue",
11
+ "frontend/next",
12
+
13
+ "backend/node",
14
+ "backend/express",
15
+ "backend/nest",
16
+
17
+ "wrappers/component",
18
+ "wrappers/function"
19
+ ];
20
+
21
+ function runCLI(args: string[], cwd: string) {
22
+ const result = spawnSync(
23
+ process.execPath,
24
+ [CLI, ...args],
25
+ {
26
+ cwd,
27
+ encoding: "utf-8",
28
+ input: "\n"
29
+ }
30
+ );
31
+
32
+ return (result.stdout ?? "") + (result.stderr ?? "");
33
+ }
34
+
35
+ function ensureTestConfig(dir: string) {
36
+ const configPath = path.join(dir, "eventra.json");
37
+
38
+ if (fs.existsSync(configPath)) return;
39
+
40
+ const config = {
41
+ apiKey: "",
42
+ events: [],
43
+ wrappers: [
44
+ {
45
+ name: "Button",
46
+ prop: "event"
47
+ }
48
+ ],
49
+ functionWrappers: [
50
+ {
51
+ name: "trackFeature",
52
+ path: "0"
53
+ }
54
+ ],
55
+ sync: {
56
+ include: [
57
+ "**/*.{ts,tsx,js,jsx,vue,svelte,astro}"
58
+ ],
59
+ exclude: [
60
+ "node_modules",
61
+ "dist",
62
+ ".next",
63
+ ".git"
64
+ ]
65
+ }
66
+ };
67
+
68
+ fs.writeFileSync(
69
+ configPath,
70
+ JSON.stringify(config, null, 2)
71
+ );
72
+ }
73
+
74
+ function cleanup(dir: string) {
75
+ const configPath = path.join(dir, "eventra.json");
76
+
77
+ if (fs.existsSync(configPath)) {
78
+ fs.unlinkSync(configPath);
79
+ }
80
+ }
81
+
82
+ function runFixture(name: string) {
83
+ const dir = path.resolve(
84
+ __dirname,
85
+ "fixtures",
86
+ name
87
+ );
88
+
89
+ console.log(`\nRunning: ${name}`);
90
+
91
+ ensureTestConfig(dir);
92
+
93
+ runCLI(["init"], dir);
94
+
95
+ const output = runCLI(["sync"], dir);
96
+
97
+ const match =
98
+ output.match(/Found (\d+) events/);
99
+
100
+ const count = match ? match[1] : "0";
101
+
102
+ console.log(
103
+ `✓ ${name} OK (${count} events)`
104
+ );
105
+
106
+ // cleanup
107
+ cleanup(dir);
108
+ }
109
+
110
+ function run() {
111
+ for (const fixture of fixtures) {
112
+ runFixture(fixture);
113
+ }
114
+
115
+ console.log(
116
+ "\nAll fixtures passed"
117
+ );
118
+ }
119
+
120
+ run();