Codemap Generator
Generate CODEMAP.md files that help humans and AI agents navigate codebases.
Output Separation
- CODEMAP.md = Auto-generated navigation map (this skill creates/updates)
- ARCHITECTURE.md = Hand-written design decisions (never touch)
Workflow
Phase 1: Project Analysis
Count files to determine project size:
- Use Glob with pattern
**/* excluding vendored paths
- Exclude:
node_modules/, vendor/, __pycache__/, target/, .terraform/, .*
- Count results to determine depth
Detect project type(s):
- Check for IaC patterns (Terraform, Ansible, K8s, etc.)
- Check for application code (package.json, go.mod, requirements.txt, etc.)
- Projects can be mixed (app + infra)
Set depth based on file count:
| Files |
Depth |
Output |
| <50 |
shallow |
Single root CODEMAP.md |
| 50-500 |
medium |
Root + key directories |
| >500 |
deep |
Root + 2 levels, skip vendored |
Phase 2: Existing File Check
STOP. Check for existing CODEMAP.md before proceeding.
Before generating, check if CODEMAP.md exists.
If exists: Use AskUserQuestion with options:
- Overwrite completely
- Merge/update (preserve manual additions)
- Abort
If not exists: Proceed to generation.
Do NOT proceed to Phase 3 until resolved.
Phase 3: Content Generation
Generate sections based on what's detected:
For Application Code
- Tech Stack - languages, frameworks, key dependencies
- Directory Structure - annotated tree with purpose per directory
- Entry Points - main files, CLI commands, API servers
- Code Architecture Diagram - mermaid showing component relationships
- Key Files Reference - important files with one-line descriptions
For Infrastructure Code
- IaC Stack - Terraform, Ansible, K8s, etc.
- Infrastructure Topology Diagram - mermaid showing resource relationships
- Module/Role Hierarchy Diagram - mermaid showing code organization
- Resource Inventory - what's managed, providers used
For Mixed Repos
Include both sections with clear delineation.
Phase 4: Output
Write CODEMAP.md to project root (and subdirectories if depth warrants).
Detection Patterns
Application Code
| Indicator |
Stack |
package.json |
Node.js/JavaScript/TypeScript |
go.mod |
Go |
Cargo.toml |
Rust |
requirements.txt, pyproject.toml, setup.py |
Python |
pom.xml, build.gradle |
Java |
Gemfile |
Ruby |
composer.json |
PHP |
*.csproj, *.sln |
.NET |
Infrastructure as Code
| Indicator |
Tool |
Analysis Method |
*.tf files |
Terraform |
Parse resources, modules, use terraform graph if initialized |
playbook*.yml + roles/ |
Ansible |
Map playbooks → roles → tasks |
kind: in YAML, kustomization.yaml |
Kubernetes |
Parse manifests, map services/deployments |
Chart.yaml |
Helm |
Parse templates, values |
Pulumi.yaml |
Pulumi |
Treat like application code |
AWSTemplateFormatVersion |
CloudFormation |
Parse resources, nested stacks |
docker-compose.yml |
Docker Compose |
Map services, networks, volumes |
Mermaid Diagram Patterns
Code Architecture (Component Relationships)
graph TD
subgraph "API Layer"
A[REST API]
B[GraphQL]
end
subgraph "Business Logic"
C[Services]
D[Domain Models]
end
subgraph "Data Layer"
E[Repositories]
F[Database]
end
A --> C
B --> C
C --> D
C --> E
E --> F
Infrastructure Topology
graph LR
subgraph "AWS"
ALB[Load Balancer]
subgraph "ECS Cluster"
SVC1[Service A]
SVC2[Service B]
end
RDS[(PostgreSQL)]
REDIS[(Redis)]
end
ALB --> SVC1
ALB --> SVC2
SVC1 --> RDS
SVC2 --> RDS
SVC1 --> REDIS
Module Hierarchy (Terraform)
graph TD
ROOT[Root Module]
ROOT --> VPC[modules/vpc]
ROOT --> ECS[modules/ecs]
ROOT --> RDS[modules/rds]
ECS --> SG[modules/security-groups]
RDS --> SG
Analysis Techniques
Import Graph (AST-based)
For JS/TS:
ast-grep --pattern 'import $_ from "$SOURCE"' --lang ts
ast-grep --pattern 'require("$SOURCE")' --lang js
For Python:
ast-grep --pattern 'from $MODULE import $_' --lang python
ast-grep --pattern 'import $MODULE' --lang python
For Go:
ast-grep --pattern 'import "$PKG"' --lang go
Entry Point Detection
| File Pattern |
Type |
main.go, main.py, main.ts |
Application entry |
index.ts, index.js |
Module entry |
cli.py, cli.ts, cmd/ |
CLI entry |
server.ts, app.py, api/ |
Server entry |
*_test.go, *.test.ts, test_*.py |
Test entry |
Terraform Resource Parsing
Extract from *.tf:
resource blocks → managed infrastructure
module blocks → dependencies
provider blocks → cloud targets
data blocks → external references
If .terraform/ exists and terraform init has been run, can use:
terraform graph | # convert DOT to mermaid
Note: terraform graph requires initialized state. Skip if .terraform/ missing or init incomplete.
Output Template
# Codemap
> Auto-generated navigation map. Last updated: {date}
> For design decisions, see ARCHITECTURE.md (if exists)
## Tech Stack
- **Languages**: {detected languages}
- **Frameworks**: {detected frameworks}
- **Infrastructure**: {detected IaC tools}
## Directory Structure
\`\`\`
{annotated tree}
\`\`\`
## Code Architecture
\`\`\`mermaid
{component diagram}
\`\`\`
## Infrastructure Topology
\`\`\`mermaid
{infra diagram}
\`\`\`
## Entry Points
| Entry | Purpose | Command |
|-------|---------|---------|
| {file} | {purpose} | {how to run} |
## Key Files
| File | Purpose |
|------|---------|
| {path} | {description} |
Edge Cases
- Monorepos: Detect workspace patterns (lerna, nx, turborepo, go workspaces), generate per-package maps
- No clear structure: Generate minimal map with warnings about organization
- Vendored code: Always exclude from analysis (node_modules, vendor, .terraform, pycache)
- Generated code: Detect and label (protobuf, OpenAPI, etc.)
1---2name: codemap-33description: Generate navigational codebase maps with architecture diagrams. Use when mapping a codebase, creating architecture docs, visualizing project structure, generating infrastructure diagrams, understanding repo layout, or onboarding to a new project.4---5
6# Codemap Generator
7
8Generate `CODEMAP.md` files that help humans and AI agents navigate codebases.
9
10## Output Separation
11
12- **CODEMAP.md** = Auto-generated navigation map (this skill creates/updates)
13- **ARCHITECTURE.md** = Hand-written design decisions (never touch)
14
15## Workflow
16
17### Phase 1: Project Analysis
18
191. **Count files** to determine project size:
20 - Use Glob with pattern `**/*` excluding vendored paths
21 - Exclude: `node_modules/`, `vendor/`, `__pycache__/`, `target/`, `.terraform/`, `.*`
22 - Count results to determine depth
23
242. **Detect project type(s)**:
25 - Check for IaC patterns (Terraform, Ansible, K8s, etc.)
26 - Check for application code (package.json, go.mod, requirements.txt, etc.)
27 - Projects can be mixed (app + infra)
28
293. **Set depth based on file count**:
30 | Files | Depth | Output |
31 |-------|-------|--------|
32 | <50 | shallow | Single root CODEMAP.md |
33 | 50-500 | medium | Root + key directories |
34 | >500 | deep | Root + 2 levels, skip vendored |
35
36### Phase 2: Existing File Check
37
38**STOP. Check for existing CODEMAP.md before proceeding.**
39
40Before generating, check if `CODEMAP.md` exists.
41
42**If exists**: Use `AskUserQuestion` with options:
43- Overwrite completely
44- Merge/update (preserve manual additions)
45- Abort
46
47**If not exists**: Proceed to generation.
48
49**Do NOT proceed to Phase 3 until resolved.**
50
51### Phase 3: Content Generation
52
53Generate sections based on what's detected:
54
55#### For Application Code
56
571. **Tech Stack** - languages, frameworks, key dependencies
582. **Directory Structure** - annotated tree with purpose per directory
593. **Entry Points** - main files, CLI commands, API servers
604. **Code Architecture Diagram** - mermaid showing component relationships
615. **Key Files Reference** - important files with one-line descriptions
62
63#### For Infrastructure Code
64
651. **IaC Stack** - Terraform, Ansible, K8s, etc.
662. **Infrastructure Topology Diagram** - mermaid showing resource relationships
673. **Module/Role Hierarchy Diagram** - mermaid showing code organization
684. **Resource Inventory** - what's managed, providers used
69
70#### For Mixed Repos
71
72Include both sections with clear delineation.
73
74### Phase 4: Output
75
76Write `CODEMAP.md` to project root (and subdirectories if depth warrants).
77
78## Detection Patterns
79
80### Application Code
81
82| Indicator | Stack |
83|-----------|-------|
84| `package.json` | Node.js/JavaScript/TypeScript |
85| `go.mod` | Go |
86| `Cargo.toml` | Rust |
87| `requirements.txt`, `pyproject.toml`, `setup.py` | Python |
88| `pom.xml`, `build.gradle` | Java |
89| `Gemfile` | Ruby |
90| `composer.json` | PHP |
91| `*.csproj`, `*.sln` | .NET |
92
93### Infrastructure as Code
94
95| Indicator | Tool | Analysis Method |
96|-----------|------|-----------------|
97| `*.tf` files | Terraform | Parse resources, modules, use `terraform graph` if initialized |
98| `playbook*.yml` + `roles/` | Ansible | Map playbooks → roles → tasks |
99| `kind:` in YAML, `kustomization.yaml` | Kubernetes | Parse manifests, map services/deployments |
100| `Chart.yaml` | Helm | Parse templates, values |
101| `Pulumi.yaml` | Pulumi | Treat like application code |
102| `AWSTemplateFormatVersion` | CloudFormation | Parse resources, nested stacks |
103| `docker-compose.yml` | Docker Compose | Map services, networks, volumes |
104
105## Mermaid Diagram Patterns
106
107### Code Architecture (Component Relationships)
108
109```mermaid
110graph TD
111 subgraph "API Layer"
112 A[REST API]
113 B[GraphQL]
114 end
115 subgraph "Business Logic"
116 C[Services]
117 D[Domain Models]
118 end
119 subgraph "Data Layer"
120 E[Repositories]
121 F[Database]
122 end
123 A --> C
124 B --> C
125 C --> D
126 C --> E
127 E --> F
128```
129
130### Infrastructure Topology
131
132```mermaid
133graph LR
134 subgraph "AWS"
135 ALB[Load Balancer]
136 subgraph "ECS Cluster"
137 SVC1[Service A]
138 SVC2[Service B]
139 end
140 RDS[(PostgreSQL)]
141 REDIS[(Redis)]
142 end
143 ALB --> SVC1
144 ALB --> SVC2
145 SVC1 --> RDS
146 SVC2 --> RDS
147 SVC1 --> REDIS
148```
149
150### Module Hierarchy (Terraform)
151
152```mermaid
153graph TD
154 ROOT[Root Module]
155 ROOT --> VPC[modules/vpc]
156 ROOT --> ECS[modules/ecs]
157 ROOT --> RDS[modules/rds]
158 ECS --> SG[modules/security-groups]
159 RDS --> SG
160```
161
162## Analysis Techniques
163
164### Import Graph (AST-based)
165
166For JS/TS:
167```bash
168ast-grep --pattern 'import $_ from "$SOURCE"' --lang ts
169ast-grep --pattern 'require("$SOURCE")' --lang js
170```
171
172For Python:
173```bash
174ast-grep --pattern 'from $MODULE import $_' --lang python
175ast-grep --pattern 'import $MODULE' --lang python
176```
177
178For Go:
179```bash
180ast-grep --pattern 'import "$PKG"' --lang go
181```
182
183### Entry Point Detection
184
185| File Pattern | Type |
186|--------------|------|
187| `main.go`, `main.py`, `main.ts` | Application entry |
188| `index.ts`, `index.js` | Module entry |
189| `cli.py`, `cli.ts`, `cmd/` | CLI entry |
190| `server.ts`, `app.py`, `api/` | Server entry |
191| `*_test.go`, `*.test.ts`, `test_*.py` | Test entry |
192
193### Terraform Resource Parsing
194
195Extract from `*.tf`:
196- `resource` blocks → managed infrastructure
197- `module` blocks → dependencies
198- `provider` blocks → cloud targets
199- `data` blocks → external references
200
201If `.terraform/` exists and `terraform init` has been run, can use:
202```bash
203terraform graph | # convert DOT to mermaid
204```
205
206**Note**: `terraform graph` requires initialized state. Skip if `.terraform/` missing or init incomplete.
207
208## Output Template
209
210```markdown
211# Codemap
212
213> Auto-generated navigation map. Last updated: {date}
214> For design decisions, see ARCHITECTURE.md (if exists)
215
216## Tech Stack
217
218- **Languages**: {detected languages}
219- **Frameworks**: {detected frameworks}
220- **Infrastructure**: {detected IaC tools}
221
222## Directory Structure
223
224\`\`\`
225{annotated tree}
226\`\`\`
227
228## Code Architecture
229
230\`\`\`mermaid
231{component diagram}
232\`\`\`
233
234## Infrastructure Topology
235
236\`\`\`mermaid
237{infra diagram}
238\`\`\`
239
240## Entry Points
241
242| Entry | Purpose | Command |
243|-------|---------|---------|
244| {file} | {purpose} | {how to run} |
245
246## Key Files
247
248| File | Purpose |
249|------|---------|
250| {path} | {description} |
251```
252
253## Edge Cases
254
255- **Monorepos**: Detect workspace patterns (lerna, nx, turborepo, go workspaces), generate per-package maps
256- **No clear structure**: Generate minimal map with warnings about organization
257- **Vendored code**: Always exclude from analysis (node_modules, vendor, .terraform, __pycache__)
258- **Generated code**: Detect and label (protobuf, OpenAPI, etc.)