This PR implements a complete static analysis tool for Nexus IoC dependency injection graphs. The @nexus-ioc/graph-analyzer package analyzes TypeScript code without execution, extracting complete dependency metadata and generating both JSON and visual (PNG) outputs.
Key Achievements:
- ✅ Phase 1: Complete dependency injection metadata extraction
- ✅ Phase 2: JSON output format and CLI interface
- ✅ Documentation: Comprehensive docs (README, Architecture, CLI Guide, API Reference)
- ✅ Publishing Ready: Fully configured for npm with optimized package size
Related Issue: Closes #25
-
🔍 Dependency Extraction
- Constructor dependencies from
@Injectdecorators - Property dependencies from
@Injectdecorators - Optional dependency detection via
@Optional - Support for class, string, and symbol tokens
- TC39 Stage 3 decorator support (static dependencies arrays)
- Automatic import resolution and class file loading
- Constructor dependencies from
-
📊 JSON Output Format
- Structured graph output with modules, providers, and metadata
- Complete dependency information for each provider
- Module relationships (imports, exports, providers)
- Analysis metadata (timestamp, version, statistics)
-
🖥️ Command-Line Interface
- Easy-to-use CLI with intuitive options
- Multiple output formats (JSON, PNG, or both)
- Automatic tsconfig.json discovery
- Helpful error messages and usage documentation
-
📚 Comprehensive Documentation
- Complete README with quick start guide
- Architecture documentation explaining internal design
- CLI usage guide with all options and examples
- API reference for programmatic usage
- JSDoc comments on all exported APIs
-
📦 Publishing Ready
- Scoped package name:
@nexus-ioc/graph-analyzer - Optimized package size (43.5 kB, 69 files)
- Complete TypeScript declarations
- Proper npm configuration
- Scoped package name:
- Source Files: 21 TypeScript files
- Lines of Code: 2,386 lines (excluding tests)
- Test Files: 3 test suites
- Test Lines: 1,095 lines
- Documentation: 2,879 lines across 5 markdown files
File | % Stmts | % Branch | % Funcs | % Lines
--------------------------|---------|----------|---------|--------
All files | 82.49 | 81.55 | 84.61 | 82.49
parser | 91.56 | 86.36 | 91.66 | 91.56
dependency-extractor.ts | 91.56 | 86.36 | 91.66 | 91.56
parser/provider | 46.92 | 85.71 | 66.66 | 46.92
class-parser.ts | 46.92 | 85.71 | 66.66 | 46.92
visualize | 96.13 | 65.21 | 100 | 96.13
json-formatter.ts | 96.13 | 65.21 | 100 | 96.13
--------------------------|---------|----------|---------|--------
Test Suites: 3 passed, 3 total
Tests: 35 passed, 35 total
- Package size: 43.5 kB (23% reduction from initial)
- Unpacked size: 189.5 kB (31% reduction)
- Total files: 69 files (excludes tests and dev files)
# Install locally in project
npm install @nexus-ioc/graph-analyzer --save-dev
# Or install globally
npm install -g @nexus-ioc/graph-analyzer# Analyze and generate both JSON and PNG
graph-analyzer src/main.ts
# Generate only JSON output
graph-analyzer -f json -o output.json src/main.ts
# Generate only PNG visualization
graph-analyzer -f png -o graph.png src/main.ts
# Specify custom tsconfig.json
graph-analyzer -c ./tsconfig.app.json src/main.ts$ graph-analyzer -f json -o graph.json src/entry.ts
Analyzing: /path/to/src/entry.ts
Using tsconfig: /path/to/tsconfig.json
JSON output written to: graph.json
Analysis complete!Generated JSON structure:
{
"modules": [
{
"name": "AppsModule",
"path": "/path/to/apps/index.ts",
"imports": ["UserModule", "PostsModule"],
"exports": [],
"providers": [],
"isGlobal": false
}
],
"providers": [
{
"token": "HttpService",
"type": "Class",
"module": "HttpModule",
"scope": "Singleton",
"dependencies": [
{
"type": "constructor",
"index": 0,
"token": "URL",
"tokenType": "string",
"optional": false
}
]
}
],
"metadata": {
"entryPoint": "/path/to/src/entry.ts",
"rootModule": "AppsModule",
"analyzedAt": "2025-10-18T10:12:44.507Z",
"version": "1.0.0",
"totalModules": 6,
"totalProviders": 8
}
}import { GraphAnalyzer, ParseEntryFile, ParseTsConfig } from '@nexus-ioc/graph-analyzer';
// Parse entry file
const tsConfig = new ParseTsConfig(configContent, basePath);
const entryFile = new ParseEntryFile(sourceFile, 'src/main.ts', tsConfig);
entryFile.parse();
// Build graph and analyze
const graph = new Map();
graph.set('entry', entryFile);
const analyzer = new GraphAnalyzer(graph, 'src/main.ts', {
outputFormat: 'json'
});
const output = analyzer.parse();
console.log(`Found ${output.metadata.totalProviders} providers`);Commit: d03ce9de
- Created
DependencyExtractorclass with comprehensive AST parsing - Enhanced
ClassParserto load and parse class files from imports - Enhanced
UseClassParserto extract dependencies from referenced classes - Added support for both legacy decorators and TC39 Stage 3 patterns
- Tests: 28 unit tests with 77.5% coverage
Commit: 2e9a23fe
- Created structured
GraphOutput,ModuleInfo,ProviderInfointerfaces - Implemented
JsonFormatterclass for JSON serialization - Built complete CLI with argument parsing and help documentation
- Added support for multiple output formats (json, png, both)
- Tests: 7 unit tests for JsonFormatter (94.2% coverage)
- Total: 35 tests passing with 80.79% overall coverage
Commit: 47062fa
- README.md (317 lines): Installation, quick start, CLI reference
- docs/ARCHITECTURE.md (486 lines): Technical architecture and design
- docs/CLI_GUIDE.md (576 lines): Complete CLI usage guide
- docs/API_REFERENCE.md (747 lines): Programmatic API documentation
- Added JSDoc comments to all exported classes and interfaces
Commit: bfc0b04
- Updated package name to
@nexus-ioc/graph-analyzer - Added keywords for npm discoverability
- Created
.npmignoreto exclude dev files - Optimized package size (23% reduction)
- Added
prepublishOnlyscript - Tested with
npm packandnpm link
GraphAnalyzer Constructor
- Before:
new GraphAnalyzer(graph, options) - After:
new GraphAnalyzer(graph, entryPoint, options) - Reason: Entry point is required for metadata generation
- Migration: Add entry point as second parameter
All tests pass successfully:
$ npm test
PASS src/visualize/__tests__/json-formatter.spec.ts
PASS src/parser/__tests__/dependency-extractor.spec.ts
PASS src/parser/provider/__tests__/class-parser.spec.ts
Test Suites: 3 passed, 3 total
Tests: 35 passed, 35 totalManual Testing:
- ✅ CLI tested with
npm linkand example project - ✅ JSON output validated with real Nexus IoC application
- ✅ PNG generation tested with Graphviz
- ✅ TypeScript declarations verified
- ✅ Package contents verified with
npm pack --dry-run
Complete documentation is available:
- README.md: Quick start and overview
- ARCHITECTURE.md: Technical design
- CLI_GUIDE.md: CLI usage
- API_REFERENCE.md: Programmatic API
The following features could be added in future PRs:
-
Enhanced Error Handling (P1)
- Circular dependency detection
- Validation warnings for missing providers
- Better error messages with suggestions
-
Additional Output Formats (P2)
- Interactive HTML visualization
- Mermaid diagram format
- DOT format for custom Graphviz styling
-
Analysis Features (P2)
- Unused provider detection
- Dependency graph metrics
- Performance analysis
- All tests passing (35/35)
- Code coverage >80% (82.49%)
- Documentation complete
- CLI tested manually
- Package size optimized
- TypeScript declarations generated
- Breaking changes documented
- Conventional commits used
- No linter errors
This PR includes 6 commits:
eede331- Initial static analyzer setup3563bb7- Enhanced parser implementationd03ce9d- Phase 1: Dependency extraction (28 tests)2e9a23f- Phase 2: JSON output & CLI (7 tests)47062fa- Phase 3: Comprehensive documentationbfc0b04- Phase 4: Publishing preparation
Ready for review and merge! 🚀