envpro-cli
v1.0.0
Published
CLI tool for managing environment files with TypeScript support
Downloads
4
Readme
envpro 🌱
A powerful environment management tool for Node.js applications. Manage, encrypt, and switch between different environment configurations with ease.
Features ✨
- 🔐 Secure Encryption: Encrypt your environment files with AES-256-GCM
- 🔄 Environment Switching: Easily switch between different environments
- 🔑 Key Management: Automatic key generation and storage
- 📦 Backup System: Automatic backups with easy restoration
- 🛡️ Security: Secure key storage and encrypted files
- 🎯 TypeScript Support: Built with TypeScript for better development experience
- 🔍 Environment Validation: Validate environment variables against a schema
- 🔄 Automatic Loading: Load environment variables without dotenv dependency
Installation 📦
# Install globally
npm install -g envpro
# Or install as a dev dependency
npm install --save-dev envproQuick Start 🚀
Initialize envpro in your project:
envpro initCreate your first environment:
envpro new developmentAdd your environment variables:
# Edit .env.development DATABASE_URL=postgres://user:pass@localhost:5432/dev_db API_KEY=your_api_keyEncrypt your environment:
envpro encrypt developmentUse the environment in your code:
import { useEnv, getEnvVar } from 'envpro/utils/env-loader'; // Load environment variables await useEnv({ environment: 'development' }); // Access variables const dbUrl = getEnvVar('DATABASE_URL');
Sample Project Usage 🏗️
1. Project Setup
# Initialize project
mkdir my-app
cd my-app
npm init -y
npm install --save-dev envpro typescript @types/node2. Configure TypeScript
// tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true
}
}3. Initialize envpro
envpro init
envpro new development
envpro new production4. Add Environment Variables
# .env.development
DATABASE_URL=postgres://user:pass@localhost:5432/dev_db
API_KEY=dev_api_key
PORT=3000
NODE_ENV=development
# .env.production
DATABASE_URL=postgres://user:pass@prod-db:5432/prod_db
API_KEY=prod_api_key
PORT=80
NODE_ENV=production5. Encrypt Environments
envpro encrypt development
envpro encrypt production6. Create Application Code
// src/index.ts
import { useEnv, getEnvVar } from 'envpro/utils/env-loader';
async function main() {
// Load environment variables
await useEnv({
environment: process.env.NODE_ENV || 'development'
});
// Access variables
const dbUrl = getEnvVar('DATABASE_URL');
const port = getEnvVar('PORT') || '3000';
const apiKey = getEnvVar('API_KEY');
console.log('Database URL:', dbUrl);
console.log('Port:', port);
console.log('API Key:', apiKey ? '***' : 'Not set');
}
main().catch(console.error);7. Build and Run
# Build
npm run build
# Run in development
NODE_ENV=development node dist/index.js
# Run in production
NODE_ENV=production node dist/index.jsAdvanced Usage Examples 🚀
1. Environment Validation
import { useEnv, getEnvVar } from 'envpro/utils/env-loader';
interface EnvSchema {
DATABASE_URL: string;
API_KEY: string;
PORT?: string;
}
async function validateEnv(): Promise<EnvSchema> {
await useEnv();
const requiredVars = ['DATABASE_URL', 'API_KEY'];
for (const key of requiredVars) {
if (!getEnvVar(key)) {
throw new Error(`Missing required environment variable: ${key}`);
}
}
return {
DATABASE_URL: getEnvVar('DATABASE_URL')!,
API_KEY: getEnvVar('API_KEY')!,
PORT: getEnvVar('PORT')
};
}2. Multiple Environment Support
import { useEnv, getEnvVar } from 'envpro/utils/env-loader';
class Config {
private static instance: Config;
private env: string;
private constructor() {
this.env = process.env.NODE_ENV || 'development';
}
static async getInstance(): Promise<Config> {
if (!Config.instance) {
Config.instance = new Config();
await useEnv({
environment: Config.instance.env,
override: true
});
}
return Config.instance;
}
getDatabaseUrl(): string {
return getEnvVar('DATABASE_URL')!;
}
getApiKey(): string {
return getEnvVar('API_KEY')!;
}
getPort(): number {
return parseInt(getEnvVar('PORT') || '3000', 10);
}
}
// Usage
async function main() {
const config = await Config.getInstance();
console.log('Database URL:', config.getDatabaseUrl());
console.log('Port:', config.getPort());
}3. Environment Switching in Tests
import { useEnv } from 'envpro/utils/env-loader';
describe('My Test Suite', () => {
beforeAll(async () => {
await useEnv({
environment: 'test',
override: true
});
});
it('should use test environment variables', () => {
expect(process.env.NODE_ENV).toBe('test');
// ... rest of your test
});
});Security Best Practices 🔒
Key Management:
- Keys are stored in
.envpro/keys/ - Each environment has its own key
- Keys are automatically generated and managed
- Keys are stored in
File Security:
- Never commit unencrypted
.envfiles - Keep
.envpro/keys/in a secure location - Use different keys for different environments
- Never commit unencrypted
Backup Strategy:
- Regular backups using
envpro backup - Store backups securely
- Use the restore system for recovery
- Regular backups using
Troubleshooting 🛠️
Common Issues
Key Not Found
❌ No encryption key found! 💡 Please ensure: 1. The key exists in .envpro/keys/ 2. Or set it as an environment variable: ENCRYPTION_KEY=your_keySolution: Run
envpro encrypt <environment>to generate a new key.File Not Found
❌ Environment file not found: .env.productionSolution: Create the environment first using
envpro new production.Decryption Failed
❌ Decryption failed: Invalid encryption keySolution: Ensure you're using the correct key for the environment.
Contributing 🤝
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
License 📄
MIT License - feel free to use this tool in your projects!
Support 💖
If you find this tool helpful, please consider giving it a ⭐️ on GitHub!
Made with ❤️ by [Avijit Sen]
