monorepo-env-manager
v1.0.0
Published
A powerful environment variable manager for monorepo projects that provides a single source of truth for all your environment configurations.
Readme
monorepo-env-manager
A powerful environment variable manager for monorepo projects that provides a single source of truth for all your environment configurations.
Features
- Centralized management of .env files across monorepo
- Support for multiple environments (dev, staging, prod)
- Environment variable validation
- TypeScript support
- CLI tool for easy management
- Comprehensive error messages
Installation
npm install monorepo-env-managerGetting the Code
Option 1: Clone from GitHub
git clone https://github.com/yourusername/monorepo-env-manager.git
cd monorepo-env-manager
npm installOption 2: Download the Code
- Download the ZIP file from the repository
- Extract it to your desired location
- Navigate to the extracted directory
- Run
npm install
Building from Source
# Install dependencies
npm install
# Run tests
npm test
# Build the package
npm run buildUsage
Programmatic Usage
import { EnvManager } from 'monorepo-env-manager';
// Basic usage
const manager = new EnvManager({
rootDir: process.cwd(), // Root directory of your monorepo
});
// Load all environment variables
const env = manager.load();
console.log(env); // { API_KEY: 'value', DB_URL: 'mongodb://...' }
// With environment specific configuration
const prodManager = new EnvManager({
rootDir: process.cwd(),
environment: 'prod', // Will look for .env.prod files
});
// With validation
const validatedManager = new EnvManager({
rootDir: process.cwd(),
validate: [
{ key: 'API_KEY', required: true },
{ key: 'PORT', type: 'number' },
{ key: 'DEBUG', type: 'boolean' },
{ key: 'EMAIL', pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/ },
],
});
// Validate environment variables
const validation = validatedManager.validate();
if (!validation.isValid) {
console.error('Validation errors:', validation.errors);
}CLI Usage
The package includes a CLI tool for validating environment variables:
# Validate all .env files in the monorepo
npx monorepo-env-manager validate
# Validate environment-specific .env files
npx monorepo-env-manager validate --env prod
# Specify a custom root directory
npx monorepo-env-manager validate --dir /path/to/monorepoExamples
Check out the examples directory for complete usage examples.
Monorepo Structure Example
my-monorepo/
├── packages/
│ ├── api/
│ │ └── .env # API-specific variables
│ ├── web/
│ │ └── .env # Web app-specific variables
│ └── shared/
│ └── .env # Shared variables
├── .env # Root-level defaults
├── .env.development # Development environment overrides
├── .env.staging # Staging environment overrides
└── .env.production # Production environment overridesConfiguration
EnvManagerOptions
| Option | Type | Description | |--------|------|-------------| | rootDir | string | Root directory of your monorepo | | environment | string? | Environment name (e.g., 'dev', 'prod') | | validate | ValidationRule[]? | Array of validation rules |
ValidationRule
| Property | Type | Description | |----------|------|-------------| | key | string | Environment variable name | | required | boolean? | Whether the variable is required | | type | 'string' | 'number' | 'boolean'? | Expected type | | pattern | RegExp? | Regular expression pattern |
Environment Resolution
Variables are loaded and merged in the following order (later sources override earlier ones):
- Root-level
.env - Package-specific
.envfiles - Environment-specific
.env.{environment}files
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
MIT - see LICENSE for more details.
