Skip to main content
Use Droid Exec to refactor import statements across hundreds of files. The workflow groups, sorts, and optimizes imports while removing unused dependencies and converting module formats.

How it works

The script:
  1. Finds files: Searches for all .js, .jsx, .ts, and .tsx files
  2. Filters smartly: Excludes node_modules, .git, dist, and build directories
  3. Checks for imports: Only processes files that contain import statements
  4. Groups imports: Organizes into external, internal, and relative imports
  5. Sorts alphabetically: Within each group for consistency
  6. Removes unused: Eliminates imports that aren’t referenced
  7. Modernizes syntax: Converts require() to ES6 import
  8. Consolidates duplicates: Merges multiple imports from the same module

Get the script

Prerequisites

Before you begin, complete the Droid Exec installation.

Basic usage

Preview changes (dry run)

Always start with a dry run to preview changes before modifying files. This helps you understand what transformations will be applied.
The dry run feature is controlled by the DRY_RUN environment variable:
How dry run works:
  • 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
This is particularly useful for:
  • 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:
Actual execution example:

Real-world transformations

Example 1: CommonJS to ES6 Conversion

Example 2: Consolidating Duplicate Imports

Best practices

Follow these best practices for safe and effective import refactoring.
1

Start with a dry run

Always preview changes before modifying files:
2

Test on a small scope first

Start with a specific subdirectory before processing entire codebase:
3

Process incrementally

For large codebases, process directories separately for easier review: