π οΈ 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).
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β
- preGenerate
- afterEach
- postGenerate
Runs before any files are created.
Show example
hooks:
preGenerate:
- run: "echo Start scaffolding in {{=outputDir}}"
Runs after each file is created. Extra vars: fileName
, filePath
.
Show example
hooks:
afterEach:
- run: "echo Created {{=fileName}} at {{=filePath}}"
- run: "npx prettier --write {{=filePath}}"
condition: "{{=addPublicApi}}"
timeout: 20000
env:
COMPONENT: "{{=componentName}}"
FILE: "{{=fileName}}"
onError:
- run: "echo 'Prettier failed for {{=fileName}}: {{=errorMessage}}' >&2"
- run: "node ./scripts/report-hook-failure.js --file='{{=filePath}}' --code='{{=exitCode}}'"
cwd: "{{=templateDir}}"
continueOnError: true
Runs after all files are created. Extra var: createdFiles
(JSON string array of { filename, fullPath }
).
Show example
hooks:
postGenerate:
- run: "npx eslint --fix ."
cwd: "{{=outputDir}}"
timeout: 30000
env:
GEN_OUT_DIR: "{{=outputDir}}"
onError: "echo 'ESLint failed with code {{=exitCode}}' >&2"
- run: "node ./scripts/index-files.js --files='{{=createdFiles}}'"
cwd: "{{=templateDir}}"
continueOnError: true
Step formatsβ
- String β treated as a shell command
- Object β fields described below
Step fields reference
Field | Type | Description |
---|---|---|
run | string | Command to execute |
cwd | string (optional) | Working directory |
shell | boolean (optional) | Run via shell (defaults to true for string steps) |
continueOnError | boolean (optional) | Continue even if this step fails |
condition | string (optional) | Template expression to decide whether to run the step |
timeout | number (optional) | Timeout in ms |
env | object (optional) | Environment variables (templated) |
onError | string | object |
Context available in hooksβ
- All your params (e.g.,
{{=componentName}}
) outputDir
β resolved generation target directorytemplateDir
β directory of the.zgf.yaml
template
Error context in onError
steps:
errorMessage
β stringified error messageexitCode
β process exit codestdout
/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 matchparams
. - [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 tofalse
to run without a shell (pass args explicitly)condition
: treat as boolean template expression; falsy skips the stepenv
: can reference template vars, e.g.COMPONENT: "{{=componentName}}"
continueOnError
: prefer for nonβcritical lint/format steps
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.
This feature is in active development. If you have issues β please create an issue in the repository.
ποΈ Project Structureβ
Overviewβ
Folder | Purpose |
---|---|
π .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.json | Package manifest |
π README.md | Project readme |
π LICENSE | License |
π€ CONTRIBUTING.md | Contribution guide |
π CODE_OF_CONDUCT.md | Code of Conduct |
π SECURITY.md | Security 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