common/autoinstallers/rush-commands/src/convert-comments/README.md
本项目是一个TypeScript命令行工具,用于自动将代码仓库中的中文注释翻译为英文。通过调用OpenAI API,实现高质量的代码注释翻译,同时保持原有的代码格式和结构。
npm install
# 翻译指定目录下的所有支持文件
ai-translate --root ./src
# 指定文件扩展名
ai-translate --root ./src --exts ts,js,go
# 仅分析不修改(预览模式)
ai-translate --root ./src --dry-run
# 详细输出模式
ai-translate --root ./src --verbose
# 通过环境变量设置
export OPENAI_API_KEY=your-api-key
# 或通过命令行参数
ai-translate --root ./src --openai-key your-api-key
src/convert-comments/
├── 📄 requirements.md # 需求文档
├── 📄 implementation-plan.md # 实现方案
├── 📄 technical-specification.md # 技术规格
├── 📄 README.md # 项目概览(本文件)
├── 📦 index.ts # 主入口文件
├── 🗂️ cli/ # 命令行接口
│ ├── command.ts # Commander.js命令定义
│ └── config.ts # 配置管理
├── 🗂️ modules/ # 核心功能模块
│ ├── file-scan.ts # 文件扫描模块
│ ├── chinese-detection.ts # 中文检测模块
│ ├── translation.ts # 翻译服务模块
│ ├── file-replacement.ts # 文件替换模块
│ └── report.ts # 报告生成模块
├── 🗂️ utils/ # 工具函数
│ ├── git.ts # Git操作工具
│ ├── language.ts # 编程语言识别
│ ├── chinese.ts # 中文字符检测
│ └── fp.ts # 函数式编程工具
├── 🗂️ types/ # TypeScript类型定义
│ ├── index.ts # 主要类型定义
│ └── config.ts # 配置类型
└── 🗂️ __tests__/ # 测试文件
├── unit/ # 单元测试
└── integration/ # 集成测试
采用纯函数设计和不可变数据结构:
const processRepository = pipe(
getGitTrackedFiles,
asyncMap(readFile),
asyncFilter(hasChineseComments),
asyncMap(extractChineseComments),
asyncMap(translateComments),
asyncMap(applyTranslations),
generateReport
);
npm install
npm run dev
npm test
npm run build
git checkout -b feature/new-featuregit commit -am 'Add new feature'git push origin feature/new-feature--root, -r <directory>:需要处理的根目录--exts, -e <extensions>:文件扩展名,如 "ts,js,go,md"--openai-key <key>:OpenAI API密钥--model <model>:OpenAI模型名称(默认:gpt-3.5-turbo)--dry-run:仅分析不实际修改文件--backup:创建文件备份(默认启用)--verbose, -v:详细输出模式--output <file>:报告输出文件路径# 基本使用
ai-translate --root ./src --exts ts,js
# 预览模式(不修改文件)
ai-translate --root ./src --dry-run --verbose
# 使用GPT-4模型
ai-translate --root ./src --model gpt-4
# 生成JSON格式报告
ai-translate --root ./src --output report.json
支持使用配置文件来管理默认设置:
{
"translation": {
"model": "gpt-3.5-turbo",
"maxRetries": 3,
"timeout": 30000,
"concurrency": 3
},
"processing": {
"defaultExtensions": ["ts", "js", "go", "md"],
"createBackup": true,
"outputFormat": "console"
},
"git": {
"ignorePatterns": ["node_modules/**", ".git/**", "dist/**"],
"includeUntracked": false
}
}
处理完成后会生成详细的统计报告:
📊 翻译处理报告
==================
总文件数: 45
处理成功: 42
跳过文件: 3
翻译注释: 128
错误数量: 0
处理时间: 45.32秒
✅ 处理完成,无错误
如有问题或建议,请通过以下方式联系:
Happy Coding! 🎉