Skip to main content

πŸ› οΈ Creating Custom Component Templates

Zero Guess Frontend allows you to generate custom components using {componentTemplate}.zgf.yaml templates.

1) Create config​

Add a .zgfconfig.json in the project root:

{
"components.zgf": {
"path": "./templates/"
},
"alias": {
"@components": "./src/components/"
}
}
  • components.zgf.path β€” path to your .zgf.yaml templates.
  • alias β€” shortcuts for output folders (you can add multiple aliases).
tip

Keep your templates in a dedicated folder like ./templates/ inside your repo.


2) Create a template file​

In the specified folder (e.g., ./templates/), create a file:

{componentTemplate}.zgf.yaml

Example file name:

ui-component.zgf.yaml

3) .zgf.yaml structure​

params:
componentName:
placeholder: "Input component name"
type: string
default: "MyComponent"
validator: "UpperCamelCase"

componentStyleExtension:
placeholder: "Select style extension"
type: enum
values:
- css
- scss
- module.scss
- module.css
default: "css"

addPublicApi:
placeholder: "Add public Api?"
type: boolean
default: true

files:
- name: "{{=componentName}}.tsx"
content: |
import "./{{=componentName}}.{{=componentStyleExtension}}";
import React from "react";

interface {{=componentName}}Props {
children: React.ReactNode;
}

const {{=componentName}}: React.FC<{{=componentName}}Props> = ({ children }) => {
return <div>{children}</div>;
};

export default {{=componentName}};

- name: "{{=componentName}}.{{=componentStyleExtension}}"
content: ""

- name: "index.ts"
condition: "{{=addPublicApi}}"
content: |
export { default as {{=componentName}} } from "./{{=componentName}}";

Key elements

  • params β€” parameters prompted from CLI.
  • files β€” files generated using the parameters.
  • {{=paramName}} β€” parameter substitution in file names/content.
  • condition β€” create file only if the expression is true.

4) Hooks (optional)​

You can run commands before/after generation and for each created file via hooks in .zgf.yaml.

Supported hooks​

Runs before any files are created.

Show example
hooks:
preGenerate:
- run: "echo Start scaffolding in {{=outputDir}}"

Step formats​

  • String β€” treated as a shell command
  • Object β€” fields described below
Step fields reference
FieldTypeDescription
runstringCommand to execute
cwdstring (optional)Working directory
shellboolean (optional)Run via shell (defaults to true for string steps)
continueOnErrorboolean (optional)Continue even if this step fails
conditionstring (optional)Template expression to decide whether to run the step
timeoutnumber (optional)Timeout in ms
envobject (optional)Environment variables (templated)
onErrorstringobject

Context available in hooks​

  • All your params (e.g., {{=componentName}})
  • outputDir β€” resolved generation target directory
  • templateDir β€” directory of the .zgf.yaml template

Error context in onError steps:

  • errorMessage β€” stringified error message
  • exitCode β€” process exit code
  • stdout / stderr β€” captured streams (if available)

Recipes​

  • [Auto‑format each file]

    hooks:
    afterEach:
    - run: "npx prettier --write {{=filePath}}"
  • [Barrel index after generate]

    hooks:
    postGenerate:
    - run: "node ./scripts/index-files.js --dir='{{=outputDir}}'"
    cwd: "{{=templateDir}}"
  • [Git add & commit if anything changed]

    hooks:
    postGenerate:
    - run: "git add -A && git diff --cached --quiet || git commit -m 'chore(zgf): scaffold {{=componentName}}'"
    cwd: "{{=outputDir}}"
    continueOnError: true

Troubleshooting​

  • [npx prettier / eslint not found] β€” ensure dev deps installed in the target project. Try npm i -D prettier eslint.
  • [Permission denied / EACCES] β€” check cwd is correct and you have rights to write there.
  • [Template expressions not substituted] β€” use {{=var}} syntax and ensure param names match params.
  • [onError not triggered] β€” only runs on a failing step (non‑zero exit code or thrown error).

Flow​

preGenerate β†’ create files β†’ afterEach (for every file) β†’ postGenerate

Advanced options​

Less common fields
  • shell: set to false to run without a shell (pass args explicitly)
  • condition: treat as boolean template expression; falsy skips the step
  • env: can reference template vars, e.g. COMPONENT: "{{=componentName}}"
  • continueOnError: prefer for non‑critical lint/format steps
info

If you use tools like Prettier/ESLint, ensure they are available in the target project (e.g., installed locally so npx can find them).


5) Generate component​

zgf g ui-component @components
  • ui-component β€” template name without .zgf.yaml.
  • @components β€” alias from .zgfconfig.json.

The command will generate the component in the target folder using your template.

warning

This feature is in active development. If you have issues β€” please create an issue in the repository.


πŸ—οΈ Project Structure​

Overview​

FolderPurpose
πŸ“ .github/GitHub Actions for npm deployment
βš™οΈ bin/CLI entry point
🧩 config/Dependency configs (react-router-dom, mobx, etc.)
🧠 core/Core CLI logic (setup/, scaffold/)
πŸ§ͺ examples/Sample .zgfconfig.json, demo component.zgf.yaml
πŸ› οΈ helpers/Helper utilities
πŸ“œ parser/YAML parser for custom components
πŸŽ›οΈ presets/Presets for project initialization
πŸ—οΈ templates/Project templates (FSD, Atomic, Empty, Router, State)
βœ… tests/Tests
πŸ”§ utils/Filesystem and prompt utilities
πŸ“¦ package.jsonPackage manifest
πŸ“˜ README.mdProject readme
πŸ“„ LICENSELicense
🀝 CONTRIBUTING.mdContribution guide
πŸ“œ CODE_OF_CONDUCT.mdCode of Conduct
πŸ” SECURITY.mdSecurity policy
Show full tree
β”œβ”€β”€ .github/             # GitHub Action for npm deployment
β”œβ”€β”€ bin/ # CLI entry point
β”œβ”€β”€ config/ # Config for dependencies (react-router-dom, mobx etc.)
β”œβ”€β”€ core/ # Core CLI logic
β”‚ β”œβ”€β”€ setup/ # Core modules
β”‚ └── scaffold/ # Generation modules (React etc.)
β”œβ”€β”€ examples/ # Example .zgfconfig.json, demo component.zgf.yaml and /components folder
β”œβ”€β”€ helpers/ # Helper functions
β”œβ”€β”€ parser/ # Yaml parser for your custom components
β”œβ”€β”€ presets/ # Presets for project init
β”œβ”€β”€ templates/ # Project templates (React/FSD, Atomic, Empty, React-Router-Dom, State managers)
β”œβ”€β”€ tests/ # Tests
β”œβ”€β”€ utils/ # Utilities (fs, user requests)
β”œβ”€β”€ package.json
β”œβ”€β”€ README.md
β”œβ”€β”€ LICENSE
β”œβ”€β”€ CONTRIBUTING.md
β”œβ”€β”€ CODE_OF_CONDUCT.md
└── SECURITY.md