npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@genzai/templates

v0.1.8

Published

Directory-based project templates for Genzai

Readme

@genzai/templates

Directory-based project templates for Genzai.

Overview

@genzai/templates provides a simple way to create code generators from directory structures. It walks template directories, executes .gen.ts generator files, and copies static files, returning a standard Generator that works seamlessly with Genzai's generate() function.

Installation

npm install @genzai/templates @genzai/core tsx

Note: tsx is required as a peer dependency to load TypeScript generator files at runtime.

Usage

Basic Example

import { generate } from "@genzai/core";
import { template } from "@genzai/templates";

// Create a generator that uses template
const myAppGenerator = async (ctx) => template("./templates/my-app", ctx);

// Use it like any other generator
await generate(myAppGenerator, "./output", {
  projectName: "my-app",
  apiUrl: "https://api.example.com",
});

Template Directory Structure

A template directory can contain:

  • Static files: Copied as-is to the output
  • .gen.ts files: TypeScript files that export Generators
  • Directories: Recursively processed

Example structure:

templates/my-app/
  .genzai.json            # Optional config for regeneration strategies
  src/
    index.ts              # Static - copied as-is
    config.gen.ts         # Dynamic - executes generator
    components/
      Button.tsx          # Static
  package.json.gen.ts     # Dynamic
  README.md               # Static
  .gitignore              # Static

Configuring Regeneration Strategies

For static files that can't contain JSDoc markers (JSON, markdown, etc.), use a .genzai.json config file in the template root:

{
  "files": {
    "package.json": { "regenerationStrategy": "preserve-edits" },
    "src/**/*.ts": { "regenerationStrategy": "overwrite" }
  },
  "default": { "regenerationStrategy": "overwrite" }
}

Pattern matching:

  • Exact paths: "package.json"
  • Glob patterns: "src/**/*.ts" (matches any .ts file in src/)
  • * matches any characters except /
  • ** matches any characters including /

The config sets meta.regenerationStrategy on static file nodes, which is then used by regenerationStrategyMiddleware from @genzai/core.

Creating Generator Files (.gen.ts)

Generator files must export a default Generator function that returns an array of nodes:

// package.json.gen.ts
import { file } from "@genzai/core";

interface Context {
  projectName: string;
  dependencies: Record<string, string>;
}

export default (ctx: Context) => [
  file("package.json", () =>
    JSON.stringify(
      {
        name: ctx.projectName,
        version: "1.0.0",
        dependencies: ctx.dependencies,
      },
      null,
      2
    )
  ),
];

Composing with Other Generators

Templates can be composed with other Genzai generators:

import { generate, folder, file } from "@genzai/core";
import { template } from "@genzai/templates";

const myGenerator = async (ctx) => [
  folder("project", [
    ...(await template("./templates/base", ctx)),  // Load from directory
    file("custom.ts", () => "// custom"),          // Add extra file
  ]),
];

await generate(myGenerator, "./output", context);

With Middleware

Templates work seamlessly with Genzai middleware:

import { generate, defaultMiddleware } from "@genzai/core";
import { template } from "@genzai/templates";
import { prettierMiddleware } from "@genzai/prettier";
import { slotMiddleware } from "@genzai/slots";

const myAppGenerator = async (ctx) => template("./templates/my-app", ctx);

await generate(
  myAppGenerator,
  "./output",
  context,
  {
    middlewares: [...defaultMiddleware, slotMiddleware, prettierMiddleware],
  }
);

API

template(templatePath, context)

Loads a template directory and returns nodes.

Parameters:

  • templatePath (string): Path to template directory (absolute or relative)
  • context (C): Context object to pass to .gen.ts generators

Returns:

  • Promise<Node[]>: A promise that resolves to an array of nodes

Throws:

  • Error if path is empty or not a string
  • Error if path doesn't exist or isn't a directory
  • Error if .gen.ts files don't export valid Generators

Example:

const myGenerator = async (ctx) => template("./templates/my-app", ctx);
await generate(myGenerator, "./output", context);

How It Works

  1. Directory Walking: Recursively walks the template directory
  2. File Processing:
    • .gen.ts files: Dynamically loaded with tsx and executed as async Generators with the provided context
    • Directories: Recursively processed as folders
    • Other files: Copied as-is using lazy content loading
  3. Output: Returns an array of nodes that can be used in any generator

Error Handling

The package provides comprehensive validation and error messages:

  • Template path validation (exists, is directory, readable)
  • Generator file validation (valid TypeScript, exports default function)
  • File system error handling with context
  • Detailed error messages with file paths

License

MIT