scaffold.js

ScaffoldJS πŸ—οΈ

npm version build License: MIT

A powerful CLI tool for scaffolding code templates with multi-variable support. Generate consistent, organized code structures across your projects with customizable templates and flexible variable transformations.

✨ Features

πŸš€ Quick Start

# Install globally
npm install -g scaffoldjs-cli

# Or use directly with npx
npx scaffold help

Initialize Your Project

# Set up configuration and default templates
npx scaffold setup

# View available templates
npx scaffold list

# Get help
npx scaffold help

# View comprehensive guide
npx scaffold guide

πŸ“– Usage

Basic Commands

# Generate a single file
npx scaffold make user controller

# Generate with custom variables
npx scaffold make table controller -v module=restaurant feature=dining

# Generate multiple files using a group
npx scaffold make booking crud-group -v module=hotel feature=reservations

# Inject code into existing files
npx scaffold inject import-statement src/app.module.ts -v module=auth

# Force overwrite existing files
npx scaffold make order service -v module=ecommerce --force

Command Reference

Command Description Example
setup Initialize scaffoldjs in your project npx scaffold setup
list Show available templates and groups npx scaffold list
make <resource> <template> Generate new files from templates npx scaffold make user controller
inject <template> <file> Inject code into existing files npx scaffold inject method src/user.service.ts
help Display help information npx scaffold help
guide Show comprehensive usage guide npx scaffold guide

Options

Option Short Description Example
--vars -v Custom variables in key=value format -v module=auth feature=users
--force -f Force overwrite existing files --force
--target -t Target point for injection -t "// inject:methods"
--resource -r Resource name for injection -r user

βš™οΈ Configuration

scaffold.config.json

The configuration file defines your templates, injection points, and groups:

{
  "version": 1,
  "templates": {
    "controller": {
      "description": "Generate a controller file",
      "src": "./scaffolds/controller.tp",
      "dest": "./src//controllers/.controller.ts"
    },
    "service": {
      "description": "Generate a service file", 
      "src": "./scaffolds/service.tp",
      "dest": "./src//services/.service.ts"
    },
    "import-statement": {
      "description": "Import statement for injection",
      "src": "./scaffolds/-import.tp",
      "dest": "inject"
    },
    "crud-group": [
      "controller",
      "service", 
      "dto",
      "entity"
    ]
  }
}

Template Structure

your-project/
β”œβ”€β”€ scaffold.config.json
β”œβ”€β”€ scaffolds/
β”‚   β”œβ”€β”€ controller.tp
β”‚   β”œβ”€β”€ service.tp
β”‚   β”œβ”€β”€ dto.tp
β”‚   └── auth-import.tp
└── src/
    └── (generated files will go here)

πŸ”§ Variables & Transformations

Built-in Variables

Available Transformations

Transformation Example Input Example Output
raw user-account user-account
singular users user
plural user users
lowerCase UserAccount useraccount
upperCase user USER
camelCase user-account userAccount
pascalCase user-account UserAccount
snakeCase user-account user_account
spaceCase user-account user account
hyphenCase user account user-account
sentenceCase user-account Useraccount

Chaining Transformations

     // user-accounts β†’ UserAccount
          // auth β†’ AUTHS
       // dining-tables β†’ diningTable

πŸ“ Template Examples

Controller Template (scaffolds/controller.tp)

import { Controller, Get, Post, Put, Delete, Body, Param } from '@nestjs/common';
import { Service } from '../services/.service';
import { CreateDto } from '../dto/.dto';

@Controller('//')
export class Controller {
  constructor(
    private readonly Service: Service
  ) {}

  @Get()
  async findAll() {
    return this.Service.findAll();
  }

  @Post()
  async create(@Body() dto: CreateDto) {
    return this.Service.create(dto);
  }
}

// Generated for  module,  feature

Import Injection Template (scaffolds/auth-import.tp)

import { Module } from './/.module';

🎯 Real-World Examples

E-commerce Application

# Generate product management
npx scaffold make product crud-group -v module=catalog feature=products

# Generate order processing
npx scaffold make order service -v module=sales feature=checkout

# Add authentication
npx scaffold make user controller -v module=auth feature=users

Generated structure:

src/
β”œβ”€β”€ catalog/
β”‚   β”œβ”€β”€ controllers/product.controller.ts
β”‚   β”œβ”€β”€ services/product.service.ts
β”‚   β”œβ”€β”€ dto/product.dto.ts
β”‚   └── entities/product.entity.ts
β”œβ”€β”€ sales/
β”‚   └── services/order.service.ts
└── auth/
    └── controllers/user.controller.ts

Hotel Management System

# Room management
npx scaffold make room full-feature -v module=hotel feature=rooms

# Booking system  
npx scaffold make booking crud-group -v module=reservations feature=bookings

# Guest services
npx scaffold make guest service -v module=hospitality feature=guests

Multi-tenant SaaS

# Tenant management
npx scaffold make tenant controller -v module=core domain=admin

# Feature modules per tenant
npx scaffold make dashboard service -v module=analytics domain=tenant

# Billing system
npx scaffold make invoice crud-group -v module=billing domain=finance

πŸ’‰ Code Injection

Inject code snippets into existing files at specific points:

# Inject import statements
npx scaffold inject import-statement src/app.module.ts -v module=auth

# Inject methods into services
npx scaffold inject crud-methods src/user.service.ts -r user

# Inject routes with target point
npx scaffold inject api-route src/app.routes.ts -t "// inject:routes" -v module=auth

Injection Targets

πŸ”„ Migration from Single Variable

Existing templates using only `` variables continue to work seamlessly:

// Old template - still works
export class Service {}

// New multi-variable template
export class Service {
  // Module: 
  // Feature: 
}

πŸ› οΈ Advanced Configuration

Dynamic Template Paths

{
  "templates": {
    "feature-controller": {
      "src": "./scaffolds//controller.tp",
      "dest": "./src///.controller.ts"
    }
  }
}

Conditional Templates

{
  "templates": {
    "api-controller": {
      "src": "./scaffolds//api-controller.tp", 
      "dest": "./src//controllers/.controller.ts"
    },
    "web-controller": {
      "src": "./scaffolds//web-controller.tp",
      "dest": "./src//controllers/.controller.ts" 
    }
  }
}

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ†˜ Support

β˜•οΈ Support the Project

If ScaffoldJS has saved you time and helped improve your development workflow, consider buying us a coffee! Your support helps us maintain and improve the project.

Buy Me A Coffee

Other ways to support:

Every contribution, no matter how small, helps make ScaffoldJS better for everyone! πŸ™

πŸ—ΊοΈ Roadmap


Made with ❀️ by Ebuka Odini

Scaffold smarter, code faster! πŸš€