0.2.0 • Published 5 months ago
ai-code-documenter v0.2.0
AI Code Documenter
AI-powered documentation generator for your code that integrates with Git hooks and lint-staged.
Features
- 🤖 Uses AI to automatically generate and update code documentation
- 🔄 Seamlessly integrates with Git hooks via lint-staged
- 🧰 Support for JSDoc, TSDoc, docstrings, and Markdown formats
- ⚡ Command line interface for individual files or batch processing
- 🛠️ Programmable API for custom integrations
- 📝 Preserves your code while enhancing documentation
- 🚀 Parallel processing with configurable concurrency limits
Installation
npm install --save-dev ai-code-documenter
# or
yarn add --dev ai-code-documenter
Setup with lint-staged
- Install the package
- Update your lint-staged configuration in
package.json
:
{
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"ai-document",
"prettier --write",
"eslint --fix"
]
}
}
For improved performance with multiple files, you can set a concurrency limit:
{
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"ai-document --concurrency 3",
"prettier --write",
"eslint --fix"
]
}
}
Command Line Usage
Document individual files:
npx ai-document src/components/Button.tsx
Document multiple files:
npx ai-document src/**/*.ts --verbose
CLI options:
--verbose Show detailed output
--tsdoc Use TSDoc style (default is JSDoc)
--docstring Use docstring style (for Python-like docs)
--markdown Use Markdown style
--config <path> Specify config file location
--concurrency <n> Set maximum number of files to process in parallel (default: 5)
--no-colors Disable colored output
API Usage
import { documentFile, documentFiles } from 'ai-code-documenter';
// Document a single file
const result = await documentFile('src/utils/helper.ts', {
outputStyle: 'tsdoc',
verbose: true
});
// Document multiple files with concurrency limit
const results = await documentFiles([
'src/components/Button.tsx',
'src/utils/format.ts'
], {
model: 'gpt-4',
verbose: true,
maxConcurrency: 3 // Process up to 3 files at once
});
Configuration
Create a configuration file in one of the following formats:
.ai-documenter.json
.ai-documenter.js
.ai-documenter.ts
ai-documenter.config.js
ai-documenter.config.ts
Example JSON configuration:
{
"apiKey": "your-openai-api-key", // Optional: can also use OPENAI_API_KEY env variable
"model": "gpt-3.5-turbo", // Default model to use
"outputStyle": "jsdoc", // jsdoc, tsdoc, docstring, or markdown
"verbose": false, // Whether to show detailed logs
"maxConcurrency": 5 // Maximum number of files to process concurrently
}
Example JavaScript/TypeScript configuration:
module.exports = {
apiKey: process.env.MY_OPENAI_KEY,
model: 'gpt-4',
maxConcurrency: 3, // Process up to 3 files simultaneously
// Other options...
};
Environment Variables
The tool supports loading environment variables from various .env files:
.env
- Default environment variables.env.local
- Local overrides (not committed to git).env.development
- Development environment variables.env.development.local
- Local overrides for development.env.production
- Production environment variables.env.production.local
- Local overrides for production.env.test
- Test environment variables.env.test.local
- Local overrides for test
Variables are loaded with the following precedence (highest to lowest):
1. .env.[environment].local
2. .env.local
3. .env.[environment]
4. .env
Required environment variables:
OPENAI_API_KEY
: Your OpenAI API key (unless specified in configuration)
Supported Languages
- JavaScript (.js)
- TypeScript (.ts)
- React (.jsx, .tsx)
- Support for additional languages coming soon!
How It Works
- The tool extracts code from your files
- It sends the code to an AI model with specific documentation instructions
- The AI analyzes the code and generates appropriate documentation
- The tool updates your files with the enhanced documentation
- No code logic is changed, only comments are added or improved
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request