# carlrannaberg/codebase-map architecture

> Architecture diagram of the GitHub repository carlrannaberg/codebase-map, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/carlrannaberg/codebase-map
- Repository: https://github.com/carlrannaberg/codebase-map

## Overview

#### Purpose and Entry Points
`codebase-map` is a Node.js 18+ TypeScript CLI that indexes TypeScript/JavaScript repositories into a persisted `.codebasemap`, then renders LLM-oriented project views. The executable is `bin/codebase-map`; command dispatch and CLI behavior live in `src/cli.ts`. `src/index.ts` and `src/core/index.ts` expose the library-facing public surface.

#### CLI Control Plane
The CLI provides four workflows: `scan` creates an index, `format` reads and filters an existing index, `update` incrementally replaces one file’s analysis, and `list` derives index views. Root/index-file discovery allows invocation from nested directories. Relevant paths: `src/cli.ts`, `src/cli-compact.ts`, `src/utils/find-project-root.ts`.

#### Discovery and File-Selection Boundary
Scanning begins by resolving a project root and enumerating eligible source files. Include/exclude glob patterns are applied at this filesystem boundary, preventing excluded files from reaching parsing and dependency analysis. Pattern infrastructure includes caching, validation, diagnostics, and performance analysis. Relevant paths: `src/core/file-discovery.ts`, `src/utils/pattern-cache.ts`, `src/utils/pattern-analysis.ts`.

#### AST Analysis Stage
Each discovered source file is parsed into an internal AST-derived representation that captures declarations, functions, classes, constants, imports, and exports. This separates language syntax handling from index assembly and formatter concerns. Relevant paths: `src/parsers/ast-parser.ts`, `src/parsers/index.ts`, `src/types/ast.ts`.

#### Index Assembly and Incremental Persistence
The indexer coordinates discovery, parsing, dependency construction, and serialization into the project index consumed by every later command. `update` can re-index a targeted file rather than requiring a complete scan. The persisted `.codebasemap` is the key boundary between potentially expensive repository analysis and fast read/format operations. Relevant paths: `src/core/indexer.ts`, `src/core/index.ts`, `src/types/common.ts`.

#### Dependency Graph and Structural Views
Import/export relationships are resolved into a dependency graph. The graph supports dependency-oriented listing and is also the basis for compressed graph output; tree construction provides a separate directory-structure projection. Relevant paths: `src/core/dependency-resolver.ts`, `src/core/tree-builder.ts`, `src/types/index.ts`.

#### Formatting and Consumption
Formatters transform a `ProjectIndex` into JSON, DSL, graph, Markdown, or tree output. Automatic selection favors DSL for projects up to 5,000 files and graph output above that threshold; format-time filtering creates focused views without rescanning. Output is stdout-oriented for shell pipelines and Claude hooks, while stats/diagnostics go separately to stderr. Relevant paths: `src/core/index-formatter.ts`, `src/utils/project-index-filter.ts`, `src/constants.ts`.

#### Runtime and External Boundaries
The application is a local Node.js CLI with filesystem access; it has no server, database, or required remote service. Its external integration surface is npm installation, shell use, and optional Claude Code session hooks configured under `.claude/settings.json`. Core behavior is implemented in TypeScript and compiled/executed through the package’s Node CLI distribution.

Main entry points: [`src/cli.ts`](https://github.com/carlrannaberg/codebase-map/blob/main/src/cli.ts) (Command dispatcher), [`src/core/indexer.ts`](https://github.com/carlrannaberg/codebase-map/blob/main/src/core/indexer.ts) (Indexer), [`src/types/index.ts`](https://github.com/carlrannaberg/codebase-map/blob/main/src/types/index.ts) (Project index), [`src/core/file-discovery.ts`](https://github.com/carlrannaberg/codebase-map/blob/main/src/core/file-discovery.ts) (Source discovery)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI control plane"]
  node_node_cli_bin{{"CLI executable<br/>Node CLI"}}
  node_node_cli["Command dispatcher<br/>CLI commands<br/>[cli.ts]"]
  node_node_compact_cli["Compact CLI<br/>CLI variant<br/>[cli-compact.ts]"]
  node_node_root_discovery["Project root discovery<br/>path resolution"]
  node_node_public_api["Library public API<br/>TypeScript exports<br/>[index.ts]"]
end

subgraph group_analysis["Repository analysis"]
  node_node_file_discovery["Source discovery<br/>filesystem selection<br/>[file-discovery.ts]"]
  node_node_patterns["Pattern infrastructure<br/>glob filtering<br/>[pattern-cache.ts]"]
  node_node_pattern_analysis["Pattern performance analysis<br/>glob diagnostics"]
  node_node_ast_parser["AST parser<br/>syntax analysis<br/>[ast-parser.ts]"]
  node_node_ast_types["AST representation<br/>TypeScript types<br/>[ast.ts]"]
end

subgraph group_index["Index and views"]
  node_node_indexer["Indexer<br/>index orchestration<br/>[indexer.ts]"]
  node_node_dependency_resolver["Dependency resolver<br/>import/export graph"]
  node_node_project_index["Project index<br/>index model<br/>[index.ts]"]
  node_node_persisted_index[("Persisted index<br/>local index file")]
  node_node_tree_builder["Tree builder<br/>directory projection<br/>[tree-builder.ts]"]
end

subgraph group_output["Output and integrations"]
  node_node_formatter["Index formatter<br/>view rendering<br/>[index-formatter.ts]"]
  node_node_filter["Index filter<br/>format-time filtering"]
  node_node_shell_output(("Shell and Claude consumers<br/>local integration<br/>[settings.json]"))
end

node_node_cli_bin -->|"starts"| node_node_cli
node_node_cli -.->|"supports"| node_node_compact_cli
node_node_cli -->|"resolves root/index"| node_node_root_discovery
node_node_cli -->|"scan or update"| node_node_indexer
node_node_cli -->|"reads for format/list"| node_node_persisted_index
node_node_public_api -.->|"exposes"| node_node_indexer
node_node_indexer -->|"discovers files"| node_node_file_discovery
node_node_file_discovery -->|"applies globs"| node_node_patterns
node_node_patterns -->|"analyzes patterns"| node_node_pattern_analysis
node_node_file_discovery -->|"selected files"| node_node_ast_parser
node_node_ast_parser -->|"creates"| node_node_ast_types
node_node_indexer -->|"builds dependencies"| node_node_dependency_resolver
node_node_ast_parser -->|"imports and exports"| node_node_dependency_resolver
node_node_indexer -->|"assembles"| node_node_project_index
node_node_dependency_resolver -->|"adds graph"| node_node_project_index
node_node_project_index -->|"serializes"| node_node_persisted_index
node_node_persisted_index -->|"loads"| node_node_formatter
node_node_project_index -->|"projects structure"| node_node_tree_builder
node_node_filter -->|"focused index view"| node_node_formatter
node_node_formatter -->|"stdout views"| node_node_shell_output

click node_node_cli_bin "https://github.com/carlrannaberg/codebase-map/blob/main/bin/codebase-map"
click node_node_cli "https://github.com/carlrannaberg/codebase-map/blob/main/src/cli.ts"
click node_node_compact_cli "https://github.com/carlrannaberg/codebase-map/blob/main/src/cli-compact.ts"
click node_node_root_discovery "https://github.com/carlrannaberg/codebase-map/blob/main/src/utils/find-project-root.ts"
click node_node_public_api "https://github.com/carlrannaberg/codebase-map/blob/main/src/index.ts"
click node_node_file_discovery "https://github.com/carlrannaberg/codebase-map/blob/main/src/core/file-discovery.ts"
click node_node_patterns "https://github.com/carlrannaberg/codebase-map/blob/main/src/utils/pattern-cache.ts"
click node_node_pattern_analysis "https://github.com/carlrannaberg/codebase-map/blob/main/src/utils/pattern-analysis.ts"
click node_node_ast_parser "https://github.com/carlrannaberg/codebase-map/blob/main/src/parsers/ast-parser.ts"
click node_node_ast_types "https://github.com/carlrannaberg/codebase-map/blob/main/src/types/ast.ts"
click node_node_indexer "https://github.com/carlrannaberg/codebase-map/blob/main/src/core/indexer.ts"
click node_node_dependency_resolver "https://github.com/carlrannaberg/codebase-map/blob/main/src/core/dependency-resolver.ts"
click node_node_project_index "https://github.com/carlrannaberg/codebase-map/blob/main/src/types/index.ts"
click node_node_tree_builder "https://github.com/carlrannaberg/codebase-map/blob/main/src/core/tree-builder.ts"
click node_node_formatter "https://github.com/carlrannaberg/codebase-map/blob/main/src/core/index-formatter.ts"
click node_node_filter "https://github.com/carlrannaberg/codebase-map/blob/main/src/utils/project-index-filter.ts"
click node_node_shell_output "https://github.com/carlrannaberg/codebase-map/blob/main/.claude/settings.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_cli_bin,node_node_cli,node_node_compact_cli,node_node_root_discovery,node_node_public_api toneBlue
class node_node_file_discovery,node_node_patterns,node_node_pattern_analysis,node_node_ast_parser,node_node_ast_types toneAmber
class node_node_indexer,node_node_dependency_resolver,node_node_project_index,node_node_persisted_index,node_node_tree_builder toneMint
class node_node_formatter,node_node_filter,node_node_shell_output toneRose
```

## Components

### CLI control plane

- [CLI executable](https://github.com/carlrannaberg/codebase-map/blob/main/bin/codebase-map): Node CLI · `bin/codebase-map`
- [Command dispatcher](https://github.com/carlrannaberg/codebase-map/blob/main/src/cli.ts): CLI commands · Runs scan, format, update, and list workflows. · `src/cli.ts`
- [Compact CLI](https://github.com/carlrannaberg/codebase-map/blob/main/src/cli-compact.ts): CLI variant · `src/cli-compact.ts`
- [Project root discovery](https://github.com/carlrannaberg/codebase-map/blob/main/src/utils/find-project-root.ts): path resolution · Finds the project root or existing index from nested directories. · `src/utils/find-project-root.ts`
- [Library public API](https://github.com/carlrannaberg/codebase-map/blob/main/src/index.ts): TypeScript exports · `src/index.ts`

### Repository analysis

- [Source discovery](https://github.com/carlrannaberg/codebase-map/blob/main/src/core/file-discovery.ts): filesystem selection · Enumerates eligible source files before analysis. · `src/core/file-discovery.ts`
- [Pattern infrastructure](https://github.com/carlrannaberg/codebase-map/blob/main/src/utils/pattern-cache.ts): glob filtering · Caches, validates, diagnoses, and analyzes include/exclude patterns. · `src/utils/pattern-cache.ts`
- [Pattern performance analysis](https://github.com/carlrannaberg/codebase-map/blob/main/src/utils/pattern-analysis.ts): glob diagnostics · `src/utils/pattern-analysis.ts`
- [AST parser](https://github.com/carlrannaberg/codebase-map/blob/main/src/parsers/ast-parser.ts): syntax analysis · Extracts declarations, symbols, imports, and exports. · `src/parsers/ast-parser.ts`
- [AST representation](https://github.com/carlrannaberg/codebase-map/blob/main/src/types/ast.ts): TypeScript types · `src/types/ast.ts`

### Index and views

- [Indexer](https://github.com/carlrannaberg/codebase-map/blob/main/src/core/indexer.ts): index orchestration · Coordinates full scans and targeted incremental updates. · `src/core/indexer.ts`
- [Dependency resolver](https://github.com/carlrannaberg/codebase-map/blob/main/src/core/dependency-resolver.ts): import/export graph · `src/core/dependency-resolver.ts`
- [Project index](https://github.com/carlrannaberg/codebase-map/blob/main/src/types/index.ts): index model · Shared model consumed by formatting and structural views. · `src/types/index.ts`
- **Persisted index**: local index file · Separates expensive analysis from fast read operations.
- [Tree builder](https://github.com/carlrannaberg/codebase-map/blob/main/src/core/tree-builder.ts): directory projection · `src/core/tree-builder.ts`

### Output and integrations

- [Index formatter](https://github.com/carlrannaberg/codebase-map/blob/main/src/core/index-formatter.ts): view rendering · Produces JSON, DSL, graph, Markdown, or tree output. · `src/core/index-formatter.ts`
- [Index filter](https://github.com/carlrannaberg/codebase-map/blob/main/src/utils/project-index-filter.ts): format-time filtering · Creates focused views without rescanning. · `src/utils/project-index-filter.ts`
- [Shell and Claude consumers](https://github.com/carlrannaberg/codebase-map/blob/main/.claude/settings.json): local integration · Receives stdout output; diagnostics are sent to stderr. · `.claude/settings.json`

## Connections

- CLI executable → Command dispatcher: starts
- Command dispatcher → Compact CLI: supports
- Command dispatcher → Project root discovery: resolves root/index
- Command dispatcher → Indexer: scan or update
- Command dispatcher → Persisted index: reads for format/list
- Library public API → Indexer: exposes
- Indexer → Source discovery: discovers files
- Source discovery → Pattern infrastructure: applies globs
- Pattern infrastructure → Pattern performance analysis: analyzes patterns
- Source discovery → AST parser: selected files
- AST parser → AST representation: creates
- Indexer → Dependency resolver: builds dependencies
- AST parser → Dependency resolver: imports and exports
- Indexer → Project index: assembles
- Dependency resolver → Project index: adds graph
- Project index → Persisted index: serializes
- Persisted index → Index formatter: loads
- Project index → Tree builder: projects structure
- Index filter → Index formatter: focused index view
- Index formatter → Shell and Claude consumers: stdout views
