aicp-tracker 1.1.4 → 1.1.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/daemon.js +22 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aicp-tracker",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "description": "AI Code Pulse — Claude Code usage tracker for JIRA cost attribution",
5
5
  "main": "src/daemon.js",
6
6
  "bin": {
package/src/daemon.js CHANGED
@@ -6,15 +6,28 @@ const { findJsonlFiles } = require('./log-scanner');
6
6
  const { parseNewLines } = require('./log-parser');
7
7
  const wal = require('./wal');
8
8
  const { sendPending } = require('./sender');
9
- const config = require('./config');
10
- const { CONFIG_DIR } = config;
9
+ const { CONFIG_DIR } = require('./config');
11
10
 
12
11
  const PID_FILE = path.join(CONFIG_DIR, 'daemon.pid');
13
12
 
13
+ // When installed locally: __dirname = <project>/node_modules/aicp-tracker/src
14
+ // Resolve three levels up to get the host project root, then verify it has a
15
+ // package.json that isn't ours (guards against global/npx installs).
16
+ function detectProjectPath() {
17
+ const candidate = path.resolve(__dirname, '..', '..', '..');
18
+ const hostPkg = path.join(candidate, 'package.json');
19
+ try {
20
+ const pkg = JSON.parse(fs.readFileSync(hostPkg, 'utf8'));
21
+ if (pkg.name !== 'aicp-tracker') return candidate;
22
+ } catch {}
23
+ // Global install or npx — fall back to cwd at start time
24
+ return process.cwd();
25
+ }
26
+
27
+ const PROJECT_PATH = detectProjectPath();
28
+
14
29
  async function tick() {
15
- const cfg = (() => { try { return config.load(); } catch { return null; } })();
16
- const projectPath = cfg?.projectPath || null;
17
- const files = findJsonlFiles(projectPath);
30
+ const files = findJsonlFiles(PROJECT_PATH);
18
31
  let total = 0;
19
32
  for (const f of files) {
20
33
  const records = parseNewLines(f);
@@ -31,31 +44,23 @@ function readPid() {
31
44
  try { return parseInt(fs.readFileSync(PID_FILE, 'utf8'), 10); } catch { return null; }
32
45
  }
33
46
 
34
- function saveProjectPath() {
35
- const cfg = config.load() || {};
36
- config.save({ ...cfg, projectPath: process.cwd() });
37
- }
38
-
39
47
  function start() {
40
- saveProjectPath();
41
-
42
48
  const existingPid = readPid();
43
49
  if (existingPid) {
44
50
  try {
45
- process.kill(existingPid, 0); // check if running
46
- console.log(`[daemon] Already running (pid ${existingPid}), tracking: ${process.cwd()}`);
51
+ process.kill(existingPid, 0);
52
+ console.log(`[daemon] Already running (pid ${existingPid}), tracking: ${PROJECT_PATH}`);
47
53
  return;
48
54
  } catch {}
49
55
  }
50
56
 
51
- // Detach and run as background process
52
57
  const child = require('child_process').spawn(
53
58
  process.execPath,
54
59
  [path.join(__dirname, 'daemon-worker.js')],
55
60
  { detached: true, stdio: 'ignore' }
56
61
  );
57
62
  child.unref();
58
- console.log(`[daemon] Started (pid ${child.pid}), tracking: ${process.cwd()}`);
63
+ console.log(`[daemon] Started (pid ${child.pid}), tracking: ${PROJECT_PATH}`);
59
64
  }
60
65
 
61
66
  function stop() {
@@ -73,11 +78,9 @@ function stop() {
73
78
  function status() {
74
79
  const pid = readPid();
75
80
  const running = pid ? (() => { try { process.kill(pid, 0); return true; } catch { return false; } })() : false;
76
- const cfg = (() => { try { return config.load(); } catch { return null; } })();
77
81
  console.log(`[daemon] Status: ${running ? `running (pid ${pid})` : 'stopped'}`);
78
- console.log(`[daemon] Project: ${cfg?.projectPath || '(all projects)'}`);
82
+ console.log(`[daemon] Project: ${PROJECT_PATH}`);
79
83
  console.log(`[daemon] WAL pending: ${wal.pendingCount()} records`);
80
84
  }
81
85
 
82
- // When required directly (e.g. aicp-tracker start calls this)
83
86
  module.exports = { start, stop, status, tick };