How mmkal/trpc-cli works
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.