# mmkal/trpc-cli architecture

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

- Interactive diagram: https://gitdiagram.com/mmkal/trpc-cli
- Repository: https://github.com/mmkal/trpc-cli

## Overview

##### Project Type and Purpose
- This project is a CLI tool library that transforms a tRPC router (with its type-safe, validated procedures using zod/arktype/valibot) into a professional command-line interface.
- Its purpose is to bring the type safety and developer experience of tRPC into the realm of CLI applications, automatically mapping procedure inputs to command-line arguments and options, auto-generating help docs, and handling complex input validation.

##### Analyzing the File Structure
- Top-Level Files/Folders:
- The .github directory shows CI/CD workflows and dependency management.
- The docs directory and README.md contain rich documentation and examples for usage.
- The src folder contains the main source code files. Key files include:
▪ completions.ts – likely related to enabling command completions.
▪ errors.ts – centralized error definitions/handling.
▪ json-schema.ts – converting zod schemas (or similar) to JSON schema.
▪ parse-procedure.ts – responsible for mapping and parsing tRPC procedures into CLI commands.
▪ trpc-compat.ts – provides compatibility layers for different versions of tRPC.
▪ logging.ts, types.ts, util.ts – provide supportive functionality.
- The test folder holds unit and end-to-end tests, along with example fixtures (calculator.ts, migrations.ts) demonstrating real CLI command flows.
- Configuration Files: The presence of tsconfig.json, eslint.config.js, vite.config.ts, and package.json indicates standard TypeScript project structure, ensuring that the build, linting, and dependency definitions are managed properly.

##### Information from the README
- It explains how the library maps tRPC router procedures into CLI commands, handling positional and option arguments derived from zod (or arktype/valibot) validations.
- It details the lifecycle: creating a router, defining procedures with metadata (like description, default command, etc.), invoking createCli({router}).run(), and how input validations and error reporting are handled.
- It also covers the integration with helper libraries for CLI argument parsing (e.g., commander implied) and completions (via omelette).

##### Identifying Main Architectural Components and Relationships
a. Components of the System:
- CLI Entry Point & Command Dispatcher
- The main entry script (often created via createCli) that parses command-line arguments.
- tRPC Router Integration
- The core router object, mapping procedures that are used both for CLI operation and programmatic calling.
- Input Parsing & Validation Module
- Converts CLI arguments into data types validated via zod/arktype/valibot (with json-schema conversion support).
- Procedural Execution Engine
- The logic that invokes the mapped procedures based on the user’s command.
- Help Documentation Generator
- Auto-generates usage and help information, derived from procedure metadata and validation schemas.
- Logging & Error Handling Subsystem
- Handles logging of outputs (including proper JSON formatting) and graceful error handling as seen in logging.ts and errors.ts.
- Testing and Compatibility Layers
- Test suites in the test directory and compatibility support for different tRPC versions (e.g., trpc-compat.ts).
- CLI Completions Module
- Optionally integrates with omelette to provide tab completions.

b. Relationships and Data Flow:
- The user invokes the CLI command in a terminal → the entry point (via createCli) receives and parses the command-line arguments.
- The Argument Parser (and optionally commander/omelette integration) maps the raw input to a structured format.
- The Input Parsing module (using the JSON schema conversion of zod types) validates and transforms the inputs.
- The CLI dispatcher then routes the validated inputs to the appropriate tRPC procedure defined within the router.
- The procedure executes (using its defined query or mutation), producing an output.
- Output goes to the Logging subsystem, where it is either printed or formatted (line-by-line, using JSON.stringify where applicable).
- Any errors are caught and passed through the Error Handling subsystem, which also triggers a non-zero process exit.

c. Architectural Patterns and Design Principles:
- Separation of Concerns: Clear divide between CLI input handling, tRPC procedure mapping, and validation/logging.
- Modularity: Each functionality (parsing, validation, logging, error handling) is kept in separate source files.
- Extensibility: The system allows plugging in alternative validation libraries and even falls back to JSON input for complex types.
- Reusability: The tRPC router is used both for CLI commands and as a backend component, enabling programmatic calls.

d. Relevant Technologies and Frameworks:
- TypeScript: Enforces type safety.
- tRPC: Core routing and procedure definitions (supporting both v10 and v11).
- zod (along with arktype and valibot): For input validation.
- Commander (or a similar library, as implied): For parsing CLI arguments.
- omelette (optional): For providing tab completions.
- Node.js: Underlying runtime, with process and console integrations.

##### Elements
- "CLI Entry Point" or "Main Command Dispatcher"
- "tRPC Router & Procedures"
- "Input Parser & Validator"
- "Help Documentation Generator"
- "Logging & Error Handler"
- "Completions Module (Omelette Integration)"
- Data flow from command line input → parsing → validation → procedure invocation → output logging.
- Dependency relationships (for example, the CLI entry depends on the tRPC router for routing the commands).
- Rectangles for components.

##### Final Recommendations
- Start at the entry point: the CLI interface that processes user input.
- Invocation of the corresponding tRPC procedure and display both the help generation (for the “--help” commands) and the error output pathways.
- Finally, illustrate the result output being managed by the logging subsystem.

## Diagram (Mermaid)

```mermaid
flowchart TD
    T["User Terminal Input"]:::external
    A["CLI Entry Point & Command Dispatcher"]:::cli
    B["Input Parsing & Validation Module"]:::parsing
    C["tRPC Router Integration & Procedure Mapping"]:::router
    D["Compatibility Layer for tRPC Versions"]:::compat
    F["Help Documentation Generator"]:::help
    G["CLI Completions Module"]:::completion
    subgraph "Logging & Error Handling"
        E1["Logging Subsystem"]:::logging
        E2["Error Handling Subsystem"]:::error
    end
    H["Testing & Example Fixtures"]:::test

    T --> A
    A --> B
    B --> C
    D --> C
    C --> E1
    C --> E2
    A --> F
    A --> G
    H --- A

    click A "https://github.com/mmkal/trpc-cli/blob/main/src/index.ts"
    click B "https://github.com/mmkal/trpc-cli/blob/main/src/json-schema.ts"
    click C "https://github.com/mmkal/trpc-cli/blob/main/src/parse-procedure.ts"
    click D "https://github.com/mmkal/trpc-cli/blob/main/src/trpc-compat.ts"
    click E1 "https://github.com/mmkal/trpc-cli/blob/main/src/logging.ts"
    click E2 "https://github.com/mmkal/trpc-cli/blob/main/src/errors.ts"
    click G "https://github.com/mmkal/trpc-cli/blob/main/src/completions.ts"
    click H "https://github.com/mmkal/trpc-cli/tree/main/test"

    classDef external fill:#fde2e4,stroke:#333,stroke-width:2px;
    classDef cli fill:#d0efeb,stroke:#333,stroke-width:2px;
    classDef parsing fill:#cff4fc,stroke:#333,stroke-width:2px;
    classDef router fill:#e4c1f9,stroke:#333,stroke-width:2px;
    classDef compat fill:#f6e8ea,stroke:#333,stroke-width:2px;
    classDef help fill:#dfffd6,stroke:#333,stroke-width:2px;
    classDef completion fill:#fff3b0,stroke:#333,stroke-width:2px;
    classDef logging fill:#cdeac0,stroke:#333,stroke-width:2px;
    classDef error fill:#ffcccc,stroke:#333,stroke-width:2px;
    classDef test fill:#d0d6f7,stroke:#333,stroke-width:2px;
```
