How it works
The script:- Finds files: Searches for all
.js,.jsx,.ts, and.tsxfiles - Filters smartly: Excludes
node_modules,.git,dist, andbuilddirectories - Checks for imports: Only processes files that contain import statements
- Groups imports: Organizes into external, internal, and relative imports
- Sorts alphabetically: Within each group for consistency
- Removes unused: Eliminates imports that aren’t referenced
- Modernizes syntax: Converts
require()to ES6import - Consolidates duplicates: Merges multiple imports from the same module
Get the script
View full script source
View full script source
Prerequisites
Before you begin, complete the Droid Exec installation.Basic usage
Preview changes (dry run)
The dry run feature is controlled by theDRY_RUN environment variable:
- When
DRY_RUN=true: The script finds all files that need processing and shows which files have imports to refactor, but doesn’t modify any files - When
DRY_RUN=false(default): Actually runs the AI refactoring and modifies the files
- Testing on a small directory first to understand the changes
- Estimating time/cost before processing a large codebase
- Verifying the script finds the right files before committing to changes
Apply refactoring
Once you’re satisfied with the preview, run the actual refactoring:Real-world transformations
Example 1: CommonJS to ES6 Conversion
- Before
- After
Example 2: Consolidating Duplicate Imports
- Before
- After
Best practices
Follow these best practices for safe and effective import refactoring.
