NgLens
August 21, 2025 Β· View on GitHub
This Node.js utility uses ts-morph to statically analyze Angular TypeScript components and identify service usage patterns from any specified API library. It detects constructor dependency injection and tracks which service methods are actually called within component code.
π Features
- Service Injection Detection: Identifies services from your API library injected via Angular constructor dependency injection
- Method Usage Tracking: Analyzes component methods to track which service methods are actually called
- Routing Analysis: Scans Angular routing modules to extract
component,fullPath,data.menuId, andimportPath - Scalable Architecture: Class-based design ready for analyzing 1000+ files
- JSON Output: Clean, structured output perfect for integration with other tools
- Configurable: Easy to change target modules or source file patterns
π¦ Setup Instructions
- Clone or download the project.
- Run the following commands from the project root:
npm install
This installs all required dependencies, including:
ts-morphβ for TypeScript AST analysistypescript,ts-node, and@types/nodeβ for clean TypeScript execution
βΆοΈ Usage
To run the component/service analysis script:
npm start
This executes ts-node src/index.ts and analyzes all .ts files in the sample/ directory by default.
Route scan
To scan routing modules (e.g., sample/crm-routing.module.ts) and list components that include a data.menuId with their full paths and import locations:
npm run scan:routes
Example output (truncated):
[
{
"file": "D:/ng-lens/sample/crm-routing.module.ts",
"component": "ConstituentSearchComponent",
"importPath": "./constituent-search/constituent-search.component",
"fullPath": "search",
"menuId": "MenuEntries.ConstituentSearch"
},
{
"file": "D:/ng-lens/sample/crm-routing.module.ts",
"component": "AddressesComponent",
"importPath": "./constituent-detail/addresses/addresses.component",
"fullPath": "constituents/:constituentId/addresses",
"menuId": "MenuEntries.Addresses"
}
]
Configuration
You can customize the analyzer by modifying src/index.ts:
const analyzer = new AngularAnalyzer({
targetModule: "your-api-module", // Specify your API library name
sourcePattern: "src/**/*.ts" // Default: "sample/**/*.ts"
});
π Example Output
{
"UserProfileComponent": {
"file": "C:/Repo/ng-lens/sample/user-profile.component.ts",
"services": {
"UserService": [
"GetProfile",
"UpdateProfile",
"Delete",
"RefreshCache",
"ExportData"
],
"ProductService": [
"GetByUser",
"RefreshCache"
],
"OrderService": [
"GetRecent"
],
"NotificationService": [
"ShowError",
"ShowSuccess",
"ShowInfo"
]
}
},
"ProductEditComponent": {
"file": "C:/Repo/ng-lens/sample/product-edit.component.ts",
"services": {
"ProductService": [
"GetById",
"Create",
"Update",
"Delete",
"Validate"
],
"CategoryService": [
"GetAll",
"RefreshCache"
],
"InventoryService": [
"GetByProduct",
"Update",
"Remove"
],
"ReviewService": [
"GetByProduct",
"DeleteByProduct"
],
"PricingService": [
"GetDefaults",
"GetByProduct",
"Update"
],
"ShippingService": [
"Calculate"
]
}
}
}
ποΈ Architecture
NgLens uses a clean, class-based architecture following Single Responsibility Principle:
AngularAnalyzer: Main orchestrator coordinating all analysisImportAnalyzer: Detects imports from target modules and constructor injectionServiceUsageAnalyzer: Analyzes method calls on injected servicesRoutingAnalyzer: Parses Angular route definitions to collectcomponent,fullPath,data.menuId, andimportPathReportGenerator: Formats and outputs analysis results
π Documentation
See CHANGELOG.md for detailed development history, technical decisions, and architecture explanations.
π― Use Cases
- API Usage Analysis: Track which parts of your API are actually being used
- Refactoring Planning: Identify unused service methods before API changes
- Dependency Mapping: Understand component-to-service relationships
- Code Review: Generate usage reports for large codebases
- Navigation Mapping: Inventory routed components with their menuIds and paths
π Project Structure
src/
βββ AngularAnalyzer.ts # Main orchestrator
βββ index.ts # Entry point
βββ route-scan.ts # CLI to scan routes in sample/*
βββ test-runner.ts # Basic test harness
βββ analyzers/
βββ ImportAnalyzer.ts # Import & injection detection
βββ ServiceUsageAnalyzer.ts # Method call analysis
βββ RoutingAnalyzer.ts # Route analysis (menuId, full path, import)
βββ ReportGenerator.ts # Output formatting
sample/ # Test files
βββ user-profile.component.ts # Component with multiple services
βββ address-edit.component.ts # Component with CRUD-like operations
βββ crm.component.ts # Component with API interactions
βββ crm-routing.module.ts # Rich routing module for route scanning
π§ Development
- Run all tests:
npm test
- Run validation tests only:
npm run test:validation
- Scan routes:
npm run scan:routes
All current tests validate both the component/service analysis and routing analysis.
π οΈ Troubleshooting
- On Windows, you may see executable shim files appear in the project root after install (e.g.,
acorn,ts-node.ps1,tsc.cmd). They are duplicates of the ones innode_modules/.binand are safe to remove. Ensure your.gitignoreexcludes them (theyβre not needed for the repo). If they show up, delete the root-level shims and keep the copies undernode_modules/.bin.
For development history and technical details, see CHANGELOG.md.