Automatically organize imports across your entire codebase using Droid Exec
This tutorial demonstrates how to use Droid Exec to refactor import statements across hundreds of files simultaneously. The script intelligently groups, sorts, and optimizes imports while removing unused dependencies and converting module formats.
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:
Copy
Ask AI
# Preview what would happen (no changes made)DRY_RUN=true ./droid-refactor-imports.sh src# Example output:# === Droid Import Refactoring ===# Directory: src# Concurrency: 5# DRY RUN MODE# # Found 25 files to check# # Processing: src/components/Button.tsx# [DRY RUN] Would refactor imports# Processing: src/utils/api.ts# [DRY RUN] Would refactor imports
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
Once you’re satisfied with the preview, run the actual refactoring:
Copy
Ask AI
# Actually refactor the imports (default behavior)./droid-refactor-imports.sh packages/models# Or explicitly set DRY_RUN=falseDRY_RUN=false ./droid-refactor-imports.sh packages/models# Adjust concurrency for faster processingCONCURRENCY=10 ./droid-refactor-imports.sh packages/models
Actual execution example:
Copy
Ask AI
=== Droid Import Refactoring ===Directory: packages/modelsConcurrency: 5Found 78 files to checkProcessing: packages/models/src/organization/test-utils/fixtures.tsProcessing: packages/models/src/organization/agentReadiness/types.tsProcessing: packages/models/src/organization/utils.tsProcessing: packages/models/src/organization/agentReadiness/handlers.tsProcessing: packages/models/jest.config.tsProcessing: packages/models/src/organization/user/defaultRepositories/handlers.tsPerfect! I've successfully refactored the imports in the file...## SummaryI've successfully refactored the imports in `packages/models/src/organization/test-utils/fixtures.ts`. The imports are now properly organized with comments separating external packages from relative imports......
Follow these best practices for safe and effective import refactoring.
1
Start with a dry run
Always preview changes before modifying files:
Copy
Ask AI
# Preview what would happen without making changesDRY_RUN=true ./droid-refactor-imports.sh packages/models
2
Test on a small scope first
Start with a specific subdirectory before processing entire codebase:
Copy
Ask AI
# Test on a single module first./droid-refactor-imports.sh packages/models/src/utils# Then expand to larger directories./droid-refactor-imports.sh packages/models
3
Process incrementally
For large codebases, process directories separately for easier review:
Copy
Ask AI
# Process each package separately./droid-refactor-imports.sh packages/modelsgit add -A && git commit -m "refactor: organize imports in models"./droid-refactor-imports.sh packages/servicesgit add -A && git commit -m "refactor: organize imports in services"