Skip to main content
Custom Schematics

Custom Schematics Overview

Estimated Time: 3 hours | Difficulty: Advanced | Prerequisites: Angular CLI, TypeScript
Schematics are code generators that can create, modify, or delete files in your project. Think of them as “recipes” that the Angular CLI follows to scaffold files with your team’s exact conventions baked in. Instead of a new developer spending 20 minutes creating a component file, a spec file, a story file, and wiring up the barrel export — all while trying to match the team’s patterns — they run one command and get perfect boilerplate every time. The real value is consistency, not speed. Yes, schematics save time. But the bigger win is that every component, service, and feature module across your entire codebase follows the exact same structure. Code reviews get faster because reviewers are not nitpicking file organization — the schematic already enforced it.

Setting Up Schematics Project

Project Structure

Collection Configuration


Creating a Component Schematic

Schema Definition

The schema is the “contract” between the schematic and the user. It defines what options are available, their types, default values, and validation rules. The x-prompt property creates interactive prompts in the terminal, making your schematic feel like a first-class CLI tool. Good schemas make common cases easy (sensible defaults) and uncommon cases possible (optional flags).
Practical tip: Always provide a $default with "$source": "argv" for the primary argument (usually name). This lets users write ng g my-schematics:component button instead of ng g my-schematics:component --name=button — a small UX improvement that matters when you run the command dozens of times a day.

Factory Implementation

Template Files


AST Manipulation


ng-add Schematic


Testing Schematics


Custom Builders


Publishing & Using Schematics

Package.json Configuration


Best Practices

Use Templates

Keep templates in files/ folder for maintainability

Validate Input

Use JSON schema with validation and prompts

Test Thoroughly

Write unit tests for all schematics

Provide ng-add

Make installation seamless with ng-add

Next: Error Handling

Master global error handling patterns