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.
- 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
- 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
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.
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"
}
]
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"
});
{
"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"
]
}
}
}
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
, andimportPath
ReportGenerator
: Formats and outputs analysis results
See CHANGELOG.md for detailed development history, technical decisions, and architecture explanations.
- 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
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
- 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.
- 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/.bin
and are safe to remove. Ensure your.gitignore
excludes 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.