gen-context
v1.0.3
Published
A tool to create a context for AI prompts from files.
Readme
gen-context
gen-context is a simple yet powerful command-line tool designed to gather the contents of multiple files within a project directory and consolidate them into a single text file named context.txt. This is particularly useful for providing a large language model (LLM) with a comprehensive overview of a project.
Key Features
- File Consolidation: Combines multiple files into a single
context.txtfile. - Intelligent File Selection: By default, it processes only text files, ignoring binaries.
- Flexible Filtering: Allows for the inclusion or exclusion of files based on their extensions.
- Pattern Exclusion: Enables the exclusion of files whose names contain specific strings.
- Directory Ignoring: Excludes common directories like
node_modules,.git, and others by default. - Token Estimation: Provides an estimate of the token count for the generated context file.
Installation
Ensure you have Node.js installed on your system. You can then run this tool using npx without needing a global installation:
npx gen-context [options]Usage
Run the command in your project's root directory. The tool will recursively scan the current directory, gather the content of relevant files, and create a context.txt file.
Default Behavior
When run without any arguments, gen-context will scan the current directory and all subdirectories. It will include the content of all text files it finds into the context.txt file. Directories such as node_modules, .git, .vscode, dist, and build are automatically ignored.
npx gen-contextOptions
You can customize the behavior of gen-context with the following command-line arguments:
Include File Extensions: To include only files with specific extensions, simply list them.
- Example: To include only
.jsand.htmlfiles:npx gen-context .js .html
- Example: To include only
Exclude File Extensions: To exclude files with specific extensions, prefix the extension with a
-.- Example: To exclude
.cssand.mdfiles:npx gen-context -.css -.md
- Example: To exclude
Exclude Filename Patterns: To exclude files whose names contain a specific pattern (case-insensitive), prefix the pattern with a
-.- Example: To exclude any file containing "test" or "config" in its name:
npx gen-context -test -config
- Example: To exclude any file containing "test" or "config" in its name:
Combining Options
You can combine these options to further refine your file selection.
Include only
.jsand.cssfiles, but exclude any file with "vendor" in its name:npx gen-context .js .css -vendorInclude all text files but exclude
.logfiles and any file containing "temp":npx gen-context -.log -temp
Output
After running, the script will print the following to the console:
- A summary of the filters being applied (included/excluded extensions and patterns).
- A success message confirming the creation of
context.txt. - A list of all the files that were included in the context file.
- An estimated token count of the generated
context.txtfile, which is useful for LLM usage.
The resulting context.txt file will be formatted as follows:
The context is:
# File: path/to/file1.js
// content of file1.js
# File: path/to/file2.html
<!-- content of file2.html -->
...Use Case
The primary use case for this tool is to quickly provide the entire context of a code project to a large language model. Instead of manually copying and pasting each file, you can simply run gen-context and use the content of the resulting context.txt file.
