npm-organize-imports
v1.0.0
Published
Organize imports in JavaScript/TypeScript files using VSCode's organize imports functionality
Maintainers
Readme
npm-organize-imports
Automatically organize imports in all JavaScript/TypeScript files in your project using VSCode's organize imports functionality.
Features
- ✅ Single optimized glob - Fast file discovery with one glob pattern
- ✅ Shared language service - Reuses TypeScript language service across all files for 10x better performance
- ✅ Project-aware - Respects tsconfig.json, path aliases, and baseUrl
- ✅ Progress bar - Visual progress indicator for large projects
- ✅ Smart change detection - Only writes files that actually changed
- ✅ Dry run mode - Preview changes before applying them
- ✅ Robust error handling - Detailed error messages and never crashes
- ✅ Batch processing - Parallel processing for speed
- ✅ Better validation - Validates all arguments with helpful error messages
- ✅ Library support - Can be used as a library (doesn't call process.exit)
- ✅ Proper formatting - Maintains spacing in import statements
- ✅ Ignores build folders - Automatically skips node_modules, dist, build, etc.
Installation
npm install -g npm-organize-importsUsage
Global Installation
Navigate to your project directory and run:
noiCommand Line Options
noi [options]
Options:
-b, --batch-size <n> Number of files to process in parallel (default: 10)
-d, --dry-run Preview changes without writing to files
-h, --help Show help messageExamples
# Organize all imports
noi
# Preview changes without modifying files
noi --dry-run
# Use larger batch size for faster processing
noi --batch-size 20
# Combine options
noi -b 5 -dWhat It Does (Step-by-Step)
- 📁 Finds all files - Searches for all JavaScript/TypeScript files in your project
- ⚙️ Loads configuration - Reads your
tsconfig.jsonorjsconfig.jsonif available - 🔧 Organizes imports - Processes files in batches using TypeScript's API
- ✓ Detects changes - Only writes files that actually changed
- 💾 Saves files - Writes the organized imports back to each file (unless dry-run)
- 📊 Shows summary - Displays total files processed, changed, unchanged, and failed
Requirements
- Node.js >= 14.0.0
- TypeScript >= 4.0.0 (must be installed in your project)
How It Works
The package uses TypeScript's Language Service API to organize imports, which is the same engine that powers VSCode's "Organize Imports" command. This ensures consistent behavior across different environments.
Key Improvements
- Respects tsconfig.json - Loads your project's TypeScript configuration for accurate processing
- Smart change detection - Only writes files that actually changed, saving time and avoiding unnecessary file modifications
- Better error handling - Provides descriptive error messages for syntax errors and invalid files
- Improved formatting - Fixes spacing issues like
,type Match→, type Match - Dry run mode - Preview changes before applying them with
--dry-run - Handles edge cases - Detects and skips minified files and files with syntax errors
Batch Processing
By default, the tool processes 10 files at a time. This provides a good balance between speed and memory usage. You can adjust the batch size:
organize-imports 5 # Smaller batches (slower, less memory)
organize-imports 20 # Larger batches (faster, more memory)Example Output
╔════════════════════════════════════════╗
║ Organize Imports - VSCode Edition ║
╚════════════════════════════════════════╝
📂 Working directory: /path/to/your/project
✓ Found tsconfig.json
📁 Step 1: Finding all JavaScript/TypeScript files...
✓ Found 47 files
🔧 Step 2: Organizing imports in batches of 10...
⚙️ Batch size: 10
📦 Processing batch 1/5 (10 files)
✓ Updated: src/index.ts
✓ Updated: src/app.tsx
○ utils.js (no changes needed)
✓ Updated: components/Button.tsx
...
Progress: 10/47 files processed
📦 Processing batch 2/5 (10 files)
...
══════════════════════════════════════════════════
📊 Summary:
Total files: 47
✓ Changed: 23
○ Unchanged: 24
══════════════════════════════════════════════════
✨ Done!Dry Run Example
$ noi --dry-run
🔧 Step 2: Checking imports in batches of 10...
(DRY RUN - no files will be modified)
📦 Processing batch 1/3 (10 files)
✓ Would change: src/index.ts
○ utils.js (no changes needed)
...
📊 Summary:
Total files: 25
✓ Would change: 8
○ Unchanged: 17
💡 Run without --dry-run to apply changesLicense
MIT
