@flisk/analyze-tracking 0.2.8 → 0.2.9

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flisk/analyze-tracking",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "description": "Analyzes tracking code in a project and generates data schemas",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -10,47 +10,61 @@ const parserOptions = { ecmaVersion: 'latest', sourceType: 'module', locations:
10
10
  extend(walk.base);
11
11
 
12
12
  function analyzeJsFile(filePath, customFunction) {
13
- const code = fs.readFileSync(filePath, 'utf8');
14
- const ast = parser.parse(code, parserOptions);
15
- const events = [];
16
-
17
- walk.ancestor(ast, {
18
- CallExpression(node, ancestors) {
19
- const source = detectSourceJs(node, customFunction);
20
- if (source === 'unknown') return;
21
-
22
- let eventName = null;
23
- let propertiesNode = null;
24
-
25
- if (source === 'googleanalytics' && node.arguments.length >= 3) {
26
- eventName = node.arguments[1]?.value || null;
27
- propertiesNode = node.arguments[2];
28
- } else if (source === 'snowplow' && node.arguments.length >= 2) {
29
- const actionProperty = node.arguments[1].properties.find(prop => prop.key.name === 'action');
30
- eventName = actionProperty ? actionProperty.value.value : null;
31
- propertiesNode = node.arguments[1];
32
- } else if (node.arguments.length >= 2) {
33
- eventName = node.arguments[0]?.value || null;
34
- propertiesNode = node.arguments[1];
35
- }
36
-
37
- const line = node.loc.start.line;
38
- const functionName = findWrappingFunctionJs(node, ancestors);
39
-
40
- if (eventName && propertiesNode && propertiesNode.type === 'ObjectExpression') {
41
- const properties = extractJsProperties(propertiesNode);
42
-
43
- events.push({
44
- eventName,
45
- source,
46
- properties,
47
- filePath,
48
- line,
49
- functionName
50
- });
51
- }
52
- },
53
- });
13
+ let events = [];
14
+ try {
15
+ const code = fs.readFileSync(filePath, 'utf8');
16
+ let ast;
17
+ try {
18
+ ast = parser.parse(code, parserOptions);
19
+ } catch (parseError) {
20
+ console.error(`Error parsing file ${filePath}`);
21
+ return events; // Return empty events array if parsing fails
22
+ }
23
+
24
+ walk.ancestor(ast, {
25
+ CallExpression(node, ancestors) {
26
+ try {
27
+ const source = detectSourceJs(node, customFunction);
28
+ if (source === 'unknown') return;
29
+
30
+ let eventName = null;
31
+ let propertiesNode = null;
32
+
33
+ if (source === 'googleanalytics' && node.arguments.length >= 3) {
34
+ eventName = node.arguments[1]?.value || null;
35
+ propertiesNode = node.arguments[2];
36
+ } else if (source === 'snowplow' && node.arguments.length >= 2) {
37
+ const actionProperty = node.arguments[1].properties.find(prop => prop.key.name === 'action');
38
+ eventName = actionProperty ? actionProperty.value.value : null;
39
+ propertiesNode = node.arguments[1];
40
+ } else if (node.arguments.length >= 2) {
41
+ eventName = node.arguments[0]?.value || null;
42
+ propertiesNode = node.arguments[1];
43
+ }
44
+
45
+ const line = node.loc.start.line;
46
+ const functionName = findWrappingFunctionJs(node, ancestors);
47
+
48
+ if (eventName && propertiesNode && propertiesNode.type === 'ObjectExpression') {
49
+ const properties = extractJsProperties(propertiesNode);
50
+
51
+ events.push({
52
+ eventName,
53
+ source,
54
+ properties,
55
+ filePath,
56
+ line,
57
+ functionName
58
+ });
59
+ }
60
+ } catch (nodeError) {
61
+ console.error(`Error processing node in ${filePath}`);
62
+ }
63
+ },
64
+ });
65
+ } catch (fileError) {
66
+ console.error(`Error reading or processing file ${filePath}`);
67
+ }
54
68
 
55
69
  return events;
56
70
  }
@@ -2,49 +2,66 @@ const ts = require('typescript');
2
2
  const { detectSourceTs, findWrappingFunctionTs, extractTsProperties } = require('./helpers');
3
3
 
4
4
  function analyzeTsFile(filePath, program, customFunction) {
5
- const sourceFile = program.getSourceFile(filePath);
6
- const checker = program.getTypeChecker();
7
- const events = [];
8
-
9
- function visit(node) {
10
- if (ts.isCallExpression(node)) {
11
- const source = detectSourceTs(node, customFunction);
12
- if (source === 'unknown') return;
13
-
14
- let eventName = null;
15
- let propertiesNode = null;
16
-
17
- if (source === 'googleanalytics' && node.arguments.length >= 3) {
18
- eventName = node.arguments[1]?.text || null;
19
- propertiesNode = node.arguments[2];
20
- } else if (source === 'snowplow' && node.arguments.length >= 2) {
21
- const actionProperty = node.arguments[1].properties.find(prop => prop.name.escapedText === 'action');
22
- eventName = actionProperty ? actionProperty.initializer.text : null;
23
- propertiesNode = node.arguments[1];
24
- } else if (node.arguments.length >= 2) {
25
- eventName = node.arguments[0]?.text || null;
26
- propertiesNode = node.arguments[1];
27
- }
5
+ let events = [];
6
+ try {
7
+ const sourceFile = program.getSourceFile(filePath);
8
+ if (!sourceFile) {
9
+ console.error(`Error: Unable to get source file for ${filePath}`);
10
+ return events;
11
+ }
12
+
13
+ const checker = program.getTypeChecker();
14
+
15
+ function visit(node) {
16
+ try {
17
+ if (ts.isCallExpression(node)) {
18
+ const source = detectSourceTs(node, customFunction);
19
+ if (source === 'unknown') return;
20
+
21
+ let eventName = null;
22
+ let propertiesNode = null;
28
23
 
29
- const line = sourceFile.getLineAndCharacterOfPosition(node.getStart()).line + 1;
30
- const functionName = findWrappingFunctionTs(node);
31
-
32
- if (eventName && propertiesNode && ts.isObjectLiteralExpression(propertiesNode)) {
33
- const properties = extractTsProperties(checker, propertiesNode);
34
- events.push({
35
- eventName,
36
- source,
37
- properties,
38
- filePath,
39
- line,
40
- functionName
41
- });
24
+ if (source === 'googleanalytics' && node.arguments.length >= 3) {
25
+ eventName = node.arguments[1]?.text || null;
26
+ propertiesNode = node.arguments[2];
27
+ } else if (source === 'snowplow' && node.arguments.length >= 2) {
28
+ const actionProperty = node.arguments[1].properties.find(prop => prop.name.escapedText === 'action');
29
+ eventName = actionProperty ? actionProperty.initializer.text : null;
30
+ propertiesNode = node.arguments[1];
31
+ } else if (node.arguments.length >= 2) {
32
+ eventName = node.arguments[0]?.text || null;
33
+ propertiesNode = node.arguments[1];
34
+ }
35
+
36
+ const line = sourceFile.getLineAndCharacterOfPosition(node.getStart()).line + 1;
37
+ const functionName = findWrappingFunctionTs(node);
38
+
39
+ if (eventName && propertiesNode && ts.isObjectLiteralExpression(propertiesNode)) {
40
+ try {
41
+ const properties = extractTsProperties(checker, propertiesNode);
42
+ events.push({
43
+ eventName,
44
+ source,
45
+ properties,
46
+ filePath,
47
+ line,
48
+ functionName
49
+ });
50
+ } catch (propertyError) {
51
+ console.error(`Error extracting properties in ${filePath} at line ${line}`);
52
+ }
53
+ }
54
+ }
55
+ ts.forEachChild(node, visit);
56
+ } catch (nodeError) {
57
+ console.error(`Error processing node in ${filePath}`);
42
58
  }
43
59
  }
44
- ts.forEachChild(node, visit);
45
- }
46
60
 
47
- ts.forEachChild(sourceFile, visit);
61
+ ts.forEachChild(sourceFile, visit);
62
+ } catch (fileError) {
63
+ console.error(`Error analyzing TypeScript file ${filePath}`);
64
+ }
48
65
 
49
66
  return events;
50
67
  }
@@ -1,5 +1,6 @@
1
1
  const fs = require('fs');
2
2
  const git = require('isomorphic-git');
3
+ const { execSync } = require('child_process');
3
4
 
4
5
  async function getRepositoryUrl(targetDir) {
5
6
  try {
@@ -10,8 +11,13 @@ async function getRepositoryUrl(targetDir) {
10
11
  });
11
12
  return repoUrl.trim();
12
13
  } catch (error) {
13
- console.warn('Could not determine repository URL. Will exclude.');
14
- return null;
14
+ try {
15
+ const repoUrl = execSync('git config --get remote.origin.url', { cwd: targetDir, encoding: 'utf8' });
16
+ return repoUrl.trim();
17
+ } catch (error) {
18
+ console.warn('Could not determine repository URL. Will exclude.');
19
+ return null;
20
+ }
15
21
  }
16
22
  }
17
23
 
@@ -24,8 +30,13 @@ async function getCommitHash(targetDir) {
24
30
  });
25
31
  return commitHash.trim();
26
32
  } catch (error) {
27
- console.warn('Could not determine latest commit hash. Will exclude.');
28
- return null;
33
+ try {
34
+ const commitHash = execSync('git rev-parse HEAD', { cwd: targetDir, encoding: 'utf8' });
35
+ return commitHash.trim();
36
+ } catch (error) {
37
+ console.warn('Could not determine latest commit hash. Will exclude.');
38
+ return null;
39
+ }
29
40
  }
30
41
  }
31
42
 
@@ -39,8 +50,14 @@ async function getCommitTimestamp(targetDir, commitHash) {
39
50
  const unixTimeSeconds = commit.committer.timestamp;
40
51
  return new Date(unixTimeSeconds * 1000);
41
52
  } catch (error) {
42
- console.warn('Could not retrieve commit timestamp. Using current timestamp as default.');
43
- return new Date();
53
+ try {
54
+ const commitTimestamp = execSync(`git --no-pager show -s --format=%ct ${commitHash}`, { cwd: targetDir, encoding: 'utf8' });
55
+ const unixTimeSeconds = commitTimestamp.trim();
56
+ return new Date(unixTimeSeconds * 1000);
57
+ } catch (error) {
58
+ console.warn('Could not retrieve commit timestamp. Using current timestamp as default.');
59
+ return new Date();
60
+ }
44
61
  }
45
62
  }
46
63