New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@computerwwwizards/di-code-generator

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@computerwwwizards/di-code-generator

Code generator for @computerwwwizards/dependency-injection - generates TypeScript boilerplate for DI containers

latest
Source
npmnpm
Version
0.2.0
Version published
Maintainers
1
Created
Source

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
    • camelCase
    • PascalCase
    • snake_case
    • kebab-case
    • SCREAMING_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 test

Testing

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 test

Usage 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.md

API 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) ![]u8
  • toPascalCase(allocator, input) ![]u8
  • toSnakeCase(allocator, input) ![]u8
  • toKebabCase(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 insert
  • case: 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.

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-generator

CLI 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/di

Quick 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., userServiceIUserService):

# 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/di

Auto-inference examples:

  • userServiceIUserService
  • paymentGatewayIPaymentGateway
  • auth_serviceIAuthService
  • DBConnectionIDbconnection

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.ts
  • types.ts (empty ServicesList with augmentation elsewhere):
import { PreProcessDependencyContainerWithUse } from '@computerwwwizards/dependency-injection'

export interface ServicesList {}
export type ContainerCtx = PreProcessDependencyContainerWithUse<ServicesList>
  • registerUserService.ts includes IUserService, default register() stub, a mock() helper, and augments ServicesList:
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 = mock

Programmatic 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).

Keywords

dependency-injection

FAQs

Package last updated on 11 Dec 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts