# xlboy/type-zen architecture

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

- Interactive diagram: https://gitdiagram.com/xlboy/type-zen
- Repository: https://github.com/xlboy/type-zen

## Overview

##### Main Components
- Core Engine (packages/core)
- Contains the compiler, parser, AST, grammar (Nearley grammar files, moo lexer, etc.), and related utilities.
- Divided into submodules such as:
▫ AST – building and traversing type representations.
▫ Compiler – converting sugar syntax to valid TypeScript.
▫ Grammar – nearley definitions and lexer implementations.
- CLI Interface (packages/cli)
- Command-line executable that exposes the core functionality.
- Wraps the core engine features by parsing user input, invoking the compiler, and outputting TypeScript code.
- TypeScript Plugin (packages/ts-plugin)
- Integrates with the TypeScript compiler.
- Offers runtime transformation or real-time diagnostics integration; it uses logging, snapshotting, and other helper modules.
- VSCode Extension (packages/vscode)
- Provides language support, editor integrations, and commands (like “compile-current-file”) within the Visual Studio Code ecosystem.
- Relies on configurations, language syntax highlighting files, and underlying core functionalities (possibly reusing the TS Plugin or direct calls to the core engine).
- Global Preset Types (packages/preset-type)
- Supplies global type declarations (like TZ_URS and others) required by the compiled output.
- Imported automatically via tsconfig for TypeScript.
- Playground Website (website/playground)
- A front-end built (using React as suggested by TSX files) that lets users experiment with TypeZen interactively.
- Acts as a client that calls the underlying transformation, probably via a web assembly variant or by bundling the core’s transformation logic.

##### Relationships and Data Flow
– The Core Engine is clearly the heart of the system. Both CLI, TS Plugin, and even parts of the VSCode extension depend on it.
– The CLI takes user commands and feeds code into the Core Engine; its output is then either presented on the console or used to generate files.
– The TS Plugin integrates with the TypeScript toolchain and may intercept source files or serve as a transformer that dispatches code to the Core Engine.
– The VSCode Extension acts as an orchestration layer for a better developer experience, providing commands, configuration, and possibly real-time analysis by interfacing with the TS Plugin or directly the Core Engine.
– The Playground website offers a UI front-end to demonstrate the language’s capabilities, sending code to be compiled and then showing the transformation results.

##### Architectural Patterns and Design Principles
– Modular design: Each major functionality (CLI, core engine, plugin, extension, playground) lives within its own package, easing maintenance and testing.
– Clear separation of concerns: The core language processing is separated from the different interfaces that leverage it. This promotes reusability (the same core engine is used by CLI, plugin, and extension).
– Standard dependency integration: Through configuration files (pnpm-workspace, tsconfig.json, etc.), each package declares its dependencies with clear interfaces.
– Extensibility: The modular approach (including plans for an “Unplugin” integration supporting multiple bundlers such as Webpack/Vite/Rollup) hints at design to allow future integration.
– Emphasis on testing: The large number of test directories across packages indicates thorough testing as a design principle.

##### Final Reminders
– Clearly label every component and sub-component.
– Capture as much detail as possible without clutter—explain the architectural “heart” (packages/core) and then show how each supporting package ties into the overall system.
– As extra references, include any external integrations (like how the Playground and VSCode Extension relate to community usage and developer tools).

## Diagram (Mermaid)

```mermaid
graph TD
    %% Core Engine Subgraph
    subgraph "Core Engine"
        CE("Core Engine"):::internal
        CE_AST("AST Module"):::internal
        CE_COMPILER("Compiler Module"):::internal
        CE_GRAMMAR("Grammar Module"):::internal
        CE_PARSER("Parser (Entry)"):::internal
        CE --> CE_AST
        CE --> CE_COMPILER
        CE --> CE_GRAMMAR
        CE --> CE_PARSER
    end

    %% TypeScript Plugin Subgraph
    subgraph "TypeScript Plugin"
        TSP("Plugin Implementation"):::integration
        TSP_logger("Logger"):::integration
        TSP_snapshot("Snapshot"):::integration
        TSP --> TSP_logger
        TSP --> TSP_snapshot
    end

    %% VSCode Extension Subgraph
    subgraph "VSCode Extension"
        VSC("VSCode Commands"):::integration
        VSC_core("Core Modules"):::integration
        VSC --> VSC_core
    end

    CLI("CLI Interface"):::integration
    PRESET("Global Preset Types"):::internal
    PLAYGROUND("Playground Website"):::frontend

    %% Relationships and Data Flow
    CLI -->|"InvokesCore"| CE
    TSP -->|"CallsCore"| CE
    VSC -->|"RequestsCore"| CE
    TSP -->|"InteractsWith"| VSC
    VSC -->|"InteractsWith"| TSP
    PLAYGROUND -->|"SendsCodeForTransformation"| CE
    CE -->|"RequiresGlobalTypes"| PRESET

    %% Click Events for Core Engine
    click CE "https://github.com/xlboy/type-zen/tree/master/packages/core"
    click CE_AST "https://github.com/xlboy/type-zen/tree/master/packages/core/src/ast"
    click CE_COMPILER "https://github.com/xlboy/type-zen/tree/master/packages/core/src/compiler"
    click CE_GRAMMAR "https://github.com/xlboy/type-zen/tree/master/packages/core/src/grammar"
    click CE_PARSER "https://github.com/xlboy/type-zen/blob/master/packages/core/src/parser.ts"

    %% Click Events for CLI Interface
    click CLI "https://github.com/xlboy/type-zen/blob/master/packages/cli/src/cli.ts"

    %% Click Events for TypeScript Plugin
    click TSP "https://github.com/xlboy/type-zen/blob/master/packages/ts-plugin/src/plugin.ts"
    click TSP_logger "https://github.com/xlboy/type-zen/blob/master/packages/ts-plugin/src/logger.ts"
    click TSP_snapshot "https://github.com/xlboy/type-zen/blob/master/packages/ts-plugin/src/snapshot.ts"

    %% Click Events for VSCode Extension
    click VSC "https://github.com/xlboy/type-zen/blob/master/packages/vscode/src/core/command/compile-current-file.ts"
    click VSC_core "https://github.com/xlboy/type-zen/tree/master/packages/vscode/src/core"

    %% Click Event for Global Preset Types
    click PRESET "https://github.com/xlboy/type-zen/blob/master/packages/preset-type/index.d.ts"

    %% Click Event for Playground Website
    click PLAYGROUND "https://github.com/xlboy/type-zen/tree/master/website/playground/src"

    %% Styling classes for color coding
    classDef internal fill:#C8E6C9,stroke:#388E3C,stroke-width:2px;
    classDef integration fill:#BBDEFB,stroke:#1976D2,stroke-width:2px;
    classDef frontend fill:#FFCDD2,stroke:#D32F2F,stroke-width:2px;
```
