How carlrannaberg/codebase-map works
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: cli.ts Command dispatcher · indexer.ts Indexer · index.ts Project index · file-discovery.ts Source discovery
Components 18
CLI control plane
- CLI executable
bin/codebase-map - Command dispatcher · Runs scan, format, update, and list workflows.
src/cli.ts - Compact CLI
src/cli-compact.ts - Project root discovery · Finds the project root or existing index from nested directories.
src/utils/find-project-root.ts - Library public API
src/index.ts
Repository analysis
- Source discovery · Enumerates eligible source files before analysis.
src/core/file-discovery.ts - Pattern infrastructure · Caches, validates, diagnoses, and analyzes include/exclude patterns.
src/utils/pattern-cache.ts - Pattern performance analysis
src/utils/pattern-analysis.ts - AST parser · Extracts declarations, symbols, imports, and exports.
src/parsers/ast-parser.ts - AST representation
src/types/ast.ts
Index and views
- Indexer · Coordinates full scans and targeted incremental updates.
src/core/indexer.ts - Dependency resolver
src/core/dependency-resolver.ts - Project index · Shared model consumed by formatting and structural views.
src/types/index.ts - Persisted index · Separates expensive analysis from fast read operations.
- Tree builder
src/core/tree-builder.ts
Output and integrations
- Index formatter · Produces JSON, DSL, graph, Markdown, or tree output.
src/core/index-formatter.ts - Index filter · Creates focused views without rescanning.
src/utils/project-index-filter.ts - Shell and Claude consumers · Receives stdout output; diagnostics are sent to stderr.
.claude/settings.json
Connections 20
- 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