# statelyai/xstate-tools architecture

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

- Interactive diagram: https://gitdiagram.com/statelyai/xstate-tools
- Repository: https://github.com/statelyai/xstate-tools

## Overview

##### Project type and purpose
- This is a public monorepo for XState tooling. It ships:
- A command-line tool under apps/cli
- A VS Code extension under apps/extension (split into client & server halves)
- Two reusable libraries under packages/machine-extractor and packages/shared

##### File structure
- Top-level “apps” folder:
- cli: the end-user CLI project
- extension: the VS Code extension, further subdivided into client (webview + editor integration) and server (language server)
- Top-level “packages” folder:
- machine-extractor: parses TS/JS files for XState machines, returns an AST-style extract result and supports modifications
- shared: common helpers for walking state trees, computing typegen output, mapping file edits, etc.
- Configuration and scripts:
- Babel, Jest, ESLint, Prettier, Husky hooks, GitHub Actions workflows for CI & publish
- Root scripts folder for e2e tests, bundling editor assets, tagging releases

##### README for additional insights
- Confirms this is the “XState Tools” monorepo
- Links directly to the VS Code extension, implying tight integration with the client/server subprojects

a. Main components
- CLI application
- VS Code extension client (UI/webview, editor glue)
- VS Code extension server (Language Server Protocol endpoint)
- Shared library packages:
– machine-extractor
– shared
- External integrations:
– TypeScript compiler API (for parsing/AST)
– VS Code editor host (for loading the extension)
b. Relationships & interactions
- CLI → invokes machine-extractor and shared APIs → outputs code/typegen/config files to disk
- Extension client ↔ communicates via LSP (JSON‐RPC) ↔ extension server
- Extension server → uses shared + machine-extractor to perform diagnostics, code actions, typegen hints
- Both CLI and extension server depend on the two packages in packages/
- CI/CD workflows orchestrate tests & publish pipelines for all apps and packages
c. Architectural patterns & design principles
- Monorepo workspace pattern (sharing code via relative imports/package.json workspaces)
- Separation of concerns:
– UI layer (extension client)
– Language server logic (extension server)
– Core parsing/extraction logic (machine-extractor)
– Utility functions & abstractions (shared)
- LSP design for the extension (client/server split over Webview vs Node process)
- CLI tool using Node/TypeScript for batch operations
d. Key technologies & frameworks
- TypeScript (ts-node in CLI, compiled in extension/server)
- VS Code Extension API + Webview
- Language Server Protocol (vscode-languageclient, vscode-languageserver)
- Babel & Jest for testing
- Husky, ESLint, Prettier for pre-commit checks
- GitHub Actions for CI & publish

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% CI/CD Pipeline
    CI["CI/CD Pipeline (.github/workflows)"]:::ci

    %% External Systems
    TS["TypeScript Compiler API"]:::external
    VSCode["VS Code Editor Host"]:::external
    Disk[(Disk I/O)]:::external

    %% Shared Core Libraries
    subgraph "Shared Core Libraries"
        ME["packages/machine-extractor"]:::lib
        SH["packages/shared"]:::lib
    end

    %% Applications
    subgraph "Applications"
        CLI["apps/cli (CLI Tool)"]:::app
        subgraph "VS Code Extension"
            CL["apps/extension/client (UI/Webview)"]:::extClient
            SV["apps/extension/server (LSP Server)"]:::extServer
        end
    end

    %% Connections
    CI --> CLI
    CI --> CL
    CI --> SV
    CI --> ME
    CI --> SH

    CLI --> ME
    CLI --> SH
    CLI -->|"writes output to disk"| Disk

    CL <-->|"LSP (JSON-RPC)"| SV
    CL --> VSCode
    SV --> ME
    SV --> SH
    SV --> TS

    ME --> TS

    %% Click Events
    click CLI "https://github.com/statelyai/xstate-tools/tree/main/apps/cli"
    click CL "https://github.com/statelyai/xstate-tools/tree/main/apps/extension/client"
    click SV "https://github.com/statelyai/xstate-tools/tree/main/apps/extension/server"
    click ME "https://github.com/statelyai/xstate-tools/tree/main/packages/machine-extractor"
    click SH "https://github.com/statelyai/xstate-tools/tree/main/packages/shared"

    %% Styles
    classDef ci fill:#f5a623,stroke:#333,stroke-width:1px,color:#000
    classDef external fill:#dddddd,stroke:#333,stroke-width:1px,color:#000
    classDef lib fill:#aaaaaa,stroke:#333,stroke-width:1px,color:#000
    classDef app fill:#7ed321,stroke:#333,stroke-width:1px,color:#000
    classDef extClient fill:#4a90e2,stroke:#333,stroke-width:1px,color:#fff
    classDef extServer fill:#50e3c2,stroke:#333,stroke-width:1px,color:#000
```
