1.0.5 ⢠Published 8 months ago
replace-imports-cli v1.0.5
Replace Imports CLI
A lightweight CLI tool to refactor and optimize ES6 imports by replacing long relative paths with aliases defined in tsconfig.json
file. It supports configurable file extensions, processes only the specified files, and allows for flexibility with simple-to-use prompts.
Features
- Replace Long Relative Imports: Automatically replace long relative import paths (e.g.,
../../../services/auth.service
) with aliases (e.g.,@services/auth.service
). - Custom File Extensions: Specify the file extensions you want to process (default:
.js, .ts
). - Flexible Import Handling:
- Skip imports already using aliases.
- Replace imports only outside alias folders.
- Interactive CLI: Simple prompts to configure paths, extensions, and processing behavior.
- Framework-Agnostic: Works with any TypeScript project that uses
tsconfig.json
.
Installation
Prerequisites
- Node.js
- A project with a valid tsconfig.json file containing paths mappings.
Clone and Setup
git clone git@github.com:jzolnowski/replace-imports-cli.git
cd replace-imports-cli
npm install
Install and Use the Package
After publishing, users can install your CLI tool globally or locally:
Global Installation:
npm install -g replace-imports-cli
Run the tool:
replace-imports
Local Installation:
npm install --save-dev replace-imports-cli
Use it in a project:
npx replace-imports
Example
Developer Workflow:
- Initialize a project with aliases in
tsconfig.json
e.g.{ "compilerOptions": { "baseUrl": "./src", "paths": { "@services/*": ["app/services/*"], "@components/*": ["app/components/*"] } } }
- Install
replace-imports-cli
package and run it - check the details inInstall and Use the Package
section.
CLI Output:
Welcome to Replace Imports CLI! š
? Enter the path to your tsconfig.json: ./tsconfig.json
? Enter the path to your project source directory (e.g., ./src): ./src
? Enter file extensions to process (comma-separated, e.g., .ts,.js): .ts,.js
Processing files in ./src using aliases from ./tsconfig.json with extensions: .ts, .js...
Updated imports in src/app/services/auth.service.ts
Updated imports in src/app/components/header.component.js
All imports updated successfully! š
Before Running the Script
In src/app/modules/user.module.ts
:
import { AuthService } from '../../../services/auth.service';
import { UserComponent } from '../../components/user.component';
After Running the Script
import { AuthService } from '@services/auth.service';
import { UserComponent } from '@components/user.component';
How It Works
- Reads
tsconfig.json
: Parses thepaths
section to extract aliases. - Scans the Source Directory: Recursively searches for files matching the specified extensions.
- Replaces Imports:
- Checks each
import
statement to determine if it matches the alias mappings. - Replaces relative paths with the appropriate alias if applicable.
- Writes Changes: Updates the files with optimized imports.
Configuration
Default Values
The following values are used by default if not explicitly specified during the CLI prompts:
Setting | Default Value |
---|---|
tsconfig.json Path | ./tsconfig.json |
Source Directory | ./src |
File Extensions | .ts, .js |
Folder Structure
replace-imports-cli/
āāā src/
ā āāā constants.js # Defines reusable constants, like default extensions and error messages.
ā āāā file-utils.js # Contains utility functions to scan directories and retrieve files based on extensions.
ā āāā replace-imports.js # Handles the logic for replacing relative imports with tsconfig aliases.
ā āāā tsconfig-utils.js # Parses tsconfig.json and extracts alias paths for use in replacements.
ā āāā cli.js # Contains the interactive CLI logic and user prompts.
ā āāā index.js # Orchestrates the replacement process by tying together other modules.
āāā bin/
ā āāā replace-imports.js # Entry point for the CLI; calls `runCLI` from `src/cli.js`.
āāā package.json # Defines the npm package configuration, dependencies, and scripts.
āāā README.md # Documentation for the project, including usage and examples.
āāā .editorconfig # Ensures consistent coding styles across editors.
āāā .gitignore # Specifies files and directories to ignore in Git commits.
āāā LICENSE # Open-source license for the project.
Contributing
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Submit a pull request with a detailed explanation of your changes.
License
This project is licensed under the MIT License. See the LICENSE
file for more details.
Troubleshooting
Common Errors
Error: Missing "paths" property in compilerOptions of tsconfig.json
- Ensure your
tsconfig.json
includes a validpaths
mapping undercompilerOptions
.
Error: Source directory not found.
- Verify the directory path you provided exists and contains the source files.
Error: Could not parse tsconfig.json. Ensure the file is valid.
- Check for syntax errors or invalid JSON in your
tsconfig.json
.
Future Features
- Dry Run Mode: Preview changes before applying them.
- Custom Output Directory: Specify a directory to save updated files.
- Nx support: Add support for Nx workspace folder structures that use multiple
tsconfig
files.
Feel free to suggest features or report issues by opening an issue on GitHub! š