@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 +1 -1
- package/src/analyze/analyzeJsFile.js +55 -41
- package/src/analyze/analyzeTsFile.js +56 -39
- package/src/repoDetails.js +23 -6
package/package.json
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
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
|
}
|
package/src/repoDetails.js
CHANGED
|
@@ -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
|
-
|
|
14
|
-
|
|
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
|
-
|
|
28
|
-
|
|
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
|
-
|
|
43
|
-
|
|
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
|
|