TypeScript code metrics CLI tool - A wrapper for tsg CLI that provides complexity analysis with configurable thresholds, colored output, and CI/CD integration.
- Auto-discovery: Automatically detects TypeScript files via git diff or explicit paths
- Colored Output: Visual indicators (green/yellow/red) for code complexity zones
- Configurable Thresholds: Customize maintainability and complexity limits
- Multiple Output Formats: Human-readable tables or LLM-friendly JSON
- CI/CD Integration: Exit codes for automated quality gates
- Multi-project Support: Handles monorepos with multiple tsconfig files
- Intelligent Filtering: By default, shows only files needing attention (yellow/red zones)
typescript-metrics analyzes three key complexity metrics:
| Metric | Description | Range |
|---|---|---|
| Maintainability Index (MI) | Overall code maintainability | 0-100 (higher is better) |
| Cyclomatic Complexity (CC) | Control flow complexity (decision points) | 1+ (lower is better) |
| Cognitive Complexity (CoC) | Mental effort to understand code | 1+ (lower is better) |
Prerequisite: This tool requires typescript-graph (tsg CLI) as a peer dependency.
npm install -g typescript-metrics typescript-graphNote: The tsm command is available as a shorthand alias for typescript-metrics. Both commands work identically:
typescript-metrics --help # Full command name
tsm --help # Shorthand alias (equivalent)# Install tsg globally first (required)
npm install -g typescript-graph
# Then use npx for typescript-metrics
npx typescript-metrics [options] [path...]
# or
npx tsm [options] [path...]npm install --save-dev typescript-metrics typescript-graphThen add to your package.json scripts:
{
"scripts": {
"metrics": "typescript-metrics",
"metrics:all": "typescript-metrics --all src/",
"tsm": "tsm",
"tsm:all": "tsm --all src/"
}
}Tip: Use tsm as a shorthand for typescript-metrics in all examples below.
# Analyze only TypeScript files that have changed
typescript-metrics
# or
tsm# Analyze a directory
typescript-metrics src/
# or
tsm src/
# Analyze specific files
typescript-metrics src/utils/date.ts src/helpers/format.ts
# Show all files (disable filtering)
typescript-metrics --all src/# Output metrics as JSON
typescript-metrics --json src/ > metrics.json
# or
tsm --json src/ > metrics.jsonCreate a .typescript-metrics.rc file in your project root to customize thresholds and tsconfig paths.
# .typescript-metrics.rc
# TypeScript configuration directories
TSCONFIGS=(
"." # Root project
"packages/shared" # Shared library
"packages/server" # Backend server
)
# Maintainability Index (MI) - Higher is better
MI_YELLOW_MAX=40 # Yellow zone: <= 40
MI_RED_MAX=20 # Red zone: <= 20
# Cyclomatic Complexity (CC) - Lower is better
CC_YELLOW_MIN=11 # Yellow zone: >= 11
CC_RED_MIN=21 # Red zone: >= 21
# Cognitive Complexity (CoC) - Lower is better
COC_YELLOW_MIN=11 # Yellow zone: >= 11
COC_RED_MIN=21 # Red zone: >= 21typescript-metrics [OPTIONS] [PATH...]| Argument | Description |
|---|---|
PATH |
Path(s) to analyze. Can be absolute or relative paths. Supports files (.ts) or directories containing .ts files. If omitted, defaults to git diff mode (analyzes changed files only). |
| Option | Description |
|---|---|
--help |
Show help message and exit |
--all |
Show all files (disable yellow/red zone filtering) |
--json |
Output metrics as JSON instead of text table |
--red |
Show only red-zone files (works with text or JSON output) |
# Analyze only changed TypeScript files
typescript-metrics# Analyze specific directory
typescript-metrics src/
# Show all files in directory
typescript-metrics --all src/
# Show only red-zone files (needs immediate attention)
typescript-metrics --red src/
# JSON output for LLM consumption
typescript-metrics --json src/
# JSON output with only red-zone files (for CI gating)
typescript-metrics --red --json src/# Analyze specific file
typescript-metrics src/utils/date.ts
# Analyze multiple files
typescript-metrics src/utils/date.ts src/helpers/format.ts# Fail build if red-zone files detected
typescript-metrics --json src/
if [ $? -eq 2 ]; then
echo "Error: Red-zone files detected!"
exit 1
fiHuman-readable output with colored status indicators:
FILE MI CC CoC Status
----------------------------------------------------- ------- ----- ----- --------
shared/test-lib/ticket/file-ticket-creator.ts 22.77 33 54 RED
shared/test-lib/ticket/ticket-creator.ts 40.88 12 12 YLW
shared/utils/date.ts 67.45 5 3 GRN
Column Legend:
- MI: Maintainability Index (0-100, higher is better)
- CC: Cyclomatic Complexity (lower is better)
- CoC: Cognitive Complexity (lower is better)
- Status: Overall health indicator (GRN/YLW/RED)
LLM-friendly JSON output for automated processing:
typescript-metrics --json src/Output:
{
"metrics": [
{
"filePath": "shared/utils/date.ts",
"maintainabilityIndex": 67.45,
"cyclomaticComplexity": 5,
"cognitiveComplexity": 3,
"zone": "GRN"
}
]
}Filtering modes:
- Default: Show yellow + red zones
--red: Show only red zone--all: Show all zones (green + yellow + red)
| Code | Meaning | Use Case |
|---|---|---|
| 0 | Success (no red-zone files) | Build passes, code quality acceptable |
| 1 | Error (invalid arguments, missing dependencies) | Configuration issue, missing tsg CLI |
| 2 | Red-zone files detected | CI/CD quality gate failure |
#!/bin/bash
# Example CI/CD script
typescript-metrics --json
EXIT_CODE=$?
if [ $EXIT_CODE -eq 2 ]; then
echo "❌ Quality gate failed: Red-zone files detected"
echo "Please refactor high-complexity files before merging."
exit 1
elif [ $EXIT_CODE -eq 1 ]; then
echo "❌ Error running metrics check"
exit 1
else
echo "✅ Quality gate passed"
fi| Zone | Range | Description | Action |
|---|---|---|---|
| Green | ≥ 41 | Well-maintained | No action needed |
| Yellow | 21-40 | Moderate concerns | Consider refactoring |
| Red | 0-20 | Significant issues | Refactoring required |
| Zone | Range | Description | Action |
|---|---|---|---|
| Green | ≤ 10 | Simple control flow | No action needed |
| Yellow | 11-20 | Moderately complex | Consider simplifying |
| Red | ≥ 21 | Highly complex | Refactoring required |
| Zone | Range | Description | Action |
|---|---|---|---|
| Green | ≤ 10 | Easy to understand | No action needed |
| Yellow | 11-20 | Moderate effort | Consider simplifying |
| Red | ≥ 21 | Difficult to understand | Refactoring required |
name: Code Quality Check
on:
pull_request:
paths:
- '**.ts'
- '**.tsx'
jobs:
metrics:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm install -g typescript-graph
- run: npx typescript-metrics --json
# Exit code 2 (red-zone files) will fail the workflow- typescript-graph (tsg CLI): Core TypeScript analysis engine
- jq: JSON processing (for text table formatting)
# Install tsg (required)
npm install -g typescript-graph
# Install jq (required on macOS/Linux)
# macOS
brew install jq
# Linux (Debian/Ubuntu)
sudo apt-get install jq
# Linux (RHEL/CentOS)
sudo yum install jq| Error | Cause | Solution |
|---|---|---|
tsg: command not found |
typescript-graph not installed | Run npm install -g typescript-graph |
Cannot find tsconfig |
Missing tsconfig.json | Create tsconfig.json or update .typescript-metrics.rc |
No TypeScript files changed |
No .ts files in git diff | Modify a .ts file or provide explicit path |
Path does not exist |
Invalid path argument | Check path spelling and location |
typescript-metrics --all --json | \
jq -r '.metrics | sort_by(.maintainabilityIndex) | .[] | "\(.filePath): \(.maintainabilityIndex)"'# Feed metrics to Claude, GPT-4, etc.
typescript-metrics --json src/ | \
jq -r '.metrics[] | "File: \(.filePath)\nMI: \(.maintainabilityIndex), CC: \(.cyclomaticComplexity)\n"' | \
llm "Review these files and suggest refactoring"See docs/ARCHITECTURE.md for implementation details.
MIT
Contributions welcome! Please open an issue or submit a pull request.
- typescript-graph - TypeScript code analysis and dependency visualization tool
- Issues: Report bugs and feature requests on GitHub
- Documentation: See inline help:
typescript-metrics --help