- Update package description and fix mainProgram typo - Rewrite documentation to describe project switching, not process viewing - Add PROJECT_FOLDERS configuration and usage examples - Update all references across docs (README, guides, module overviews)
3.8 KiB
3.8 KiB
code2prompt
A CLI tool that converts your codebase into a single LLM prompt with a source tree, prompt templating, and token counting.
Description
code2prompt is a command-line tool designed to help developers prepare their codebases for analysis by Large Language Models (LLMs). It creates a comprehensive prompt that includes:
- Source code tree structure
- Concatenated file contents
- Prompt templates
- Token counting for context management
Features
- 📁 Source Tree Generation: Visual representation of your codebase structure
- 📝 Code Concatenation: Merges multiple files into a single prompt
- 🧩 Prompt Templates: Customizable prompt templates
- 🔢 Token Counting: Accurate token counting for various LLMs
- 🎯 Selective Inclusion: Choose specific files and directories
- 🔒 Smart Filtering: Exclude files by pattern (.git, node_modules, etc.)
Installation
Via Overlay
{pkgs, ...}: {
environment.systemPackages = with pkgs; [
code2prompt
];
}
Direct Reference
{pkgs, ...}: {
environment.systemPackages = with pkgs; [
inputs.m3ta-nixpkgs.packages.${pkgs.system}.code2prompt
];
}
Run Directly
nix run git+https://code.m3ta.dev/m3tam3re/nixpkgs#code2prompt
Usage
Basic Usage
# Generate prompt for current directory
code2prompt
# Generate for specific directory
code2prompt /path/to/project
# Output to file
code2prompt -o prompt.txt
# Use custom template
code2prompt --template my_template.md
Common Options
# Include specific files
code2prompt --include "*.py" "*.js"
# Exclude specific files
code2prompt --exclude "*.test.*" "node_modules/*"
# Generate source tree only
code2prompt --tree-only
# Add custom context
code2prompt --context "This is a Node.js project"
Examples
Prepare Codebase for GPT-4
code2prompt \
--template gpt4-template.md \
--include "*.ts" "*.tsx" \
--exclude "node_modules" "*.test.ts" \
--context "Review this TypeScript codebase" \
-o codebase_prompt.txt
# Then feed to GPT-4
cat codebase_prompt.txt
Analyze Specific Directory
# Analyze only src directory
code2prompt src/ \
--include "*.rs" \
--context "Analyze this Rust codebase" \
-o src_analysis.txt
Create Source Tree
# Generate only source tree
code2prompt --tree-only > tree.txt
cat tree.txt
Use Cases
Code Review
Prepare code for AI-assisted code review:
code2prompt \
--template code-review.md \
--include "*.py" \
--context "Review this Python code for security issues" \
-o review_prompt.txt
Documentation Generation
Generate documentation using LLMs:
code2prompt \
--template docs-template.md \
--include "*.js" "*.md" \
--context "Generate API documentation" \
-o docs_prompt.txt
Code Migration
Prepare code for migration assistance:
code2prompt \
--template migration-template.md \
--include "*.js" \
--context "Migrate this JavaScript to TypeScript" \
-o migration_prompt.txt
Configuration
Environment Variables
CODE2PROMPT_TEMPLATE_DIR: Directory containing custom templatesCODE2PROMPT_DEFAULT_TEMPLATE: Default template to use
Template Files
Custom templates can include:
# Codebase Analysis
## Context
{context}
## Directory Tree
{tree}
## Code
{code}
## Instructions
{instructions}
Build Information
- Version: 4.0.2
- Language: Rust
- License: MIT
- Source: GitHub
Dependencies
openssl- Secure communicationpkg-config- Build configuration
Platform Support
- Linux (primary)
- macOS (may work)
- Windows (not tested)
Related
- Adding Packages - How to add new packages
- Quick Start - Getting started guide