fix(qenv): Improve documentation, update dependencies, and refine project configuration
This commit is contained in:
38
.serena/memories/code_style_conventions.md
Normal file
38
.serena/memories/code_style_conventions.md
Normal file
@@ -0,0 +1,38 @@
|
||||
# Code Style and Conventions
|
||||
|
||||
## TypeScript Conventions
|
||||
- Use TypeScript with ES modules (type: "module" in package.json)
|
||||
- All files use `.js` extensions in imports (even for .ts files)
|
||||
- Interfaces prefixed with `I` (e.g., `IUserData`)
|
||||
- Types prefixed with `T` (e.g., `TEnvVarRef`)
|
||||
- Filenames must be lowercase
|
||||
- Avoid ENUMs when possible
|
||||
|
||||
## File Organization
|
||||
- Source code in `ts/` directory
|
||||
- Tests in `test/` directory
|
||||
- Plugin imports in `ts/qenv.plugins.ts`
|
||||
- Main exports in `ts/index.ts`
|
||||
- Class files named as `qenv.classes.<classname>.ts`
|
||||
|
||||
## Import Style
|
||||
- Import all dependencies in plugins file
|
||||
- Reference as `plugins.moduleName.method()`
|
||||
- Use full import paths with .js extension
|
||||
- Group imports: external packages, then internal modules
|
||||
|
||||
## Testing
|
||||
- Use @git.zone/tstest framework
|
||||
- Import expect from tapbundle: `import { tap, expect } from '@git.zone/tstest/tapbundle'`
|
||||
- Test files end with `export default tap.start()` (tstest pattern)
|
||||
- Test file naming: `*.both.ts`, `*.node.ts`, or `*.browser.ts`
|
||||
|
||||
## Documentation
|
||||
- Keep comments minimal unless specifically requested
|
||||
- README should be lowercase: `readme.md`
|
||||
- Documentation should be engaging and use emojis where appropriate
|
||||
|
||||
## Git Conventions
|
||||
- Small, focused commits
|
||||
- Use `git mv` for file operations
|
||||
- Never commit without running tests and type checks
|
37
.serena/memories/project_overview.md
Normal file
37
.serena/memories/project_overview.md
Normal file
@@ -0,0 +1,37 @@
|
||||
# Qenv Project Overview
|
||||
|
||||
## Purpose
|
||||
@push.rocks/qenv is a TypeScript/Node.js module for managing environment variables in Node.js projects. It provides a unified interface for loading environment variables from multiple sources:
|
||||
- Process environment variables
|
||||
- YAML configuration files (.yml/.yaml)
|
||||
- JSON configuration files (.json)
|
||||
- Docker secrets
|
||||
- Dynamic/async variable resolution via functions
|
||||
|
||||
## Key Features
|
||||
- Support for required and optional environment variables
|
||||
- Multi-source loading hierarchy (env vars > config files > Docker secrets)
|
||||
- Base64-encoded object support for complex configurations
|
||||
- Synchronous and asynchronous variable retrieval
|
||||
- Strict mode with error throwing for unset variables
|
||||
- TypeScript with full type definitions
|
||||
- Logging via @push.rocks/smartlog
|
||||
|
||||
## Main Components
|
||||
- `Qenv` class: Core class for environment variable management
|
||||
- `CloudlyAdapter`: Configuration vault integration
|
||||
- Support for `qenv.yml` (required vars definition) and `env.yml`/`env.json` (values)
|
||||
|
||||
## Tech Stack
|
||||
- TypeScript
|
||||
- Node.js
|
||||
- pnpm package manager
|
||||
- @push.rocks ecosystem libraries (smartfile, smartlog, smartpath)
|
||||
- @git.zone tools for building and testing
|
||||
|
||||
## Dependencies
|
||||
- @api.global/typedrequest
|
||||
- @configvault.io/interfaces
|
||||
- @push.rocks/smartfile
|
||||
- @push.rocks/smartlog
|
||||
- @push.rocks/smartpath
|
30
.serena/memories/suggested_commands.md
Normal file
30
.serena/memories/suggested_commands.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# Suggested Commands for Qenv Development
|
||||
|
||||
## Build & Test Commands
|
||||
- `pnpm test` - Run tests with tstest
|
||||
- `pnpm build` - Build the project with tsbuild (includes --web --allowimplicitany flags)
|
||||
- `pnpm buildDocs` - Generate documentation with tsdoc
|
||||
|
||||
## Development Tools
|
||||
- `tsx <file>` - Execute TypeScript files directly (globally available)
|
||||
- `tstest test/test.some.ts --verbose` - Run specific test file with verbose output
|
||||
|
||||
## Package Management
|
||||
- `pnpm install` - Install dependencies
|
||||
- `pnpm install --save-dev <package>` - Add development dependency
|
||||
- `pnpm install <package>` - Add production dependency
|
||||
|
||||
## Git Operations
|
||||
- `git mv <old> <new>` - Move/rename files preserving history
|
||||
- `git status` - Check current repository status
|
||||
- `git diff` - View uncommitted changes
|
||||
|
||||
## Type Checking
|
||||
- `tsbuild check test/**/* --skiplibcheck` - Type check test files
|
||||
- `pnpm run build` - Type check and build module files
|
||||
|
||||
## System Commands (Linux)
|
||||
- `ls` - List files
|
||||
- `find . -name "pattern"` - Find files by pattern
|
||||
- `rg "pattern"` - Search file contents (ripgrep)
|
||||
- `curl` - Make HTTP requests for testing/debugging
|
32
.serena/memories/task_completion_checklist.md
Normal file
32
.serena/memories/task_completion_checklist.md
Normal file
@@ -0,0 +1,32 @@
|
||||
# Task Completion Checklist
|
||||
|
||||
When completing any development task in the qenv project, follow these steps:
|
||||
|
||||
## Pre-Commit Checks
|
||||
1. **Run Tests**: `pnpm test`
|
||||
- Ensure all tests pass
|
||||
- Add new tests for new functionality
|
||||
|
||||
2. **Type Checking**: `pnpm run build`
|
||||
- Verify no TypeScript errors
|
||||
- Check that build output is generated correctly
|
||||
|
||||
3. **Code Quality**:
|
||||
- Verify code follows project conventions
|
||||
- Ensure no console.log statements left in production code
|
||||
- Check imports use proper .js extensions
|
||||
|
||||
## Documentation Updates
|
||||
- Update README.md if API changes
|
||||
- Update changelog.md for notable changes
|
||||
- Ensure JSDoc comments for public APIs
|
||||
|
||||
## Final Verification
|
||||
- Review changes with `git diff`
|
||||
- Test the module can be imported correctly
|
||||
- Verify no breaking changes to public API
|
||||
|
||||
## Important Notes
|
||||
- NEVER commit without explicit user approval
|
||||
- Always run tests and build before suggesting commit
|
||||
- If lint/typecheck commands unknown, ask user and update CLAUDE.md
|
68
.serena/project.yml
Normal file
68
.serena/project.yml
Normal file
@@ -0,0 +1,68 @@
|
||||
# language of the project (csharp, python, rust, java, typescript, go, cpp, or ruby)
|
||||
# * For C, use cpp
|
||||
# * For JavaScript, use typescript
|
||||
# Special requirements:
|
||||
# * csharp: Requires the presence of a .sln file in the project folder.
|
||||
language: typescript
|
||||
|
||||
# whether to use the project's gitignore file to ignore files
|
||||
# Added on 2025-04-07
|
||||
ignore_all_files_in_gitignore: true
|
||||
# list of additional paths to ignore
|
||||
# same syntax as gitignore, so you can use * and **
|
||||
# Was previously called `ignored_dirs`, please update your config if you are using that.
|
||||
# Added (renamed)on 2025-04-07
|
||||
ignored_paths: []
|
||||
|
||||
# whether the project is in read-only mode
|
||||
# If set to true, all editing tools will be disabled and attempts to use them will result in an error
|
||||
# Added on 2025-04-18
|
||||
read_only: false
|
||||
|
||||
|
||||
# list of tool names to exclude. We recommend not excluding any tools, see the readme for more details.
|
||||
# Below is the complete list of tools for convenience.
|
||||
# To make sure you have the latest list of tools, and to view their descriptions,
|
||||
# execute `uv run scripts/print_tool_overview.py`.
|
||||
#
|
||||
# * `activate_project`: Activates a project by name.
|
||||
# * `check_onboarding_performed`: Checks whether project onboarding was already performed.
|
||||
# * `create_text_file`: Creates/overwrites a file in the project directory.
|
||||
# * `delete_lines`: Deletes a range of lines within a file.
|
||||
# * `delete_memory`: Deletes a memory from Serena's project-specific memory store.
|
||||
# * `execute_shell_command`: Executes a shell command.
|
||||
# * `find_referencing_code_snippets`: Finds code snippets in which the symbol at the given location is referenced.
|
||||
# * `find_referencing_symbols`: Finds symbols that reference the symbol at the given location (optionally filtered by type).
|
||||
# * `find_symbol`: Performs a global (or local) search for symbols with/containing a given name/substring (optionally filtered by type).
|
||||
# * `get_current_config`: Prints the current configuration of the agent, including the active and available projects, tools, contexts, and modes.
|
||||
# * `get_symbols_overview`: Gets an overview of the top-level symbols defined in a given file.
|
||||
# * `initial_instructions`: Gets the initial instructions for the current project.
|
||||
# Should only be used in settings where the system prompt cannot be set,
|
||||
# e.g. in clients you have no control over, like Claude Desktop.
|
||||
# * `insert_after_symbol`: Inserts content after the end of the definition of a given symbol.
|
||||
# * `insert_at_line`: Inserts content at a given line in a file.
|
||||
# * `insert_before_symbol`: Inserts content before the beginning of the definition of a given symbol.
|
||||
# * `list_dir`: Lists files and directories in the given directory (optionally with recursion).
|
||||
# * `list_memories`: Lists memories in Serena's project-specific memory store.
|
||||
# * `onboarding`: Performs onboarding (identifying the project structure and essential tasks, e.g. for testing or building).
|
||||
# * `prepare_for_new_conversation`: Provides instructions for preparing for a new conversation (in order to continue with the necessary context).
|
||||
# * `read_file`: Reads a file within the project directory.
|
||||
# * `read_memory`: Reads the memory with the given name from Serena's project-specific memory store.
|
||||
# * `remove_project`: Removes a project from the Serena configuration.
|
||||
# * `replace_lines`: Replaces a range of lines within a file with new content.
|
||||
# * `replace_symbol_body`: Replaces the full definition of a symbol.
|
||||
# * `restart_language_server`: Restarts the language server, may be necessary when edits not through Serena happen.
|
||||
# * `search_for_pattern`: Performs a search for a pattern in the project.
|
||||
# * `summarize_changes`: Provides instructions for summarizing the changes made to the codebase.
|
||||
# * `switch_modes`: Activates modes by providing a list of their names
|
||||
# * `think_about_collected_information`: Thinking tool for pondering the completeness of collected information.
|
||||
# * `think_about_task_adherence`: Thinking tool for determining whether the agent is still on track with the current task.
|
||||
# * `think_about_whether_you_are_done`: Thinking tool for determining whether the task is truly completed.
|
||||
# * `write_memory`: Writes a named memory (for future reference) to Serena's project-specific memory store.
|
||||
excluded_tools: []
|
||||
|
||||
# initial prompt for the project. It will always be given to the LLM upon activating the project
|
||||
# (contrary to the memories, which are loaded on demand).
|
||||
initial_prompt: ""
|
||||
|
||||
project_name: "qenv"
|
@@ -1,5 +1,14 @@
|
||||
# Changelog
|
||||
|
||||
## 2025-08-14 - 6.1.1 - fix(qenv)
|
||||
Improve documentation, update dependencies, and refine project configuration
|
||||
|
||||
- Revamp README with enhanced installation, configuration, and usage instructions
|
||||
- Add new project documentation files including code style conventions, project overview, suggested commands, and task checklist
|
||||
- Upgrade dependency versions in package.json (e.g. @git.zone/tsbuild, @git.zone/tstest, @push.rocks/smartfile, smartlog, and smartpath)
|
||||
- Adjust test import to use '@git.zone/tstest/tapbundle' for improved compatibility
|
||||
- Introduce new local configuration files (.claude/settings.local.json and .serena/project.yml) to standardize project settings
|
||||
|
||||
## 2024-11-22 - 6.1.0 - feat(core)
|
||||
Added new method getEnvVarOnDemandStrict to throw error for unset env vars
|
||||
|
||||
|
16
package.json
16
package.json
@@ -31,18 +31,17 @@
|
||||
},
|
||||
"homepage": "https://code.foss.global/push.rocks/qenv",
|
||||
"devDependencies": {
|
||||
"@git.zone/tsbuild": "^2.2.0",
|
||||
"@git.zone/tsbuild": "^2.6.4",
|
||||
"@git.zone/tsrun": "^1.3.3",
|
||||
"@git.zone/tstest": "^1.0.90",
|
||||
"@push.rocks/tapbundle": "^5.5.0",
|
||||
"@types/node": "^22.9.1"
|
||||
"@git.zone/tstest": "^2.3.2",
|
||||
"@types/node": "^22.13.13"
|
||||
},
|
||||
"dependencies": {
|
||||
"@api.global/typedrequest": "^3.1.10",
|
||||
"@configvault.io/interfaces": "^1.0.17",
|
||||
"@push.rocks/smartfile": "^11.0.21",
|
||||
"@push.rocks/smartlog": "^3.0.7",
|
||||
"@push.rocks/smartpath": "^5.0.18"
|
||||
"@push.rocks/smartfile": "^11.2.5",
|
||||
"@push.rocks/smartlog": "^3.1.8",
|
||||
"@push.rocks/smartpath": "^6.0.0"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
@@ -58,5 +57,6 @@
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
]
|
||||
],
|
||||
"packageManager": "pnpm@10.11.0+sha512.6540583f41cc5f628eb3d9773ecee802f4f9ef9923cc45b69890fb47991d4b092964694ec3a4f738a420c918a333062c8b925d312f42e4f0c263eb603551f977"
|
||||
}
|
||||
|
6408
pnpm-lock.yaml
generated
6408
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
384
readme.md
384
readme.md
@@ -1,109 +1,347 @@
|
||||
# @push.rocks/qenv
|
||||
easy promised environments
|
||||
# @push.rocks/qenv 🔐
|
||||
**Smart Environment Variable Management for Node.js**
|
||||
|
||||
## Install
|
||||
To install `@push.rocks/qenv`, you need to have Node.js installed on your system. Once Node.js is installed, you can add `@push.rocks/qenv` to your project by running the following command in your project's root directory:
|
||||
> Never hardcode secrets again. Load environment variables from multiple sources with ease and confidence.
|
||||
|
||||
## 🚀 Features
|
||||
|
||||
✅ **Multi-source Loading** - Automatically loads from environment variables, config files, and Docker secrets
|
||||
✅ **Type-Safe** - Full TypeScript support with comprehensive type definitions
|
||||
✅ **Flexible Formats** - Supports `.yml`, `.yaml`, and `.json` configuration files
|
||||
✅ **Docker Ready** - Built-in support for Docker secrets and secret.json files
|
||||
✅ **Async & Sync** - Both synchronous and asynchronous variable retrieval
|
||||
✅ **Strict Mode** - Optional strict mode that throws errors for missing variables
|
||||
✅ **Base64 Objects** - Handle complex configuration objects with automatic encoding/decoding
|
||||
✅ **Dynamic Resolution** - Support for async functions as environment variable sources
|
||||
|
||||
## 📦 Installation
|
||||
|
||||
```bash
|
||||
# Using npm
|
||||
npm install @push.rocks/qenv --save
|
||||
|
||||
# Using pnpm (recommended)
|
||||
pnpm add @push.rocks/qenv
|
||||
|
||||
# Using yarn
|
||||
yarn add @push.rocks/qenv
|
||||
```
|
||||
|
||||
This command will add `@push.rocks/qenv` as a dependency to your project, and you will be ready to use it in your application.
|
||||
|
||||
## Usage
|
||||
`@push.rocks/qenv` provides a convenient way to manage and access environment variables in your Node.js projects, especially when dealing with different environments like development, testing, and production. Its primary use is to load environment-specific variables in an easy and organized manner. Below is an extensive guide on how to use this module effectively in various scenarios, ensuring you can handle environment variables efficiently in your projects.
|
||||
|
||||
### Getting Started
|
||||
First, ensure you have TypeScript configured in your project. `@push.rocks/qenv` is fully typed, providing excellent IntelliSense support when working in editors that support TypeScript, such as Visual Studio Code.
|
||||
|
||||
#### Importing Qenv
|
||||
To get started, import the `Qenv` class from `@push.rocks/qenv`:
|
||||
## 🎯 Quick Start
|
||||
|
||||
```typescript
|
||||
import { Qenv } from '@push.rocks/qenv';
|
||||
|
||||
// Create a new Qenv instance
|
||||
const qenv = new Qenv('./', './', true);
|
||||
|
||||
// Access environment variables
|
||||
const dbHost = await qenv.getEnvVarOnDemand('DB_HOST');
|
||||
const apiKey = await qenv.getEnvVarOnDemand('API_KEY');
|
||||
|
||||
// Use strict mode to ensure variables exist
|
||||
const criticalVar = await qenv.getEnvVarOnDemandStrict('CRITICAL_CONFIG');
|
||||
// Throws error if CRITICAL_CONFIG is not set!
|
||||
```
|
||||
|
||||
#### Basic Configuration
|
||||
`@push.rocks/qenv` works with two main files: `qenv.yml` for specifying required environment variables, and `env.yml` for specifying values for these variables. These files should be placed in your project directory.
|
||||
## 📖 Configuration
|
||||
|
||||
##### qenv.yml
|
||||
This file specifies the environment variables that are required by your application. An example `qenv.yml` might look like this:
|
||||
### Setting Up Your Environment Files
|
||||
|
||||
#### 1. Define Required Variables (`qenv.yml`)
|
||||
|
||||
Create a `qenv.yml` file to specify which environment variables your application needs:
|
||||
|
||||
```yaml
|
||||
required:
|
||||
- DB_HOST
|
||||
- DB_USER
|
||||
- DB_PASS
|
||||
- DB_PASSWORD
|
||||
- API_KEY
|
||||
- LOG_LEVEL
|
||||
```
|
||||
|
||||
##### env.yml
|
||||
This file contains the actual values for the environment variables in a development or testing environment. An example `env.yml` could be:
|
||||
#### 2. Provide Values (`env.yml` or `env.json`)
|
||||
|
||||
For local development, create an `env.yml` or `env.json` file:
|
||||
|
||||
**env.yml:**
|
||||
```yaml
|
||||
DB_HOST: localhost
|
||||
DB_USER: user
|
||||
DB_PASS: pass
|
||||
DB_USER: developer
|
||||
DB_PASSWORD: supersecret123
|
||||
API_KEY: dev-key-12345
|
||||
LOG_LEVEL: debug
|
||||
```
|
||||
|
||||
#### Instantiating Qenv
|
||||
Create an instance of `Qenv` by providing paths to the directories containing the `qenv.yml` and `env.yml` files, respectively:
|
||||
|
||||
```typescript
|
||||
const myQenv = new Qenv('./path/to/dir/with/qenv', './path/to/dir/with/env');
|
||||
**env.json:**
|
||||
```json
|
||||
{
|
||||
"DB_HOST": "localhost",
|
||||
"DB_USER": "developer",
|
||||
"DB_PASSWORD": "supersecret123",
|
||||
"API_KEY": "dev-key-12345",
|
||||
"LOG_LEVEL": "debug"
|
||||
}
|
||||
```
|
||||
|
||||
If the `env.yml` file is in the same directory as `qenv.yml`, you can omit the second argument:
|
||||
> 💡 **Pro Tip:** Add `env.yml` and `env.json` to your `.gitignore` to keep secrets out of version control!
|
||||
|
||||
## 🔥 Advanced Usage
|
||||
|
||||
### Loading Priority
|
||||
|
||||
Qenv loads variables in this order (first found wins):
|
||||
1. **Process environment variables** - Already set in `process.env`
|
||||
2. **Configuration files** - From `env.yml` or `env.json`
|
||||
3. **Docker secrets** - From `/run/secrets/`
|
||||
4. **Docker secret JSON** - From `/run/secrets/secret.json`
|
||||
|
||||
### Handling Complex Objects
|
||||
|
||||
Store and retrieve complex configuration objects:
|
||||
|
||||
```typescript
|
||||
const myQenv = new Qenv('./path/to/dir/with/both');
|
||||
```
|
||||
|
||||
#### Accessing Environment Variables
|
||||
After instantiating `Qenv`, you can access the loaded environment variables directly from `process.env` in Node.js or through the `myQenv` instance for more complex scenarios like asynchronous variable resolution:
|
||||
|
||||
```typescript
|
||||
// Accessing directly via process.env
|
||||
console.log(process.env.DB_HOST); // 'localhost' in development environment
|
||||
|
||||
// Accessing via Qenv instance for more advanced scenarios
|
||||
(async () => {
|
||||
const dbHost = await myQenv.getEnvVarOnDemand('DB_HOST');
|
||||
console.log(dbHost); // 'localhost'
|
||||
})();
|
||||
```
|
||||
|
||||
### Advanced Usage
|
||||
#### Handling Missing Variables
|
||||
By default, `Qenv` will throw an error and exit if any of the required environment variables specified in `qenv.yml` are missing. You can disable this behavior by passing `false` as the third argument to the constructor, which allows your application to handle missing variables gracefully:
|
||||
|
||||
```typescript
|
||||
const myQenv = new Qenv('./path/to/dir/with/qenv', './path/to/dir/with/env', false);
|
||||
```
|
||||
|
||||
#### Dynamic Environment Variables
|
||||
For dynamic or computed environment variables, you can define functions that resolve these variables asynchronously. This is particularly useful for variables that require fetching from an external source:
|
||||
|
||||
```typescript
|
||||
// Define a function to fetch a variable
|
||||
const fetchDbHost = async () => {
|
||||
// Logic to fetch DB_HOST from an external service
|
||||
return 'dynamic.host';
|
||||
// In env.yml
|
||||
const config = {
|
||||
database: {
|
||||
host: 'localhost',
|
||||
port: 5432,
|
||||
options: {
|
||||
ssl: true,
|
||||
poolSize: 10
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Use the function with getEnvVarOnDemand
|
||||
(async () => {
|
||||
const dbHost = await myQenv.getEnvVarOnDemand(fetchDbHost);
|
||||
console.log(dbHost); // 'dynamic.host'
|
||||
})();
|
||||
// Qenv automatically handles base64 encoding
|
||||
const dbConfig = await qenv.getEnvVarOnDemandAsObject('DATABASE_CONFIG');
|
||||
console.log(dbConfig.database.options.poolSize); // 10
|
||||
```
|
||||
|
||||
#### Reading Variables from Docker Secrets or Other Sources
|
||||
Internally, `@push.rocks/qenv` supports reading from Docker secrets, providing flexibility for applications deployed in Docker environments. The module attempts to read each required variable from the process environment, a provided `env.yml` file, Docker secrets, or any custom source you integrate.
|
||||
### Dynamic Environment Variables
|
||||
|
||||
### Conclusion
|
||||
`@push.rocks/qenv` simplifies handling environment variables across different environments, making your application's configuration more manageable and secure. By separating variable definitions from their values and providing support for dynamic resolution, `@push.rocks/qenv` offers a robust solution for managing configuration in Node.js projects. Whether you're working in a local development environment, CI/CD pipelines, or production, `@push.rocks/qenv` ensures that you have the correct configuration for the task at hand.
|
||||
Load variables from external sources dynamically:
|
||||
|
||||
Note: Due to the complexity and depth of `@push.rocks/qenv`, this documentation aims to cover general and advanced usage comprehensively. Please refer to the module's official documentation and typed definitions for further details on specific features or configuration options.
|
||||
```typescript
|
||||
const qenv = new Qenv();
|
||||
|
||||
// Define an async function to fetch configuration
|
||||
const fetchFromVault = async () => {
|
||||
const response = await fetch('https://vault.example.com/api/secret');
|
||||
const data = await response.json();
|
||||
return data.secret;
|
||||
};
|
||||
|
||||
// Use the function as an environment variable source
|
||||
const secret = await qenv.getEnvVarOnDemand(fetchFromVault);
|
||||
```
|
||||
|
||||
### Working with Docker
|
||||
|
||||
Qenv seamlessly integrates with Docker secrets:
|
||||
|
||||
```dockerfile
|
||||
# docker-compose.yml
|
||||
version: '3.7'
|
||||
services:
|
||||
app:
|
||||
image: your-app
|
||||
secrets:
|
||||
- db_password
|
||||
- api_key
|
||||
|
||||
secrets:
|
||||
db_password:
|
||||
external: true
|
||||
api_key:
|
||||
external: true
|
||||
```
|
||||
|
||||
Your application automatically reads from `/run/secrets/`:
|
||||
|
||||
```typescript
|
||||
const qenv = new Qenv();
|
||||
// Automatically loads from /run/secrets/db_password
|
||||
const dbPassword = await qenv.getEnvVarOnDemand('db_password');
|
||||
```
|
||||
|
||||
### Handling Missing Variables
|
||||
|
||||
Control how your application handles missing environment variables:
|
||||
|
||||
```typescript
|
||||
// Fail fast (default behavior)
|
||||
const qenvStrict = new Qenv('./', './', true);
|
||||
// Application exits if required variables are missing
|
||||
|
||||
// Graceful handling
|
||||
const qenvRelaxed = new Qenv('./', './', false);
|
||||
// Application continues, you handle missing variables
|
||||
|
||||
// Check what's missing
|
||||
if (qenvRelaxed.missingEnvVars.length > 0) {
|
||||
console.warn('Missing variables:', qenvRelaxed.missingEnvVars);
|
||||
// Implement fallback logic
|
||||
}
|
||||
```
|
||||
|
||||
### Strict Mode for Critical Variables
|
||||
|
||||
Use the new strict getter when you absolutely need a variable:
|
||||
|
||||
```typescript
|
||||
try {
|
||||
// This will throw if TOKEN is not set
|
||||
const token = await qenv.getEnvVarOnDemandStrict('TOKEN');
|
||||
|
||||
// You can also check multiple fallback names
|
||||
const db = await qenv.getEnvVarOnDemandStrict(['DATABASE_URL', 'DB_CONNECTION']);
|
||||
} catch (error) {
|
||||
console.error('Critical configuration missing:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
```
|
||||
|
||||
## 🏗️ CI/CD Integration
|
||||
|
||||
### GitHub Actions
|
||||
|
||||
```yaml
|
||||
name: Deploy
|
||||
on: [push]
|
||||
jobs:
|
||||
deploy:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Deploy with secrets
|
||||
env:
|
||||
API_KEY: ${{ secrets.API_KEY }}
|
||||
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
|
||||
run: |
|
||||
npm install
|
||||
npm run deploy
|
||||
```
|
||||
|
||||
### GitLab CI
|
||||
|
||||
```yaml
|
||||
deploy:
|
||||
stage: deploy
|
||||
script:
|
||||
- npm install
|
||||
- npm run deploy
|
||||
variables:
|
||||
API_KEY: $CI_API_KEY
|
||||
DB_PASSWORD: $CI_DB_PASSWORD
|
||||
```
|
||||
|
||||
## 🎭 Testing
|
||||
|
||||
For testing, create a separate `test/assets/env.yml`:
|
||||
|
||||
```typescript
|
||||
import { Qenv } from '@push.rocks/qenv';
|
||||
|
||||
describe('MyApp', () => {
|
||||
let qenv: Qenv;
|
||||
|
||||
beforeEach(() => {
|
||||
qenv = new Qenv('./test/assets', './test/assets', false);
|
||||
});
|
||||
|
||||
it('should load test configuration', async () => {
|
||||
const testVar = await qenv.getEnvVarOnDemand('TEST_VAR');
|
||||
expect(testVar).toBe('test-value');
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
## 🔍 Debugging
|
||||
|
||||
Enable detailed logging to troubleshoot environment variable loading:
|
||||
|
||||
```typescript
|
||||
const qenv = new Qenv();
|
||||
|
||||
// Check what's loaded
|
||||
console.log('Required vars:', qenv.requiredEnvVars);
|
||||
console.log('Available vars:', qenv.availableEnvVars);
|
||||
console.log('Missing vars:', qenv.missingEnvVars);
|
||||
|
||||
// Access the logger
|
||||
qenv.logger.log('info', 'Custom log message');
|
||||
```
|
||||
|
||||
## 📊 Real-World Example
|
||||
|
||||
Here's how you might use qenv in a production Node.js application:
|
||||
|
||||
```typescript
|
||||
import { Qenv } from '@push.rocks/qenv';
|
||||
import { createServer } from './server';
|
||||
import { connectDatabase } from './database';
|
||||
|
||||
async function bootstrap() {
|
||||
// Initialize environment
|
||||
const qenv = new Qenv();
|
||||
|
||||
// Load critical configuration
|
||||
const config = {
|
||||
port: await qenv.getEnvVarOnDemand('PORT') || '3000',
|
||||
dbUrl: await qenv.getEnvVarOnDemandStrict('DATABASE_URL'),
|
||||
apiKey: await qenv.getEnvVarOnDemandStrict('API_KEY'),
|
||||
logLevel: await qenv.getEnvVarOnDemand('LOG_LEVEL') || 'info',
|
||||
features: await qenv.getEnvVarOnDemandAsObject('FEATURE_FLAGS')
|
||||
};
|
||||
|
||||
// Connect to database
|
||||
await connectDatabase(config.dbUrl);
|
||||
|
||||
// Start server
|
||||
const server = createServer(config);
|
||||
server.listen(config.port, () => {
|
||||
console.log(`🚀 Server running on port ${config.port}`);
|
||||
});
|
||||
}
|
||||
|
||||
bootstrap().catch(error => {
|
||||
console.error('Failed to start application:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
```
|
||||
|
||||
## 🤝 API Reference
|
||||
|
||||
### Class: `Qenv`
|
||||
|
||||
#### Constructor
|
||||
```typescript
|
||||
new Qenv(
|
||||
qenvFileBasePathArg?: string, // Path to qenv.yml (default: process.cwd())
|
||||
envFileBasePathArg?: string, // Path to env.yml/json (default: same as qenv)
|
||||
failOnMissing?: boolean // Exit on missing vars (default: true)
|
||||
)
|
||||
```
|
||||
|
||||
#### Methods
|
||||
|
||||
| Method | Description | Returns |
|
||||
|--------|-------------|---------|
|
||||
| `getEnvVarOnDemand(name)` | Get environment variable value | `Promise<string \| undefined>` |
|
||||
| `getEnvVarOnDemandStrict(name)` | Get variable or throw error | `Promise<string>` |
|
||||
| `getEnvVarOnDemandSync(name)` | Synchronously get variable | `string \| undefined` |
|
||||
| `getEnvVarOnDemandAsObject(name)` | Get variable as decoded object | `Promise<any>` |
|
||||
|
||||
#### Properties
|
||||
|
||||
| Property | Type | Description |
|
||||
|----------|------|-------------|
|
||||
| `requiredEnvVars` | `string[]` | List of required variable names |
|
||||
| `availableEnvVars` | `string[]` | List of found variable names |
|
||||
| `missingEnvVars` | `string[]` | List of missing variable names |
|
||||
| `keyValueObject` | `object` | All loaded variables as key-value pairs |
|
||||
|
||||
## License and Legal Information
|
||||
|
||||
@@ -122,4 +360,4 @@ Registered at District court Bremen HRB 35230 HB, Germany
|
||||
|
||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||
|
||||
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
||||
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
@@ -1,5 +1,5 @@
|
||||
import * as path from 'path';
|
||||
import { tap, expect } from '@push.rocks/tapbundle';
|
||||
import { tap, expect } from '@git.zone/tstest/tapbundle';
|
||||
import * as qenv from '../ts/index.js';
|
||||
|
||||
import * as smartpath from '@push.rocks/smartpath';
|
||||
|
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/qenv',
|
||||
version: '6.1.0',
|
||||
version: '6.1.1',
|
||||
description: 'A module for easily handling environment variables in Node.js projects with support for .yml and .json configuration.'
|
||||
}
|
||||
|
Reference in New Issue
Block a user