Skip to content

Commit eb160dc

Browse files
committed
Update README.md
1 parent 2aebe9d commit eb160dc

File tree

1 file changed

+170
-2
lines changed

1 file changed

+170
-2
lines changed

README.md

+170-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ A CLI tool to aggregate your codebase into a single Markdown file for use with C
88
- Ignores common build artifacts and configuration files
99
- Outputs a single Markdown file containing the whole codebase
1010
- Provides options for whitespace removal and custom ignore patterns
11+
- Can be used programmatically as a library in Node.js projects
1112

1213
## How to Use
1314

15+
### CLI Usage
16+
1417
Start by running the CLI tool in your project directory:
1518

1619
```bash
@@ -22,17 +25,181 @@ This will generate a `codebase.md` file with your codebase.
2225
Once you've generated the Markdown file containing your codebase, you can use it with AI models like ChatGPT and Claude for code analysis and assistance.
2326

2427
### With ChatGPT:
28+
2529
1. Create a Custom GPT
2630
2. Upload the generated Markdown file to the GPT's knowledge base
2731

2832
### With Claude:
33+
2934
1. Create a new Project
3035
2. Add the Markdown file to the Project's knowledge
3136

3237
For best results, re-upload the Markdown file before starting a new chat session to ensure the AI has the most up-to-date version of your codebase.
3338

39+
### Library Usage
40+
41+
You can also use ai-digest programmatically in your Node.js applications:
42+
43+
```javascript
44+
// ESM
45+
import aiDigest from "ai-digest";
46+
47+
// CommonJS
48+
const aiDigest = require("ai-digest").default;
49+
50+
// Generate digest and save to file
51+
await aiDigest.generateDigest({
52+
inputDir: "./my-project",
53+
outputFile: "my-digest.md",
54+
removeWhitespaceFlag: true,
55+
});
56+
57+
// Generate digest and get content as string
58+
const content = await aiDigest.generateDigest({
59+
inputDir: "./my-project",
60+
outputFile: null, // Return content as string instead of writing to file
61+
silent: true, // Suppress console output
62+
});
63+
```
64+
65+
For more advanced use cases, you can access the lower-level functions:
66+
67+
```javascript
68+
import { generateDigestContent, writeDigestToFile } from "ai-digest";
69+
70+
// Generate digest content
71+
const { content, stats } = await generateDigestContent({
72+
inputDir: "./my-project",
73+
silent: true,
74+
});
75+
76+
// Do something with the content
77+
console.log(`Generated digest with ${stats.includedCount} files`);
78+
79+
// Write to file later
80+
await writeDigestToFile(content, "output.md", stats, true);
81+
```
82+
83+
## API Reference
84+
85+
### Main Functions
86+
87+
#### `generateDigest(options)`
88+
89+
The main function for programmatic usage. Generates a digest and either returns it as a string or writes it to a file.
90+
91+
**Parameters:**
92+
93+
- `options` (Object, optional): Configuration options
94+
- `inputDir` (string, optional): Input directory path (default: `process.cwd()`)
95+
- `outputFile` (string | null, optional): Output file path or `null` to return as string (default: `"codebase.md"`)
96+
- `useDefaultIgnores` (boolean, optional): Whether to use default ignore patterns (default: `true`)
97+
- `removeWhitespaceFlag` (boolean, optional): Whether to remove whitespace (default: `false`)
98+
- `ignoreFile` (string, optional): Custom ignore file name (default: `".aidigestignore"`)
99+
- `showOutputFiles` (boolean, optional): Whether to display included files (default: `false`)
100+
- `silent` (boolean, optional): Whether to suppress console output (default: `false`)
101+
102+
**Returns:**
103+
104+
- If `outputFile` is `null`: A Promise resolving to the digest content as a string
105+
- Otherwise: A Promise resolving to `void` (content is written to file)
106+
107+
**Example:**
108+
109+
```javascript
110+
// Return as string
111+
const content = await aiDigest.generateDigest({
112+
inputDir: "./src",
113+
outputFile: null,
114+
removeWhitespaceFlag: true,
115+
silent: true,
116+
});
117+
118+
// Write to file
119+
await aiDigest.generateDigest({
120+
inputDir: "./src",
121+
outputFile: "src-digest.md",
122+
});
123+
```
124+
125+
#### `generateDigestContent(options)`
126+
127+
Low-level function to generate the digest content and statistics without writing to a file.
128+
129+
**Parameters:**
130+
131+
- `options` (Object): Configuration options
132+
- `inputDir` (string): Input directory path
133+
- `outputFilePath` (string | null, optional): Output file path (to exclude from digest) or `null`
134+
- `useDefaultIgnores` (boolean, optional): Whether to use default ignore patterns (default: `true`)
135+
- `removeWhitespaceFlag` (boolean, optional): Whether to remove whitespace (default: `false`)
136+
- `ignoreFile` (string, optional): Custom ignore file name (default: `".aidigestignore"`)
137+
- `silent` (boolean, optional): Whether to suppress console output (default: `false`)
138+
139+
**Returns:**
140+
141+
- A Promise resolving to an Object containing:
142+
- `content` (string): The generated digest content
143+
- `stats` (Object): Statistics about the digest
144+
- `totalFiles` (number): Total number of files found
145+
- `includedCount` (number): Number of files included in the digest
146+
- `defaultIgnoredCount` (number): Number of files ignored by default patterns
147+
- `customIgnoredCount` (number): Number of files ignored by custom patterns
148+
- `binaryAndSvgFileCount` (number): Number of binary and SVG files included
149+
- `includedFiles` (string[]): Array of included file paths
150+
- `estimatedTokens` (number): Estimated token count for AI models
151+
- `fileSizeInBytes` (number): Size of the digest content in bytes
152+
153+
**Example:**
154+
155+
```javascript
156+
const { content, stats } = await aiDigest.generateDigestContent({
157+
inputDir: "./project",
158+
silent: true,
159+
});
160+
161+
console.log(`Generated digest contains ${stats.includedCount} files`);
162+
console.log(`Estimated token count: ${stats.estimatedTokens}`);
163+
```
164+
165+
#### `writeDigestToFile(content, outputFile, stats, showOutputFiles)`
166+
167+
Writes the generated digest content to a file and displays statistics.
168+
169+
**Parameters:**
170+
171+
- `content` (string): The digest content to write
172+
- `outputFile` (string): Path to write the output file
173+
- `stats` (Object): Statistics object from `generateDigestContent`
174+
- `showOutputFiles` (boolean, optional): Whether to display the list of included files (default: `false`)
175+
176+
**Returns:**
177+
178+
- A Promise resolving to `void`
179+
180+
**Example:**
181+
182+
```javascript
183+
const { content, stats } = await aiDigest.generateDigestContent({
184+
inputDir: "./project",
185+
});
186+
187+
// Process or modify the content if needed
188+
const processedContent = content.replace(/some pattern/g, "replacement");
189+
190+
// Write to file
191+
await aiDigest.writeDigestToFile(
192+
processedContent,
193+
"modified-digest.md",
194+
stats,
195+
true
196+
);
197+
```
198+
34199
## Options
35200

201+
### CLI Options
202+
36203
- `-i, --input <directory>`: Specify input directory (default: current directory)
37204
- `-o, --output <file>`: Specify output file (default: codebase.md)
38205
- `--no-default-ignores`: Disable default ignore patterns
@@ -43,6 +210,8 @@ For best results, re-upload the Markdown file before starting a new chat session
43210

44211
## Examples
45212

213+
### CLI Examples
214+
46215
1. Basic usage:
47216

48217
```bash
@@ -79,7 +248,6 @@ ai-digest supports custom ignore patterns using a `.aidigestignore` file in the
79248

80249
Use the `--show-output-files` flag to see which files are being included, making it easier to identify candidates for exclusion.
81250

82-
83251
## Whitespace Removal
84252

85253
When using the `--whitespace-removal` flag, ai-digest removes excess whitespace from files to reduce the token count when used with AI models. This feature is disabled for whitespace-dependent languages like Python and YAML.
@@ -108,4 +276,4 @@ Contributions are welcome! Please feel free to submit a Pull Request.
108276

109277
## License
110278

111-
This project is licensed under the MIT License.
279+
This project is licensed under the MIT License.

0 commit comments

Comments
 (0)