@sagar-tarak/envswitch
v1.0.0
Published
A powerful CLI tool for switching between different environment configurations with a single command
Downloads
1
Maintainers
Readme
EnvSwitch
A powerful CLI tool for switching between different environment configurations with a single command. Perfect for developers working across multiple environments (local, development, staging, production).
🚀 Features
- Single Command Switching: Switch environments with
envswitch use <env> - Interactive Mode: User-friendly prompts for environment selection
- Automatic Backup: Safely backs up your current
.envfile before switching - Configuration Management: Easy setup and management of environment variables
- Cross-Platform: Works on Windows, macOS, and Linux
- Project Detection: Automatically finds your project root directory
- Template Support: Pre-built templates for quick setup
- Validation: Built-in configuration validation and error checking
📦 Installation
Global Installation (Recommended)
npm install -g envswitchLocal Installation
npm install --save-dev envswitchFrom Source
git clone https://github.com/envswitch/envswitch.git
cd envswitch
npm install
npm link # Makes envswitch available globally🎯 Quick Start
1. Initialize Configuration
# Interactive setup
envswitch init
# Quick setup with basic template
envswitch init --yes
# Use full template
envswitch init --template full2. Switch Environments
# Switch to development environment
envswitch use dev
# Switch to production environment
envswitch use production
# Switch to local environment
envswitch use local
# Force switch without confirmation
envswitch use dev --force3. Interactive Mode
envswitchRun without arguments to get an interactive prompt for environment selection.
📋 Commands
| Command | Description | Options |
|---------|-------------|---------|
| envswitch use <env> | Switch to a specific environment | -f, --force |
| envswitch list | List all available environments | -v, --verbose |
| envswitch status | Show currently active environment | -j, --json |
| envswitch init | Initialize configuration file | -y, --yes, -t, --template |
| envswitch validate | Validate configuration file | |
| envswitch | Interactive mode (default) | |
⚙️ Configuration
The tool uses a envswitch.config.json file in your project root. Here's an example:
{
"local": {
"API_URL": "http://localhost:3000",
"DB_URL": "mongodb://localhost:27017",
"NODE_ENV": "development",
"LOG_LEVEL": "debug",
"REDIS_URL": "redis://localhost:6379"
},
"dev": {
"API_URL": "https://dev-api.company.com",
"DB_URL": "mongodb://dev-db.company.com:27017",
"NODE_ENV": "development",
"LOG_LEVEL": "info",
"REDIS_URL": "redis://dev-redis.company.com:6379"
},
"staging": {
"API_URL": "https://staging-api.company.com",
"DB_URL": "mongodb://staging-db.company.com:27017",
"NODE_ENV": "staging",
"LOG_LEVEL": "warn",
"REDIS_URL": "redis://staging-redis.company.com:6379"
},
"production": {
"API_URL": "https://api.company.com",
"DB_URL": "mongodb://prod-db.company.com:27017",
"NODE_ENV": "production",
"LOG_LEVEL": "error",
"REDIS_URL": "redis://prod-redis.company.com:6379"
}
}🔄 How It Works
- Configuration Loading: Reads environment variables from
envswitch.config.json - Validation: Ensures the requested environment exists and config is valid
- Backup Creation: Safely backs up current
.envto.env.backup - Environment Switch: Writes new environment variables to
.env - Metadata Update: Stores switching information in
.envswitch.metadata - Confirmation: Provides clear feedback on successful switch
📁 File Structure
your-project/
├── .env # Current environment variables
├── .env.backup # Backup of previous .env
├── .envswitch.metadata # Tool metadata
├── envswitch.config.json # Environment configurations
└── package.json🛠️ Advanced Usage
Template Configuration
# Use basic template (local environment only)
envswitch init --template basic
# Use full template (local, dev, production)
envswitch init --template full
# Custom setup with prompts
envswitch initVerbose Environment Listing
# Show detailed environment information
envswitch list --verboseJSON Status Output
# Get status in JSON format for scripting
envswitch status --jsonConfiguration Validation
# Validate your configuration file
envswitch validateForce Environment Switch
# Skip confirmation prompts
envswitch use production --force🔒 Security Considerations
- Never commit sensitive data like API keys or passwords to version control
- Use environment-specific secrets for production environments
- Consider using .gitignore for your
.envfiles - Backup files are created automatically but may contain sensitive information
- Validate configurations before switching to production environments
🐛 Troubleshooting
Common Issues
"Configuration file not found"
envswitch init"Environment not found"
envswitch list # Check available environmentsPermission errors
# Ensure write permissions in project directory
chmod 755 .Invalid configuration
envswitch validate # Check for configuration issuesGetting Help
envswitch --help # Show all commands
envswitch use --help # Show command-specific help
envswitch --version # Show version information🧪 Testing
Run the test suite to ensure everything works correctly:
# Install dependencies
npm install
# Run tests
npm test
# Run tests with coverage
npm run test:coverage
# Run linting
npm run lint🤝 Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Development Setup
git clone https://github.com/envswitch/envswitch.git
cd envswitch
npm install
npm link
npm test📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🆘 Support
- Issues: GitHub Issues
- Documentation: GitHub Wiki
- Discussions: GitHub Discussions
- Security: Security Policy
🔮 Roadmap
- [ ] YAML configuration support
- [ ] Environment variable validation
- [ ] Pre/post switch hooks
- [ ] Cloud secret integration (AWS, Azure, GCP)
- [ ] Team configuration sharing
- [ ] CI/CD integration
- [ ] Environment comparison tools
- [ ] Migration utilities
📊 Statistics
- Downloads: npm stats
- Stars: GitHub stars
- Forks: GitHub forks
Made with ❤️ for developers who hate manual environment switching
Built with Node.js, Commander.js, and Inquirer.js
