@formatr/cli
v1.0.0
Published
CLI tools for the formatr template library
Maintainers
Readme
@formatr/cli
Command Line Interface for the formatr template library.
Installation
npm install -g @formatr/cli
# or
npm install --save-dev @formatr/cliQuick Start
# Render a template
echo "Hello {name}!" | formatr render --data '{"name":"World"}'
# Validate templates
formatr validate ./templates --recursive
# Start the playground
formatr playgroundCommands
formatr render
Render templates with provided data.
# Render from file with JSON data
formatr render template.fmt --data data.json --output result.txt
# Render from stdin
echo "Hello {name}!" | formatr render --data '{"name":"World"}'
# Render with YAML data
formatr render template.fmt --data config.yaml
# Render multiple templates
formatr render ./templates --data data.json --output-dir ./dist --recursive
# Watch mode
formatr render template.fmt --data data.json --watchformatr validate
Validate template syntax and structure.
# Validate single template
formatr validate template.fmt
# Validate directory
formatr validate ./templates --recursive
# Validate with context (check placeholders exist)
formatr validate template.fmt --context data.json
# Strict mode (treat warnings as errors)
formatr validate template.fmt --strict
# Output as JSON for CI
formatr validate template.fmt --jsonformatr analyze
Analyze template complexity and characteristics.
# Analyze single template
formatr analyze template.fmt
# Analyze with detailed metrics
formatr analyze template.fmt --detailed
# Compare multiple templates
formatr analyze template-v1.fmt template-v2.fmt --compare
# Output as JSON with threshold for CI
formatr analyze template.fmt --json --threshold complexity=10formatr benchmark
Benchmark template rendering performance.
# Basic benchmark
formatr benchmark template.fmt --data data.json
# Multiple iterations
formatr benchmark template.fmt --data data.json --iterations 10000
# Compare implementations
formatr benchmark template-v1.fmt template-v2.fmt --data data.json
# Memory profiling
formatr benchmark template.fmt --data data.json --memory
# Generate HTML report
formatr benchmark template.fmt --data data.json --report benchmark.htmlformatr report
Generate reports from templates and data.
# Generate HTML report
formatr report --template report.fmt --data results.json --format html
# Generate markdown report
formatr report --template report.fmt --data results.json --format markdown
# Batch report generation
formatr report --template report.fmt --data-dir ./data --output-dir ./reports
# Custom styles
formatr report --template report.fmt --data data.json --format html --style custom.cssformatr watch
Watch templates and data files for changes.
# Watch single template
formatr watch template.fmt --data data.json --output result.txt
# Watch directory
formatr watch ./templates --data-dir ./data --output-dir ./dist
# Watch with live reload server
formatr watch template.fmt --data data.json --serve --port 3000
# Watch with custom debounce
formatr watch template.fmt --data data.json --debounce 500formatr init
Initialize a new formatr project.
# Interactive initialization
formatr init
# Initialize with template
formatr init my-project --template basic
# Initialize TypeScript project
formatr init my-project --template typescript
# Create config file only
formatr init --config-onlyAvailable templates:
basic- Basic formatr projecttypescript- TypeScript project with typesadvanced- Advanced project with examplesdocumentation- Documentation project
formatr format
Format template files according to style guidelines.
# Format single file
formatr format template.fmt
# Format directory
formatr format ./templates --recursive
# Check only (don't modify)
formatr format template.fmt --check
# Format with specific style
formatr format template.fmt --style compactformatr lint
Lint templates for best practices and potential issues.
# Lint single template
formatr lint template.fmt
# Lint directory
formatr lint ./templates --recursive
# Fix auto-fixable issues
formatr lint template.fmt --fix
# Output as JSON for CI
formatr lint template.fmt --jsonformatr playground
Launch an interactive playground.
# Launch playground in browser
formatr playground
# Launch on custom port
formatr playground --port 8080
# Launch with initial template
formatr playground --template examples/basic.fmt
# Launch in CLI mode (REPL)
formatr playground --cliConfiguration
The CLI supports configuration files in the following order of precedence:
.formatrrc(JSON).formatrrc.json.formatrrc.yaml/.formatrrc.ymlformatr.config.js/formatr.config.mjs/formatr.config.cjs
Example .formatrrc.json:
{
"templateExtension": ".fmt",
"outputDir": "./dist",
"locale": "en-US",
"lint": {
"extends": "recommended",
"rules": {
"no-empty-placeholders": "error"
}
},
"format": {
"style": "expanded",
"indentation": 2
},
"watch": {
"debounce": 300,
"extensions": [".fmt", ".json", ".yaml"]
}
}Exit Codes
The CLI uses standard exit codes for CI/CD integration:
0- Success1- Command failure (e.g., validation errors, render errors)2- Invalid usage (e.g., missing required arguments)
Environment Variables
NO_COLOR- Disable colored outputFORCE_COLOR- Force colored outputDEBUG- Enable debug logging
CI/CD Integration
GitHub Actions
steps:
- name: Validate templates
run: formatr validate ./templates --recursive --json > validation.json
- name: Lint templates
run: formatr lint ./templates --recursive --json > lint.json
- name: Check complexity
run: formatr analyze ./templates --json --threshold complexity=10Pre-commit Hook
{
"scripts": {
"precommit": "formatr lint ./templates --recursive && formatr format ./templates --check"
}
}Troubleshooting
Colors not displaying
Try setting FORCE_COLOR=1 or ensure your terminal supports ANSI colors.
YAML support not working
Install the optional yaml package:
npm install yamlWatch mode not working
Install the optional chokidar package:
npm install chokidarInteractive mode not working
Install the optional inquirer package:
npm install inquirerLicense
MIT
