@flisk/analyze-tracking 0.2.4 → 0.2.5

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.4",
3
+ "version": "0.2.5",
4
4
  "description": "Analyzes tracking code in a project and generates data schemas",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -26,6 +26,7 @@
26
26
  "acorn-jsx-walk": "^2.0.0",
27
27
  "acorn-walk": "^8.3.3",
28
28
  "command-line-args": "^6.0.0",
29
+ "isomorphic-git": "^1.27.1",
29
30
  "js-yaml": "^4.1.0",
30
31
  "typescript": "^5.5.4"
31
32
  },
package/src/index.js CHANGED
@@ -2,9 +2,9 @@ const { analyzeDirectory } = require('./analyze');
2
2
  const { getRepoDetails } = require('./repoDetails');
3
3
  const { generateYamlSchema } = require('./yamlGenerator');
4
4
 
5
- function run(targetDir, outputPath, customFunction) {
5
+ async function run(targetDir, outputPath, customFunction) {
6
6
  const events = analyzeDirectory(targetDir, customFunction);
7
- const repoDetails = getRepoDetails(targetDir);
7
+ const repoDetails = await getRepoDetails(targetDir);
8
8
  generateYamlSchema(events, repoDetails, outputPath);
9
9
  }
10
10
 
@@ -1,8 +1,13 @@
1
- const { execSync } = require('child_process');
1
+ const fs = require('fs');
2
+ const git = require('isomorphic-git');
2
3
 
3
- function getRepositoryUrl(targetDir) {
4
+ async function getRepositoryUrl(targetDir) {
4
5
  try {
5
- const repoUrl = execSync('git config --get remote.origin.url', { cwd: targetDir, encoding: 'utf8' });
6
+ const repoUrl = await git.getConfig({
7
+ fs,
8
+ dir: targetDir,
9
+ path: 'remote.origin.url',
10
+ });
6
11
  return repoUrl.trim();
7
12
  } catch (error) {
8
13
  console.warn('Could not determine repository URL. Will exclude.');
@@ -10,9 +15,13 @@ function getRepositoryUrl(targetDir) {
10
15
  }
11
16
  }
12
17
 
13
- function getCommitHash(targetDir) {
18
+ async function getCommitHash(targetDir) {
14
19
  try {
15
- const commitHash = execSync('git rev-parse HEAD', { cwd: targetDir, encoding: 'utf8' });
20
+ const commitHash = await git.resolveRef({
21
+ fs,
22
+ dir: targetDir,
23
+ ref: 'HEAD',
24
+ });
16
25
  return commitHash.trim();
17
26
  } catch (error) {
18
27
  console.warn('Could not determine latest commit hash. Will exclude.');
@@ -20,13 +29,17 @@ function getCommitHash(targetDir) {
20
29
  }
21
30
  }
22
31
 
23
- function getCommitTimestamp(targetDir, commitHash) {
32
+ async function getCommitTimestamp(targetDir, commitHash) {
24
33
  try {
25
- const commitTimestamp = execSync(`git --no-pager show -s --format=%ct ${commitHash}`, { cwd: targetDir, encoding: 'utf8' });
26
- const unixTimeSeconds = commitTimestamp.trim();
34
+ const { commit } = await git.readCommit({
35
+ fs,
36
+ dir: targetDir,
37
+ oid: commitHash,
38
+ });
39
+ const unixTimeSeconds = commit.committer.timestamp;
27
40
  return new Date(unixTimeSeconds * 1000);
28
41
  } catch (error) {
29
- console.warn('Could not retrieve commit timestamp. Using current timestamp as default.')
42
+ console.warn('Could not retrieve commit timestamp. Using current timestamp as default.');
30
43
  return new Date();
31
44
  }
32
45
  }
@@ -41,20 +54,19 @@ function toISODateString(date) {
41
54
  + pad(date.getUTCDate())+'T'
42
55
  + pad(date.getUTCHours())+':'
43
56
  + pad(date.getUTCMinutes())+':'
44
- + pad(date.getUTCSeconds())+'Z'
57
+ + pad(date.getUTCSeconds())+'Z';
45
58
  }
46
59
 
47
- function getRepoDetails(targetDir) {
48
- const repoUrl = getRepositoryUrl(targetDir);
49
- const commitHash = getCommitHash(targetDir);
50
- const commitEpochTime = getCommitTimestamp(targetDir, commitHash);
60
+ async function getRepoDetails(targetDir) {
61
+ const repoUrl = await getRepositoryUrl(targetDir);
62
+ const commitHash = await getCommitHash(targetDir);
63
+ const commitEpochTime = await getCommitTimestamp(targetDir, commitHash);
51
64
  const commitTimestamp = toISODateString(commitEpochTime);
52
65
 
53
66
  const repoDetails = {};
54
67
  if (!!repoUrl) repoDetails.repository = repoUrl;
55
68
  if (!!commitHash) repoDetails.commit = commitHash;
56
- repoDetails.timestamp = commitTimestamp
57
-
69
+ repoDetails.timestamp = commitTimestamp;
58
70
  return repoDetails;
59
71
  }
60
72