code-relation-analyzer
Version:
A TypeScript project dependency analysis tool that generates function-level dependency graphs
391 lines (292 loc) • 9.48 kB
Markdown
18.0.0
- TypeScript >= 5.0.0(可选,用于 TypeScript 项目分析)
```bash
code-relation analyze
code-relation analyze ./src
code-relation analyze --output ./analysis-report
```
```bash
code-relation analyze --include-node-modules
code-relation analyze --max-depth 5
code-relation analyze --patterns "**/*.ts,**/*.tsx,**/*.vue"
code-relation analyze --exclude "**/*.test.ts,**/*.spec.ts"
```
```bash
git clone https://github.com/your-org/code-relation-analyzer.git
cd code-relation-analyzer
npm install
npm run build
npm run analyze examples/vue-project/src
node generate-html-report.js
```
工具会生成一个交互式 HTML 报告,包含多个视图:
- **📈 项目概览**: 项目统计信息和关键指标
- **📁 文件依赖**: Import/Export 关系网络图
- **🔗 继承关系**: 类和接口的继承层次结构
- **⚡ 函数调用**: 函数间的调用依赖图
- **📊 参数传递**: 组件间的数据流可视化
- **🌐 全局依赖**: 全局变量和工具类的使用分布
- **交互式图表**: 支持缩放、拖拽、点击查看详情
- **多层次展示**: 从文件级到函数级的多层次依赖关系
- **智能布局**: 自动优化的图形布局算法
- **实时筛选**: 根据依赖类型、复杂度等条件筛选显示
```typescript
import { analyzeProject } from 'code-relation-analyzer';
const result = await analyzeProject({
projectPath: './my-project',
outputPath: './analysis',
includeNodeModules: false,
maxDepth: 10,
filePatterns: ['**/*.ts', '**/*.tsx', '**/*.vue']
});
console.log('项目信息:', result.projectInfo);
console.log('文件依赖:', result.fileDependencies);
console.log('函数调用:', result.functionCalls);
```
```typescript
import {
ProjectAnalyzer,
DependencyAnalyzer,
VisualizationGenerator
} from 'code-relation-analyzer';
// 创建分析器实例
const analyzer = new ProjectAnalyzer({
includeNodeModules: false,
maxDepth: 15,
cacheEnabled: true
});
// 执行分析
const analysisResult = await analyzer.analyze('./src');
// 生成可视化
const visualizer = new VisualizationGenerator();
const htmlReport = await visualizer.generateReport(analysisResult);
// 保存报告
await visualizer.saveReport(htmlReport, './output/report.html');
```
在项目根目录创建 `code-relation.config.js` 文件:
```javascript
module.exports = {
// 基础配置
includeNodeModules: false,
maxDepth: 10,
outputPath: './analysis',
// 文件匹配模式
filePatterns: [
'**/*.ts',
'**/*.tsx',
'**/*.js',
'**/*.jsx',
'**/*.vue'
],
// 排除模式
excludePatterns: [
'**/*.test.ts',
'**/*.spec.ts',
'**/node_modules/**',
'**/dist/**',
'**/*.d.ts'
],
// 分析选项
analysis: {
// 启用的分析类型
enabledAnalyzers: [
'dependency',
'inheritance',
'functionCall',
'parameterFlow',
'globalDependency'
],
// 复杂度计算
complexity: {
maxCyclomaticComplexity: 10,
maxFunctionLength: 50,
maxParameterCount: 5
}
},
// 可视化配置
visualization: {
theme: 'light', // 'light' | 'dark'
layout: 'force', // 'force' | 'hierarchical' | 'circular'
nodeSize: 'auto', // 'auto' | 'fixed'
showLabels: true,
enableInteraction: true
},
// 输出配置
output: {
format: ['html', 'json'], // 输出格式
includeSourceMaps: true,
minify: false
}
};
```
```bash
export CODE_RELATION_LOG_LEVEL=debug
export CODE_RELATION_CACHE_DIR=./cache
export CODE_RELATION_MAX_MEMORY=4096
```
```bash
git clone https://github.com/your-org/code-relation-analyzer.git
cd code-relation-analyzer
npm install
npm run setup
npm run dev
```
```bash
npm run build
npm test
npm run test:coverage
npm run lint
npm run lint:fix
npm run check
```
```
code-relation-analyzer/
├── src/
│ ├── cli/
│ ├── parser/
│ ├── analyzer/
│ ├── visualizer/
│ ├── types/
│ └── utils/
├── examples/
├── templates/
├── tests/
└── docs/
```
我们欢迎所有形式的贡献!请遵循以下步骤:
1. **Fork 仓库**
```bash
git clone https://github.com/your-username/code-relation-analyzer.git
```
2. **创建功能分支**
```bash
git checkout -b feature/amazing-feature
```
3. **开发和测试**
```bash
npm run dev
npm test
npm run lint
```
4. **提交更改**
```bash
git add .
git commit -m 'feat: add amazing feature'
```
5. **推送分支**
```bash
git push origin feature/amazing-feature
```
6. **创建 Pull Request**
- 在 GitHub 上创建 PR
- 详细描述更改内容
- 等待代码审查
- **代码风格**: 使用 ESLint 和 Prettier
- **提交信息**: 遵循 [Conventional Commits](https://conventionalcommits.org/) 规范
- **测试覆盖率**: 新功能需要包含测试,覆盖率不低于 80%
- **文档更新**: 重要功能需要更新相应文档
如果发现 bug 或有功能建议,请:
1. 检查是否已有相关 issue
2. 使用 issue 模板创建新问题
3. 提供详细的复现步骤和环境信息
本项目基于 [MIT 许可证](LICENSE) 开源。
```
MIT License
Copyright (c) 2024 Code Relation Analyzer Team
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
## 📞 支持与社区
- 📖 **文档**: [完整文档](https://github.com/your-org/code-relation-analyzer/wiki)
- 🐛 **问题反馈**: [Issue 跟踪器](https://github.com/your-org/code-relation-analyzer/issues)
- 💬 **讨论交流**: [GitHub Discussions](https://github.com/your-org/code-relation-analyzer/discussions)
- 📧 **邮件支持**: support@code-relation-analyzer.com
- 🌟 **更新日志**: [CHANGELOG.md](CHANGELOG.md)
---
**如果这个工具对您有帮助,请给我们一个 ⭐ Star!**
一个强大的 TypeScript 项目依赖关系分析工具,能够生成交互式的函数级依赖关系图。该工具专为前端开发者、架构师和项目经理设计,通过 AST 解析技术深度分析源码,识别文件间引用、继承关系、函数调用等复杂依赖关系,并生成可视化的 HTML 报告。
- 📊 **全面分析**: 分析文件依赖、继承关系、函数调用和参数传递流
- 🎯 **AST 解析**: 基于 TypeScript 编译器 API 进行精确的源码分析
- 🌐 **交互式可视化**: 生成基于 D3.js 的美观依赖关系图
- 🖥️ **命令行界面**: 简单易用的 CLI 工具
- 📱 **响应式设计**: 支持桌面和移动设备
- 🔍 **详细报告**: 导出全面的分析报告
- 🎨 **Vue 支持**: 支持 Vue 单文件组件解析(基于 @vue/compiler-sfc)
- ⚡ **高性能**: 快速分析大型项目,支持缓存机制
- 🔧 **可配置**: 灵活的配置选项,满足不同项目需求
```bash
npm install -g code-relation-analyzer
```
```bash
npm install --save-dev code-relation-analyzer
```
- Node.js >=