← Back to Home

Getting Started

Install Clay and create your first code generator

Installation

Clay is a globally installed command-line tool. Install it using npm:

npm install -g clay-generator

Verify the installation by running:

clay --version

Prerequisites

Quick Start

1. Initialize a Project

Navigate to your project directory and initialize Clay:

cd my-project
clay init

This creates a .clay file that tracks all generated files.

2. Create a Model

Create a directory for your Clay configuration and model:

mkdir -p clay
cat > clay/model.json << 'EOF'
{
  "name": "user-service",
  "generators": ["./generators/api"],
  "model": {
    "types": [
      {
        "name": "User",
        "fields": [
          { "name": "id", "type": "string" },
          { "name": "email", "type": "string" },
          { "name": "name", "type": "string" }
        ]
      }
    ]
  }
}
EOF

3. Create a Generator

Create a simple generator that creates a model file:

mkdir -p clay/generators/api/templates
cat > clay/generators/api/generator.json << 'EOF'
{
  "partials": [],
  "steps": [
    {
      "generate": "templates/model.js",
      "select": "$.model.types[*]",
      "target": "src/models/{{kebabCase name}}.model.js"
    }
  ],
  "formatters": []
}
EOF

4. Create a Template

Create a Handlebars template for the generated files:

cat > clay/generators/api/templates/model.js << 'EOF'
// Generated Model: {{name}}

class {{pascalCase name}} {
  constructor(data) {
{{#each fields}}
    this.{{name}} = data.{{name}};
{{/each}}
  }

  toJSON() {
    return {
{{#each fields}}
      {{name}}: this.{{name}},
{{/each}}
    };
  }
}

module.exports = {{pascalCase name}};
EOF

5. Generate Code

Now generate your code:

clay generate clay/model.json ./

Or, since you have a .clay file, you can simply run:

clay generate

This creates src/models/user.model.js based on your model!

Project Structure

A typical Clay project looks like this:

.
├── .clay                      # Tracks generated files
├── clay/                      # Clay configuration
│   ├── model.json            # Your domain model
│   └── generators/           # Custom generators
│       └── api/
│           ├── generator.json
│           └── templates/
│               └── model.js
└── src/                      # Generated code appears here
    └── models/
        └── user.model.js

Basic Commands

Generate

Generate code from your model:

clay generate <model_path> <output_path>
clay generate  # Uses .clay file

Clean

Remove all generated files:

clay clean <model_path> <output_path>
clay clean  # Uses .clay file

Watch

Automatically regenerate when files change:

clay watch <model_path> <output_path>
clay watch  # Uses .clay file

Test Path

Test JSONPath expressions against your model:

clay test-path <model_path> "$.model.types[*].fields[*]"
💡 Pro Tip: Use the watch command during development to automatically regenerate code as you modify your model or templates!

Next Steps