# mslavov/shelly-forge architecture

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

- Interactive diagram: https://gitdiagram.com/mslavov/shelly-forge
- Repository: https://github.com/mslavov/shelly-forge

## Overview

##### Main Components
- CLI Interface & Command Handlers:
– The core CLI functionality is implemented in the “src/commands” directory with separate files for each command (build, create, debug, deploy, init, logs).
– This suggests a clear separation of concerns where each command is a discrete module.

- Core Modules & Utilities:
– Files like “src/shelly-device.ts”, “src/shelly-forge.ts”, “src/solutions-config.ts”, and “src/script-hash-cache.ts” provide core functionality such as device interactions, handling of solutions’ configurations, and caching.

- Templates and Project Scaffolding:
– The “templates/default” folder holds default project templates (including its own “src”, configuration files, and a nested “.cursor” folder with similar rules files). This module is used during the “init” command to scaffold new projects.

- Type Definitions & Integration with Shelly API:
– The “types” folder contains TypeScript definitions (e.g., for Shelly’s API, TemperatureStatus, etc.), which provides strong typing and integration points with the device APIs.

- Configuration Files:
– Configuration files like “solutions.config.json”, “.env”, “tsconfig.json”, and even misc files (gitignore, LICENSE, CONTRIBUTING.md, etc.) support various aspects of the development and deployment processes.

##### Relationships and Data Flow
- User Interaction:
– The user interacts with the system through CLI commands. The invocation (for example, “shelly-forge init”, “shelly-forge create”, etc.) routes into the appropriate command modules.

- Command Execution Flow:
– Each command module (e.g., build.ts, deploy.ts, etc.) orchestrates multiple lower-level modules – such as reading the “solutions.config.json”, leveraging type definitions (in “types”), and calling into core functions (like “shelly-forge.ts”) that interact with the Shelly device API.

- Project Scaffolding & Templates:
– When a new project is initiated, the “init” command pulls in the structure from the “templates/default” folder to create a baseline project.

- Live Development:

- Deployment Flow:
– The deploy command reads configuration from “solutions.config.json” (mapping scripts to target devices) and deploys the bundled script to a Shelly device via API calls defined in “ShellyAPI.d.ts” and implemented in core modules.

##### Architectural Patterns and Design Principles
- Modular Structure:
– The commands are decoupled into separate modules, making it easy to extend or modify behavior for each CLI command.

- Template-based Project Scaffolding:
– The use of a “templates” directory ensures that new projects have a predefined structure, reinforcing consistency.

- Interface-based Design & Type Safety:
– Usage of TypeScript with dedicated “types” files shows a commitment to strong typing and clearly defined interfaces for device APIs and other interactions.

- Configuration Driven:
– The solutions are dynamically defined via JSON “solutions.config.json,” which abstracts device-specific details and allows flexible deployment.

## Diagram (Mermaid)

```mermaid
flowchart TD
    U("User CLI Input"):::user

    subgraph "CLI Command Handlers"
        CB("Build Command"):::cli
        CC("Create Command"):::cli
        CD("Debug Command"):::cli
        CDe("Deploy Command"):::cli
        CI("Init Command"):::cli
        CL("Logs Command"):::cli
    end

    subgraph "Core Modules & Utilities"
        SFD("Shelly Forge Core"):::core
        SD("Shelly Device Manager"):::core
        SC("Solutions Config Module"):::core
        SHC("Script Hash Cache"):::core
    end

    subgraph "Templates & Project Scaffolding"
        TS("Default Templates"):::template
        TC(".cursor Rules"):::template
    end

    subgraph "Type Definitions & API Integration"
        TAPI("Shelly API Types"):::type
        TStatus("TemperatureStatus Types"):::type
        TIMisc("Index & Other Types"):::type
    end

    subgraph "Configuration Files"
        SOL("solutions.config.json (Project Root)"):::config
        SOLT("solutions.config.json (Template)"):::config
        TSCONF("tsconfig.json"):::config
        ENV(".env.example"):::config
    end

    U -->|"inputs"| CB
    U -->|"inputs"| CC
    U -->|"inputs"| CD
    U -->|"inputs"| CDe
    U -->|"inputs"| CI
    U -->|"inputs"| CL

    CB -->|"triggers"| SFD
    CC -->|"triggers"| SFD
    CD -->|"triggers"| SFD
    CDe -->|"triggers"| SFD
    CI -->|"triggers"| SFD
    CL -->|"triggers"| SFD

    SFD -->|"reads"| SC
    SC -->|"loads"| SOL

    CDe -->|"reads"| SOL
    CDe -->|"invokes"| TAPI
    TAPI -->|"interacts"| SD
    CDe -->|"utilizes"| TIMisc

    CI -->|"scaffolds"| TS
    TS -->|"includes"| TC
    TS -->|"contains"| SOLT
    CI -->|"scaffolds"| TS

    CD -->|"uses"| SHC

    SFD -->|"communicates_with"| SD
    SFD -->|"caches_via"| SHC

    click CB "https://github.com/mslavov/shelly-forge/blob/main/src/commands/build.ts"
    click CC "https://github.com/mslavov/shelly-forge/blob/main/src/commands/create.ts"
    click CD "https://github.com/mslavov/shelly-forge/blob/main/src/commands/debug.ts"
    click CDe "https://github.com/mslavov/shelly-forge/blob/main/src/commands/deploy.ts"
    click CI "https://github.com/mslavov/shelly-forge/blob/main/src/commands/init.ts"
    click CL "https://github.com/mslavov/shelly-forge/blob/main/src/commands/logs.ts"

    click SFD "https://github.com/mslavov/shelly-forge/blob/main/src/shelly-forge.ts"
    click SD "https://github.com/mslavov/shelly-forge/blob/main/src/shelly-device.ts"
    click SC "https://github.com/mslavov/shelly-forge/blob/main/src/solutions-config.ts"
    click SHC "https://github.com/mslavov/shelly-forge/blob/main/src/script-hash-cache.ts"

    click TS "https://github.com/mslavov/shelly-forge/tree/main/templates/default"
    click TC "https://github.com/mslavov/shelly-forge/blob/main/templates/default/.cursor"

    click TAPI "https://github.com/mslavov/shelly-forge/blob/main/types/ShellyAPI.d.ts"
    click TStatus "https://github.com/mslavov/shelly-forge/blob/main/types/TemperatureStatus.d.ts"
    click TIMisc "https://github.com/mslavov/shelly-forge/blob/main/types/index.d.ts"

    click SOL "https://github.com/mslavov/shelly-forge/blob/main/solutions.config.json"
    click SOLT "https://github.com/mslavov/shelly-forge/blob/main/templates/default/solutions.config.json"
    click TSCONF "https://github.com/mslavov/shelly-forge/blob/main/tsconfig.json"
    click ENV "https://github.com/mslavov/shelly-forge/blob/main/templates/default/.env.example"

    classDef user fill:#f6b26b,stroke:#b45f06,stroke-width:2px;
    classDef cli fill:#9fc5e8,stroke:#3d85c6,stroke-width:2px;
    classDef core fill:#b6d7a8,stroke:#6aa84f,stroke-width:2px;
    classDef template fill:#ffe599,stroke:#b8a31e,stroke-width:2px;
    classDef type fill:#d9ead3,stroke:#6aa84f,stroke-width:2px;
    classDef config fill:#ead1dc,stroke:#b8546b,stroke-width:2px;
```
