claude-code-wakatime 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,30 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2025 WakaTime, LLC.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ * Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+
11
+ * Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer
13
+ in the documentation and/or other materials provided
14
+ with the distribution.
15
+
16
+ * Neither the names of WakaTime, nor the names of its
17
+ contributors may be used to endorse or promote products derived
18
+ from this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
21
+ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
22
+ NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
24
+ OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # WakaTime for Claude Code
2
+
3
+ [WakaTime](https://wakatime.com/) is an open source VS Code plugin for metrics, insights, and time tracking automatically generated from your programming activity.
4
+
5
+ ## Installation
6
+
7
+ 1. `npm install -g claude-code-wakatime`
8
+
9
+ 2. Install WakaTime for [VS Code](https://wakatime.com/vs-code) to setup your `~/.wakatime/` folder and api key.
10
+
11
+ 4. Use Claude Code and your AI coding activity will be displayed on your [WakaTime dashboard](https://wakatime.com)
12
+
13
+ ## Usage
14
+
15
+ Visit [https://wakatime.com](https://wakatime.com) to see your coding activity.
16
+
17
+ ![Project Overview](https://wakatime.com/static/img/ScreenShots/Screen-Shot-2016-03-21.png)
package/dist/index.js ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const os_1 = __importDefault(require("os"));
10
+ const child_process_1 = require("child_process");
11
+ const STATE_FILE = path_1.default.join(os_1.default.homedir(), '.wakatime', 'claude-code.json');
12
+ const WAKATIME_CLI = path_1.default.join(os_1.default.homedir(), '.wakatime', 'wakatime-cli');
13
+ function timestamp() {
14
+ return Date.now() / 1000;
15
+ }
16
+ function shouldSendHeartbeat() {
17
+ try {
18
+ const last = JSON.parse(fs_1.default.readFileSync(STATE_FILE, 'utf-8')).lastHeartbeatAt ?? timestamp();
19
+ return timestamp() - last >= 60;
20
+ }
21
+ catch {
22
+ return true;
23
+ }
24
+ }
25
+ function updateState() {
26
+ fs_1.default.mkdirSync(path_1.default.dirname(STATE_FILE), { recursive: true });
27
+ fs_1.default.writeFileSync(STATE_FILE, JSON.stringify({ lastHeartbeatAt: timestamp() }, null, 2));
28
+ }
29
+ function sendHeartbeat() {
30
+ try {
31
+ (0, child_process_1.execFileSync)(WAKATIME_CLI, ['--entity', 'claude code', '--entity-type', 'app', '--category', 'ai coding']);
32
+ }
33
+ catch (err) {
34
+ console.error('Failed to send WakaTime heartbeat:', err.message);
35
+ }
36
+ }
37
+ if (shouldSendHeartbeat()) {
38
+ sendHeartbeat();
39
+ updateState();
40
+ }
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const fs_1 = __importDefault(require("fs"));
7
+ const path_1 = __importDefault(require("path"));
8
+ const os_1 = __importDefault(require("os"));
9
+ const CLAUDE_SETTINGS = path_1.default.join(os_1.default.homedir(), '.claude', 'settings.json');
10
+ const HOOK_EVENTS = ['PreToolUse', 'PostToolUse', 'UserPromptSubmit', 'SessionStart'];
11
+ function loadSettings() {
12
+ if (!fs_1.default.existsSync(CLAUDE_SETTINGS)) {
13
+ return {};
14
+ }
15
+ return JSON.parse(fs_1.default.readFileSync(CLAUDE_SETTINGS, 'utf-8'));
16
+ }
17
+ function saveSettings(settings) {
18
+ fs_1.default.mkdirSync(path_1.default.dirname(CLAUDE_SETTINGS), { recursive: true });
19
+ fs_1.default.writeFileSync(CLAUDE_SETTINGS, JSON.stringify(settings, null, 2));
20
+ }
21
+ function installHooks() {
22
+ const settings = loadSettings();
23
+ settings.hooks = settings.hooks || {};
24
+ const hook = {
25
+ matcher: '*',
26
+ hooks: [
27
+ {
28
+ type: 'command',
29
+ command: 'claude-code-wakatime',
30
+ },
31
+ ],
32
+ };
33
+ let hookAlreadyExists = true;
34
+ for (const event of HOOK_EVENTS) {
35
+ settings.hooks[event] = settings.hooks[event] || [];
36
+ // Check if a hook with the same command already exists
37
+ const existingHook = settings.hooks[event].find((existingHook) => existingHook.hooks &&
38
+ Array.isArray(existingHook.hooks) &&
39
+ existingHook.hooks.some((hookItem) => hookItem.command === 'claude-code-wakatime'));
40
+ if (!existingHook) {
41
+ settings.hooks[event].push(hook);
42
+ hookAlreadyExists = false;
43
+ }
44
+ }
45
+ if (hookAlreadyExists) {
46
+ console.log(`WakaTime hooks already installed in Claude ${CLAUDE_SETTINGS}`);
47
+ }
48
+ else {
49
+ saveSettings(settings);
50
+ console.log(`WakaTime hooks installed in Claude ${CLAUDE_SETTINGS}`);
51
+ }
52
+ }
53
+ installHooks();
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "claude-code-wakatime",
3
+ "version": "1.0.0",
4
+ "description": "WakaTime plugin for Claude Code",
5
+ "bin": {
6
+ "claude-code-wakatime": "dist/index.js"
7
+ },
8
+ "scripts": {
9
+ "postinstall": "node dist/install-hooks.js",
10
+ "build": "tsc",
11
+ "watch": "tsc --watch",
12
+ "release:major": "npm version major && npm publish && git push && git push --tags",
13
+ "release:minor": "npm version minor && npm publish && git push && git push --tags",
14
+ "release:patch": "npm version patch && npm publish && git push && git push --tags"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/wakatime/claude-code-wakatime.git"
19
+ },
20
+ "keywords": [
21
+ "claude",
22
+ "claude-code",
23
+ "time-tracking",
24
+ "ai"
25
+ ],
26
+ "dependencies": {},
27
+ "devDependencies": {
28
+ "@types/eslint": "^8.44.7",
29
+ "@types/node": "^20.9.2",
30
+ "@types/pidusage": "^2.0.5",
31
+ "@types/prettier": "^2.7.3",
32
+ "@typescript-eslint/eslint-plugin": "6.11.0",
33
+ "@typescript-eslint/parser": "6.11.0",
34
+ "eslint": "^8.54.0",
35
+ "prettier": "^3.1.0",
36
+ "typescript": "^5.2.2"
37
+ },
38
+ "author": "WakaTime",
39
+ "license": "BSD-3-Clause",
40
+ "bugs": {
41
+ "url": "https://github.com/wakatime/claude-code-wakatime/issues"
42
+ },
43
+ "homepage": "https://github.com/wakatime/claude-code-wakatime#readme"
44
+ }
@@ -0,0 +1,8 @@
1
+ /** @type {import("prettier").Config} */
2
+ const config = {
3
+ singleQuote: true,
4
+ trailingComma: 'all',
5
+ printWidth: 140,
6
+ };
7
+
8
+ module.exports = config;
package/src/index.ts ADDED
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fs from 'fs';
4
+ import path from 'path';
5
+ import os from 'os';
6
+ import { execFileSync } from 'child_process';
7
+
8
+ const STATE_FILE = path.join(os.homedir(), '.wakatime', 'claude-code.json');
9
+ const WAKATIME_CLI = path.join(os.homedir(), '.wakatime', 'wakatime-cli');
10
+
11
+ type State = {
12
+ lastHeartbeatAt?: number;
13
+ };
14
+
15
+ function timestamp() {
16
+ return Date.now() / 1000;
17
+ }
18
+
19
+ function shouldSendHeartbeat(): boolean {
20
+ try {
21
+ const last = (JSON.parse(fs.readFileSync(STATE_FILE, 'utf-8')) as State).lastHeartbeatAt ?? timestamp();
22
+ return timestamp() - last >= 60;
23
+ } catch {
24
+ return true;
25
+ }
26
+ }
27
+
28
+ function updateState() {
29
+ fs.mkdirSync(path.dirname(STATE_FILE), { recursive: true });
30
+ fs.writeFileSync(STATE_FILE, JSON.stringify({ lastHeartbeatAt: timestamp() } as State, null, 2));
31
+ }
32
+
33
+ function sendHeartbeat() {
34
+ try {
35
+ execFileSync(WAKATIME_CLI, ['--entity', 'claude code', '--entity-type', 'app', '--category', 'ai coding']);
36
+ } catch (err: any) {
37
+ console.error('Failed to send WakaTime heartbeat:', err.message);
38
+ }
39
+ }
40
+
41
+ if (shouldSendHeartbeat()) {
42
+ sendHeartbeat();
43
+ updateState();
44
+ }
@@ -0,0 +1,63 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import os from 'os';
4
+
5
+ const CLAUDE_SETTINGS = path.join(os.homedir(), '.claude', 'settings.json');
6
+ const HOOK_EVENTS = ['PreToolUse', 'PostToolUse', 'UserPromptSubmit', 'SessionStart'];
7
+
8
+ function loadSettings(): any {
9
+ if (!fs.existsSync(CLAUDE_SETTINGS)) {
10
+ return {};
11
+ }
12
+
13
+ return JSON.parse(fs.readFileSync(CLAUDE_SETTINGS, 'utf-8'));
14
+ }
15
+
16
+ function saveSettings(settings: any): void {
17
+ fs.mkdirSync(path.dirname(CLAUDE_SETTINGS), { recursive: true });
18
+ fs.writeFileSync(CLAUDE_SETTINGS, JSON.stringify(settings, null, 2));
19
+ }
20
+
21
+ function installHooks(): void {
22
+ const settings = loadSettings();
23
+ settings.hooks = settings.hooks || {};
24
+
25
+ const hook = {
26
+ matcher: '*',
27
+ hooks: [
28
+ {
29
+ type: 'command',
30
+ command: 'claude-code-wakatime',
31
+ },
32
+ ],
33
+ };
34
+
35
+ let hookAlreadyExists = true;
36
+
37
+ for (const event of HOOK_EVENTS) {
38
+ settings.hooks[event] = settings.hooks[event] || [];
39
+
40
+ // Check if a hook with the same command already exists
41
+ const existingHook = settings.hooks[event].find((existingHook: any) =>
42
+ existingHook.hooks &&
43
+ Array.isArray(existingHook.hooks) &&
44
+ existingHook.hooks.some((hookItem: any) =>
45
+ hookItem.command === 'claude-code-wakatime'
46
+ )
47
+ );
48
+
49
+ if (!existingHook) {
50
+ settings.hooks[event].push(hook);
51
+ hookAlreadyExists = false;
52
+ }
53
+ }
54
+
55
+ if (hookAlreadyExists) {
56
+ console.log(`WakaTime hooks already installed in Claude ${CLAUDE_SETTINGS}`);
57
+ } else {
58
+ saveSettings(settings);
59
+ console.log(`WakaTime hooks installed in Claude ${CLAUDE_SETTINGS}`);
60
+ }
61
+ }
62
+
63
+ installHooks();
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "commonjs",
5
+ "outDir": "dist",
6
+ "strict": true,
7
+ "esModuleInterop": true
8
+ },
9
+ "include": ["src"]
10
+ }