@flisk/analyze-tracking 0.7.1 → 0.7.3

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 (71) hide show
  1. package/README.md +35 -61
  2. package/bin/cli.js +1 -1
  3. package/package.json +18 -3
  4. package/src/analyze/go/astTraversal.js +121 -0
  5. package/src/analyze/go/constants.js +20 -0
  6. package/src/analyze/go/eventDeduplicator.js +47 -0
  7. package/src/analyze/go/eventExtractor.js +156 -0
  8. package/src/analyze/go/goAstParser/constants.js +39 -0
  9. package/src/analyze/go/goAstParser/expressionParser.js +281 -0
  10. package/src/analyze/go/goAstParser/index.js +52 -0
  11. package/src/analyze/go/goAstParser/statementParser.js +387 -0
  12. package/src/analyze/go/goAstParser/tokenizer.js +196 -0
  13. package/src/analyze/go/goAstParser/typeParser.js +202 -0
  14. package/src/analyze/go/goAstParser/utils.js +99 -0
  15. package/src/analyze/go/index.js +55 -0
  16. package/src/analyze/go/propertyExtractor.js +670 -0
  17. package/src/analyze/go/trackingDetector.js +71 -0
  18. package/src/analyze/go/trackingExtractor.js +54 -0
  19. package/src/analyze/go/typeContext.js +88 -0
  20. package/src/analyze/go/utils.js +215 -0
  21. package/src/analyze/index.js +11 -7
  22. package/src/analyze/javascript/constants.js +115 -0
  23. package/src/analyze/javascript/detectors/analytics-source.js +119 -0
  24. package/src/analyze/javascript/detectors/index.js +10 -0
  25. package/src/analyze/javascript/extractors/event-extractor.js +179 -0
  26. package/src/analyze/javascript/extractors/index.js +13 -0
  27. package/src/analyze/javascript/extractors/property-extractor.js +172 -0
  28. package/src/analyze/javascript/index.js +38 -0
  29. package/src/analyze/javascript/parser.js +126 -0
  30. package/src/analyze/javascript/utils/function-finder.js +123 -0
  31. package/src/analyze/python/index.js +111 -0
  32. package/src/analyze/python/pythonTrackingAnalyzer.py +814 -0
  33. package/src/analyze/ruby/detectors.js +46 -0
  34. package/src/analyze/ruby/extractors.js +258 -0
  35. package/src/analyze/ruby/index.js +51 -0
  36. package/src/analyze/ruby/traversal.js +123 -0
  37. package/src/analyze/ruby/types.js +30 -0
  38. package/src/analyze/ruby/visitor.js +66 -0
  39. package/src/analyze/typescript/constants.js +109 -0
  40. package/src/analyze/typescript/detectors/analytics-source.js +120 -0
  41. package/src/analyze/typescript/detectors/index.js +10 -0
  42. package/src/analyze/typescript/extractors/event-extractor.js +269 -0
  43. package/src/analyze/typescript/extractors/index.js +14 -0
  44. package/src/analyze/typescript/extractors/property-extractor.js +395 -0
  45. package/src/analyze/typescript/index.js +48 -0
  46. package/src/analyze/typescript/parser.js +131 -0
  47. package/src/analyze/typescript/utils/function-finder.js +114 -0
  48. package/src/analyze/typescript/utils/type-resolver.js +193 -0
  49. package/src/generateDescriptions/index.js +81 -0
  50. package/src/generateDescriptions/llmUtils.js +33 -0
  51. package/src/generateDescriptions/promptUtils.js +62 -0
  52. package/src/generateDescriptions/schemaUtils.js +61 -0
  53. package/src/index.js +7 -2
  54. package/src/{fileProcessor.js → utils/fileProcessor.js} +5 -0
  55. package/src/{repoDetails.js → utils/repoDetails.js} +5 -0
  56. package/src/{yamlGenerator.js → utils/yamlGenerator.js} +5 -0
  57. package/.github/workflows/npm-publish.yml +0 -33
  58. package/.github/workflows/pr-check.yml +0 -17
  59. package/jest.config.js +0 -7
  60. package/src/analyze/analyzeGoFile.js +0 -1164
  61. package/src/analyze/analyzeJsFile.js +0 -72
  62. package/src/analyze/analyzePythonFile.js +0 -41
  63. package/src/analyze/analyzeRubyFile.js +0 -409
  64. package/src/analyze/analyzeTsFile.js +0 -69
  65. package/src/analyze/go2json.js +0 -1069
  66. package/src/analyze/helpers.js +0 -217
  67. package/src/analyze/pythonTrackingAnalyzer.py +0 -439
  68. package/src/generateDescriptions.js +0 -196
  69. package/tests/detectSource.test.js +0 -20
  70. package/tests/extractProperties.test.js +0 -109
  71. package/tests/findWrappingFunction.test.js +0 -30
@@ -0,0 +1,111 @@
1
+ /**
2
+ * @fileoverview Python analytics tracking analyzer - main entry point
3
+ * @module analyze/python
4
+ */
5
+
6
+ const fs = require('fs');
7
+ const path = require('path');
8
+
9
+ // Singleton instance of Pyodide
10
+ let pyodide = null;
11
+
12
+ /**
13
+ * Initialize Pyodide runtime lazily
14
+ *
15
+ * This function loads Pyodide and required Python packages only when needed,
16
+ * improving startup performance when Python analysis is not immediately required.
17
+ *
18
+ * @returns {Promise<Object>} The initialized Pyodide instance
19
+ * @throws {Error} If Pyodide fails to load
20
+ */
21
+ async function initPyodide() {
22
+ if (!pyodide) {
23
+ try {
24
+ const { loadPyodide } = await import('pyodide');
25
+ pyodide = await loadPyodide();
26
+
27
+ // Pre-load required Python packages
28
+ await pyodide.loadPackagesFromImports('import ast, json');
29
+ } catch (error) {
30
+ throw new Error(`Failed to initialize Pyodide: ${error.message}`);
31
+ }
32
+ }
33
+ return pyodide;
34
+ }
35
+
36
+ /**
37
+ * Analyze a Python file for analytics tracking calls
38
+ *
39
+ * This function parses Python code and identifies analytics tracking calls from various
40
+ * libraries, extracting event names, properties, and metadata.
41
+ *
42
+ * @param {string} filePath - Path to the Python file to analyze
43
+ * @param {string} [customFunction=null] - Name of a custom tracking function to detect
44
+ * @returns {Promise<Array<Object>>} Array of tracking events found in the file
45
+ * @returns {Promise<Array>} Empty array if an error occurs
46
+ *
47
+ * @example
48
+ * const events = await analyzePythonFile('./app.py');
49
+ * // Returns: [{ eventName: 'User Signup', source: 'segment', properties: {...}, ... }]
50
+ *
51
+ * @example
52
+ * // With custom tracking function
53
+ * const events = await analyzePythonFile('./app.py', 'track_event');
54
+ */
55
+ async function analyzePythonFile(filePath, customFunction = null) {
56
+ // Validate inputs
57
+ if (!filePath || typeof filePath !== 'string') {
58
+ console.error('Invalid file path provided');
59
+ return [];
60
+ }
61
+
62
+ try {
63
+ // Check if file exists before reading
64
+ if (!fs.existsSync(filePath)) {
65
+ console.error(`File not found: ${filePath}`);
66
+ return [];
67
+ }
68
+
69
+ // Read the Python file
70
+ const code = fs.readFileSync(filePath, 'utf8');
71
+
72
+ // Initialize Pyodide if not already done
73
+ const py = await initPyodide();
74
+
75
+ // Load the Python analyzer code
76
+ const analyzerPath = path.join(__dirname, 'pythonTrackingAnalyzer.py');
77
+ if (!fs.existsSync(analyzerPath)) {
78
+ throw new Error(`Python analyzer not found at: ${analyzerPath}`);
79
+ }
80
+
81
+ const analyzerCode = fs.readFileSync(analyzerPath, 'utf8');
82
+
83
+ // Set up Python environment with necessary variables
84
+ py.globals.set('code', code);
85
+ py.globals.set('filepath', filePath);
86
+ py.globals.set('custom_function', customFunction);
87
+ // Set __name__ to null to prevent execution of main block
88
+ py.globals.set('__name__', null);
89
+
90
+ // Load and run the analyzer
91
+ py.runPython(analyzerCode);
92
+
93
+ // Execute the analysis and parse results
94
+ const result = py.runPython('analyze_python_code(code, filepath, custom_function)');
95
+ const events = JSON.parse(result);
96
+
97
+ return events;
98
+ } catch (error) {
99
+ // Log detailed error information for debugging
100
+ console.error(`Error analyzing Python file ${filePath}:`, error);
101
+ console.error('Stack trace:', error.stack);
102
+ return [];
103
+ }
104
+ }
105
+
106
+ // Export the public API
107
+ module.exports = {
108
+ analyzePythonFile,
109
+ // Export for testing purposes
110
+ _initPyodide: initPyodide
111
+ };