replace-hardcoded-domain
v1.0.0
Published
Angular migration tool to replace hardcoded localStorage domain lookups with SiteService injection. Compatible across Angular 8 to 19+ with zero runtime dependencies.
Maintainers
Readme
replace-hardcoded-domain
An automated, zero-dependency Angular migration codemod to replace hardcoded
localStorage.getItem('domain')calls withSiteServicedependency injection.
Supports all Angular versions from Angular 8 to Angular 19+ with zero external dependencies (ts-morph has been completely eliminated).
🚀 Quick Start (No Installation Needed)
Any developer can run this tool in their Angular project directory with zero installation:
npx replace-hardcoded-domainOr preview changes safely without writing to disk:
npx replace-hardcoded-domain --dry-run✨ Features
- ⚡ Zero Dependencies: No
ts-morph, no bloatednode_modules, package size is only ~7 KB. - 🔄 Universal Compatibility: Works seamlessly with Angular 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19+.
- 🔍 Smart SiteService Detection:
- Automatically checks if
SiteServiceis located atsrc/app/global/services/site/site.service. - If not found, interactively prompts you in the terminal to paste the path to your
SiteService. - You can also bypass the prompt via the
--serviceflag.
- Automatically checks if
- 🛡️ Preserves Existing Injections: If
SiteServiceis already injected in a constructor under any alias (e.g.,private siteSvc: SiteService), it reuses that alias and does not add duplicate parameters or imports. - 📐 Clean Formatting & Relative Imports: Automatically computes correct relative imports (e.g.
../../global/services/site/site.service), preserving formatting, single/double quotes, and trailing commas. - 🧪 Safe & Dry-Run Capable: Supports
--dry-runto simulate and preview modifications before touching any files.
🛠️ How It Works (Before & After)
Before:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-header',
templateUrl: './header.component.html'
})
export class HeaderComponent implements OnInit {
domain: string;
constructor(
private router: Router,
private http: HttpClient
) {}
ngOnInit() {
this.domain = localStorage.getItem('domain');
}
}After:
import { Component, OnInit } from '@angular/core';
import { SiteService } from '../../global/services/site/site.service';
@Component({
selector: 'app-header',
templateUrl: './header.component.html'
})
export class HeaderComponent implements OnInit {
domain: string;
constructor(
private router: Router,
private http: HttpClient,
private siteService: SiteService
) {}
ngOnInit() {
this.domain = this.siteService.getDomain();
}
}💻 CLI Usage & Options
npx replace-hardcoded-domain [options]| Option | Shorthand | Description | Default |
|---|---|---|---|
| --service <path> | -s | Path to SiteService file (bypasses prompt if valid) | src/app/global/services/site/site.service |
| --dir <path> | -d | Directory to scan for .ts files | src |
| --dry-run | | Simulate and preview changes without modifying files | false |
| --verbose | -v | Show detailed skipped files breakdown | false |
| --help | -h | Display help menu | |
| --version | | Display package version | |
Examples:
# Interactive run (default)
npx replace-hardcoded-domain
# Specify a custom SiteService location directly
npx replace-hardcoded-domain --service src/app/core/services/site.service.ts
# Test run without touching disk
npx replace-hardcoded-domain --dry-run
# Run only on a specific feature directory
npx replace-hardcoded-domain --dir src/app/features/dashboard📦 Programmatic Usage (Node.js API)
You can also use this library programmatically in your own scripts:
const { runMigration, transformTsContent } = require('replace-hardcoded-domain');
// Run the full migration programmatically
async function main() {
const result = await runMigration({
scanDir: 'src',
servicePath: 'src/app/global/services/site/site.service.ts',
dryRun: false
});
console.log('Modified files:', result.modifiedFiles);
}
main();🧪 Testing
Run the built-in test suite:
npm test📄 License
MIT
