env_builder_cli 1.1.3
env_builder_cli: ^1.1.3 copied to clipboard
A Flutter CLI tool to automate the creation and maintenance of the env Flutter package from multiple .env files.
env_builder_cli #
A powerful Dart CLI tool that automates the creation and maintenance of environment packages for Flutter applications. Generate type-safe environment variable access from .env files with built-in encryption support.
Features #
- ๐ Automated Environment Package Generation: Automatically creates Flutter packages from
.envfiles - ๐ Built-in Encryption: AES encryption support for sensitive environment variables
- ๐ Type-Safe Access: Generates Dart classes using Envied for compile-time safety
- ๐๏ธ Flutter Integration: Seamlessly integrates with Flutter projects and handles pubspec dependencies
- ๐ Multi-Environment Support: Handle development, staging, production, and custom environments
- ๐ Git Integration: Automatic
.gitignoreupdates with appropriate environment file rules - ๐งช Testing Support: Generates test files for environment variable validation
Installation #
Global Installation #
Install the CLI globally using pub:
dart pub global activate env_builder_cli
Local Installation #
Add to your pubspec.yaml:
dev_dependencies:
env_builder_cli: ^1.1.2
Usage #
Basic Usage #
Navigate to your Flutter project root and run:
# Build with all .env* files found in current directory (.env.ci, .env.custom, .env.app, etc.)
env_builder build
# Build with specific environment files
env_builder build --env-file=.env.development,.env.production,.env.staging
This will:
- Create a
packages/envdirectory - Copy your
.envfiles to the env package - Generate Dart classes for type-safe access
- Update dependencies in
pubspec.yamlfiles - Run
flutter pub getautomatically
Commands #
Build Command
Generates environment packages from .env files:
# Build with default configuration (auto-detects .env* files)
env_builder build
# Build with specific environment files
env_builder build --env-file=.env.development,.env.production,.env.staging
# Build with custom output directory (default: env)
env_builder build --output-dir=custom_env
# Skip encryption of sensitive variables
env_builder build --no-encrypt
# Show detailed output during build process
env_builder build --verbose
Planned Features:
- Complex Data Types Support: Handle JSON-like strings (e.g.,
APP_CONFIG={"theme":"dark","features":["chat","notifications"]}) --config-env-file: Specify a default configuration file for environment-specific settings
Encrypt Command
Encrypt sensitive environment files:
env_builder encrypt --password=yourSecretKey .env
Decrypt Command
Decrypt previously encrypted environment files:
env_builder decrypt --password=yourSecretKey .env.encrypted
Version Command
Displays version information:
env_builder version
# or
env_builder --version
Aliases:
--version,-v
Displays:
- CLI version (from pubspec.yaml)
- Dart SDK version
- Tool description
- Homepage URL
Environment File Format #
Create .env files in your project root:
# .env.development
BASE_URL=https://dev-api.example.com
API_KEY=dev_key_123
DEBUG=true
# .env.production
BASE_URL=https://api.example.com
API_KEY=prod_key_456
DEBUG=false
Generated Code #
The tool generates type-safe Dart classes:
// env.development.dart
import 'package:envied/envied.dart';
part 'env.development.g.dart';
@Envied(path: '.env.development')
abstract class EnvDevelopment {
@EnviedField(varName: 'BASE_URL')
static const String baseUrl = _EnvDevelopment.baseUrl;
@EnviedField(varName: 'API_KEY', obfuscate: true)
static final String apiKey = _EnvDevelopment.apiKey;
@EnviedField(varName: 'DEBUG')
static const bool debug = _EnvDevelopment.debug;
}
Flutter Integration #
In your Flutter app, use the generated environments:
import 'package:env/env.dart';
// Access environment variables
final appFlavor = AppFlavor.production();
class ApiService {
final appBaseUrl = appFlavor.getEnv(Env.baseUrl);
final apikey = appFlavor.getEnv(Env.apiKey);
}
Project Structure #
After running the build command, your project structure will look like:
your-flutter-project/
โโโ packages/
โ โโโ env/
โ โโโ .env.development
โ โโโ .env.production
โ โโโ env.development.dart
โ โโโ env.production.dart
โ โโโ env.dart (barrel export)
โ โโโ env.g.dart (enum definitions)
โ โโโ pubspec.yaml
โโโ .env.development
โโโ .env.production
โโโ pubspec.yaml (updated with env dependency)
โโโ lib/
โโโ main.dart
Security Best Practices #
- Never commit .env files - Add them to
.gitignore - Use encryption for sensitive production variables
- Store secrets securely in your CI/CD platform
- Use different keys for different environments
- Rotate secrets regularly
Examples #
Check the example/ directory for a complete working example.
To run the example:
cd example
flutter pub get
# The .env file already exists
env_builder build
flutter run
Contributing #
Contributions are welcome! Please see CONTRIBUTING.md for details.
License #
This project is licensed under the MIT License - see the LICENSE file for details.
Made with โค๏ธ for the Flutter community