# wakatime/vscode-wakatime architecture

> Architecture diagram of the GitHub repository wakatime/vscode-wakatime, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/wakatime/vscode-wakatime
- Repository: https://github.com/wakatime/vscode-wakatime

## Overview

##### Project Type and Purpose
- This is a Visual Studio Code extension designed to track coding activity, log work times, and send periodic “heartbeats” to the WakaTime service.
- The README highlights installation and usage within VS Code, integration with user API keys, and both desktop and web modes. Therefore, the system is essentially a VS Code plugin with separate modules for desktop and web contexts that interface with the external WakaTime API.

##### Analyzing the File Structure
- Top-level files and folders:
- The .github folder contains GitHub issue templates and CI/CD workflows (e.g., on_push.yml). These are not core to runtime architecture but indicate CI integration.
- The src folder is the heart of the project and includes core TypeScript files such as constants, dependencies, logger, options, utils, and specific files for handling desktop (desktop.ts) and extension logic (extension.ts).
- Inside src/web, we see analogous files (extension.ts, logger.ts, wakatime.ts), which indicate the project supports a web version of the extension.
- Configuration files (package.json, tsconfig.json, tslint.json, webpack.config.js) suggest that the project uses a TypeScript and Webpack toolchain.
- The test directory holds unit and integration tests that confirm the correctness of the extension’s functionality.
- Architectural Patterns:
- There is a clear separation between the core extension logic (in src) and the web-specific modules (in src/web), hinting at a modular design.
- The presence of common modules (like logger and wakatime communication) in both desktop and web subdirectories implies reusability and adherence to separation-of-concerns.
- The dependency and constants files indicate central management of configuration and external library dependencies.

a. Main Components:
- VS Code Extension Core:
- Extension Bootstrap (src/extension.ts): Initializes the extension within VS Code.
- Desktop-specific Components (src/desktop.ts): Logic specific for the local VS Code runtime.
- Web-specific Components (src/web/extension.ts): Handles cases when running in a web-based VS Code environment.
- Utility & Logging Modules:
- Logger (src/logger.ts and src/web/logger.ts): For logging and debugging operation.
- WakaTime Communication (src/wakatime.ts and src/web/wakatime.ts): Responsible for sending heartbeats and interacting with the WakaTime API.
- Configuration:
- Options and Constants (src/options.ts, src/constants.ts): Manage user settings and application constants.
- External Config Files: User API key stored in INI file (e.g., ~/.wakatime.cfg).
- External Interfaces:
- WakaTime API/Web Service: The external metrics dashboard that receives data from the extension.
- GitHub Workflows: Represent the CI/CD pipeline (less core but important for project lifecycle).

b. Relationships and Interactions:
- The extension is triggered by VS Code events (e.g., file open, edit commands) and uses the core module (src/extension.ts) to initialize and coordinate actions.
- When an action is detected, the appropriate desktop or web module (depending on the environment) is called.
- Logger modules capture events and errors.
- WakaTime modules collect and send heartbeat data to the external WakaTime API, using configuration details like the API key from the user's configuration file.
- Utility modules (options, constants, dependencies) are used across the components for consistent behavior and configuration.

c. Architectural Patterns & Design Principles:
- Modular Design: Split between desktop and web modules, with shared utilities for logging and API communication.
- Separation of Concerns: Each file (extension logic, logging, configuration) has a well-defined responsibility.
- Reusability: Code for logging and API communication is duplicated or shared (similar files in src and src/web) allowing reuse across different modes.

d. Relevant Technologies and Tools:
- Visual Studio Code Extension API: Core integration point provided by VS Code, commonplace in the extension bootstrap.
- TypeScript: Used throughout (notice .ts files, tsconfig.json, tslint.json).
- Webpack: Used for bundling the extension (webpack.config.js).
- NodeJS Environment: Underpins the extension’s infrastructure.
- GitHub Actions (via the .github/workflows directory): For continuous integration and testing.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["VS Code Events"]:::core
    B["Extension Bootstrap (VS Code Extension Core)"]:::core
    J["Configuration/Options Handler"]:::util
    C["VS Code Extension (Desktop)"]:::core
    D["VS Code Extension (Web)"]:::core
    E["Logger Module (Desktop)"]:::util
    F["Logger Module (Web)"]:::util
    G["WakaTime API Communication (Desktop)"]:::comm
    H["WakaTime API Communication (Web)"]:::comm
    I["External WakaTime Service"]:::external
    K["CI/CD Workflow"]:::cicd
    L["GitHub Issue Templates"]:::cicd

    A -->|"triggers"| B
    B -->|"loads"| J
    B -->|"initializes"| C
    B -->|"initializes"| D
    C -->|"logs activity"| E
    D -->|"logs activity"| F
    E -->|"sends heartbeat"| G
    F -->|"sends heartbeat"| H
    G -->|"calls"| I
    H -->|"calls"| I
    K -.->|"CI build & test"| B
    L -.->|"issue tracking"| B

    click B "https://github.com/wakatime/vscode-wakatime/blob/master/src/extension.ts"
    click C "https://github.com/wakatime/vscode-wakatime/blob/master/src/desktop.ts"
    click D "https://github.com/wakatime/vscode-wakatime/blob/master/src/web/extension.ts"
    click E "https://github.com/wakatime/vscode-wakatime/blob/master/src/logger.ts"
    click F "https://github.com/wakatime/vscode-wakatime/blob/master/src/web/logger.ts"
    click G "https://github.com/wakatime/vscode-wakatime/blob/master/src/wakatime.ts"
    click H "https://github.com/wakatime/vscode-wakatime/blob/master/src/web/wakatime.ts"
    click J "https://github.com/wakatime/vscode-wakatime/blob/master/src/options.ts"
    click J "https://github.com/wakatime/vscode-wakatime/blob/master/src/constants.ts"
    click K "https://github.com/wakatime/vscode-wakatime/blob/master/.github/workflows/on_push.yml"
    click L "https://github.com/wakatime/vscode-wakatime/tree/master/.github/ISSUE_TEMPLATE"

    classDef core fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef util fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef comm fill:#FFA07A,stroke:#000,stroke-width:2px;
    classDef external fill:#FF6961,stroke:#000,stroke-width:2px;
    classDef cicd fill:#F4A460,stroke:#000,stroke-width:2px;
```
