@computerwwwizards/di-code-generator
v0.2.0
Published
Code generator for @computerwwwizards/dependency-injection - generates TypeScript boilerplate for DI containers
Maintainers
Readme
DI Code Generator
A minimal, focused code generator written in Zig for creating TypeScript/JavaScript dependency injection boilerplate.
Overview
This project provides primitives for string interpolation and name manipulation - the foundational building blocks needed to generate DI registration files and related boilerplate code.
Features
String Interpolation
- Parse template strings with
{param}placeholders - Apply parameter values to generate final strings
- Concatenate strings with customizable separators
Name Utilities
- Case Conversions: Convert between different naming conventions
camelCasePascalCasesnake_casekebab-caseSCREAMING_SNAKE_CASE
- Name Parameterization: Apply names to templates with automatic case conversion
- Name Concatenation: Join multiple name parts with patterns
Building
# Build the project
zig build
# Run the example
zig build run
# Run all tests
zig build testTesting
Tests are organized by concern:
- interpolation tests: String template parsing and parameter application
- name_utils tests: Case conversion and name manipulation
- module tests: Integration tests
zig build testUsage Example
const std = @import("std");
const di_gen = @import("di-code-generator");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Template interpolation with case conversion
const result = try di_gen.name_utils.parameterizeName(
allocator,
"register{Name}",
"user_service",
.PascalCase,
);
defer allocator.free(result);
// Result: "registerUserService"
// Case conversions
const snake = try di_gen.name_utils.toSnakeCase(allocator, "HelloWorld");
defer allocator.free(snake);
// Result: "hello_world"
}Project Structure
zig/di-code-generator/
├── build.zig # Build configuration
├── build.zig.zon # Package metadata
├── src/
│ ├── root.zig # Module exports
│ ├── main.zig # CLI executable
│ ├── interpolation.zig # String interpolation primitives
│ └── name_utils.zig # Name manipulation utilities
└── README.mdAPI Reference
Interpolation Module (interpolation.zig)
InterpolationPart
Union type representing either literal text or a parameter placeholder.
parseTemplate(allocator, template) ![]InterpolationPart
Parse a template string into parts. Template format: "Hello {name}!"
applyParams(allocator, parts, params) ![]u8
Apply parameter values to interpolation parts.
concat(allocator, separator, strings) ![]u8
Concatenate multiple strings with a separator.
Name Utils Module (name_utils.zig)
Case Conversion Functions
toCamelCase(allocator, input) ![]u8toPascalCase(allocator, input) ![]u8toSnakeCase(allocator, input) ![]u8toKebabCase(allocator, input) ![]u8
parameterizeName(allocator, pattern, name, case) ![]u8
Apply a name to a pattern with automatic case conversion.
pattern: Template string like"register{Name}"name: The name to insertcase: Target naming convention
Design Decisions
Minimal Scope
This implementation focuses solely on the primitives needed for code generation:
- String interpolation - Building strings from templates
- Name manipulation - Converting and parameterizing names
These are the most fundamental operations required to generate any boilerplate code. Higher-level features (file generation, directory structure, etc.) can be built on top of these primitives.
No Dependencies
The project uses only Zig's standard library, making it lightweight and easy to integrate.
Test Organization
Tests are separated by concern into different executables, allowing focused testing and faster iteration during development.
License
See repository root for license information.
Related
This generator is designed to work with @computerwwizards/dependency-injection - a reflection-free IoC container for JavaScript/TypeScript.
NPM Package Usage
This tool is also available as an npm package: @computerwwwizards/di-code-generator
Installation
npm install @computerwwwizards/di-code-generator
# or
pnpm add @computerwwwizards/di-code-generatorCLI Usage
# Auto-discover config.json in current directory
npx di-code-gen
# Specify config file
npx di-code-gen --config ./services.config.json
# Override output directory
npx di-code-gen --config ./config.json --output ./src/diQuick CLI Generation (Auto-inferred Interface)
Generate a service directly via CLI without a config file. The interface name is automatically inferred from the service name (e.g., userService → IUserService):
# Single service
npx di-code-gen --service userService --output ./src/di
# Multiple services
npx di-code-gen --service userService --service paymentGateway --output ./src/di
# Custom interface name (optional; defaults to I + PascalCase of service name)
npx di-code-gen --service userService:IUserAuthService --output ./src/di
# Combination: CLI services override config
npx di-code-gen --config config.json --service customService --output ./src/diAuto-inference examples:
userService→IUserServicepaymentGateway→IPaymentGatewayauth_service→IAuthServiceDBConnection→IDbconnection
When --service is provided, CLI services take precedence over config file services.
Concrete Example (UserService)
- Create a config file
di.config.json:
{
"output": "./src/di",
"services": [
{ "name": "userService", "interface": "IUserService" }
]
}- Run the generator:
npx di-code-gen --config ./di.config.json- Generated structure:
src/di/
userService/
types.ts
registerUserService.tstypes.ts(empty ServicesList with augmentation elsewhere):
import { PreProcessDependencyContainerWithUse } from '@computerwwwizards/dependency-injection'
export interface ServicesList {}
export type ContainerCtx = PreProcessDependencyContainerWithUse<ServicesList>registerUserService.tsincludesIUserService, defaultregister()stub, amock()helper, and augmentsServicesList:
export interface IUserService {}
export default function register(ctx: ContainerCtx) {
// TODO: register real implementation
}
export function mock(ctx: ContainerCtx) {
// TODO: register mock for tests
}
declare module './types' {
interface ServicesList {
userService: IUserService
}
}
register.mock = mockProgrammatic API
ESM:
import { execute } from '@computerwwwizards/di-code-generator'
const exitCode = await execute([
'--config', './my-config.json',
'--output', './src/generated'
])CommonJS:
const { execute } = require('@computerwwwizards/di-code-generator')
const exitCode = await execute(['--config', './config.json'])Platform Support
Includes pre-compiled binaries for Linux, macOS, and Windows (x64, arm64).
