Compare commits

...

55 Commits

Author SHA1 Message Date
7b34ef8453 6.1.0 2024-11-22 18:47:51 +01:00
e25406662f feat(core): Added new method getEnvVarOnDemandStrict to throw error for unset env vars 2024-11-22 18:47:50 +01:00
5b1e3a184b 6.0.8 2024-11-18 19:27:20 +01:00
b9677b110c fix(Qenv): Fix environment file path initialization logic. 2024-11-18 19:27:20 +01:00
df35680dea 6.0.7 2024-11-18 19:23:35 +01:00
ec68a4db92 fix(Qenv): Fix file path initialization for environment variable files 2024-11-18 19:23:35 +01:00
fdd4fc62c2 6.0.6 2024-11-18 19:19:30 +01:00
bedefd3efe fix(core): Improve handling of env.json and env.yml file checks 2024-11-18 19:19:30 +01:00
fa00f33c13 update description 2024-05-29 14:11:29 +02:00
4a24b7df65 update tsconfig 2024-04-14 17:16:43 +02:00
2449fc059f update npmextra.json: githost 2024-04-01 21:33:31 +02:00
c5c1e5ebe3 update npmextra.json: githost 2024-04-01 19:57:33 +02:00
04b0f81b6f update npmextra.json: githost 2024-03-30 21:46:30 +01:00
7655318629 6.0.5 2024-02-09 15:42:01 +01:00
754ffa6cac fix(core): update 2024-02-09 15:42:00 +01:00
b644ca0c1a 6.0.4 2023-10-20 18:18:48 +02:00
9f638d687b fix(core): update 2023-10-20 18:18:47 +02:00
a9bdfe9373 6.0.3 2023-10-20 17:21:52 +02:00
2017d51f11 fix(core): update 2023-10-20 17:21:51 +02:00
765011ad2a 6.0.2 2023-08-09 17:47:21 +02:00
d807cc6de2 fix(core): update 2023-08-09 17:47:20 +02:00
53721a41c2 6.0.1 2023-08-09 14:50:33 +02:00
c9f79e6ea4 fix(core): update 2023-08-09 14:50:32 +02:00
3c7e3e2589 6.0.0 2023-08-09 13:24:50 +02:00
205d27f9a0 BREAKING CHANGE(core): update 2023-08-09 13:24:49 +02:00
56ce78f794 5.0.5 2023-08-09 12:49:52 +02:00
9d33054f03 fix(core): update 2023-08-09 12:49:52 +02:00
072ca59ab0 5.0.4 2023-08-08 19:50:45 +02:00
59e3759a3a fix(core): update 2023-08-08 19:50:44 +02:00
bc95ba3f2d 5.0.3 2023-08-08 19:08:25 +02:00
1e6b9779b8 fix(core): update 2023-08-08 19:08:25 +02:00
3988887a37 switch to new org scheme 2023-07-11 00:07:40 +02:00
5a26ba7771 switch to new org scheme 2023-07-10 02:41:52 +02:00
f61c0da30a 5.0.2 2022-08-02 14:50:18 +02:00
3dfb07e875 fix(core): update 2022-08-02 14:50:18 +02:00
fde1e90440 5.0.1 2022-07-28 10:10:40 +02:00
f06c9f186f fix(core): update 2022-07-28 10:10:39 +02:00
e539489901 5.0.0 2022-07-28 10:09:35 +02:00
a1dcfba0a2 BREAKING CHANGE(core): switch to esm 2022-07-28 10:09:34 +02:00
cff79e56c0 4.0.11 2022-07-28 10:09:01 +02:00
44252ab0d3 fix(core): update 2022-07-28 10:09:01 +02:00
d4c3ec6c90 4.0.10 2020-06-08 19:03:18 +00:00
42aa1c2831 fix(core): update 2020-06-08 19:03:17 +00:00
adf602ab86 4.0.9 2020-06-08 18:58:56 +00:00
8bc8285430 fix(core): update 2020-06-08 18:58:55 +00:00
63d9434a39 4.0.8 2020-06-08 18:58:44 +00:00
32ddc9cfed fix(core): update 2020-06-08 18:58:43 +00:00
936a719682 4.0.7 2020-06-08 18:57:23 +00:00
56c4b43f3c fix(core): update 2020-06-08 18:57:22 +00:00
5ed11a280f 4.0.6 2019-10-01 12:32:40 +02:00
346809d5be fix(core): update 2019-10-01 12:32:39 +02:00
90add506e3 4.0.5 2019-09-13 11:20:48 +02:00
adea8d1d69 fix(core): update 2019-09-13 11:20:47 +02:00
1b2eb1d763 4.0.4 2019-08-29 14:29:16 +02:00
cbc974b3d1 fix(core): update 2019-08-29 14:29:16 +02:00
23 changed files with 10241 additions and 2119 deletions

View File

@ -0,0 +1,66 @@
name: Default (not tags)
on:
push:
tags-ignore:
- '**'
env:
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
jobs:
security:
runs-on: ubuntu-latest
continue-on-error: true
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Install pnpm and npmci
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
- name: Run npm prepare
run: npmci npm prepare
- name: Audit production dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --prod
continue-on-error: true
- name: Audit development dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --dev
continue-on-error: true
test:
if: ${{ always() }}
needs: security
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Test stable
run: |
npmci node install stable
npmci npm install
npmci npm test
- name: Test build
run: |
npmci node install stable
npmci npm install
npmci npm build

View File

@ -0,0 +1,124 @@
name: Default (tags)
on:
push:
tags:
- '*'
env:
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
jobs:
security:
runs-on: ubuntu-latest
continue-on-error: true
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Audit production dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --prod
continue-on-error: true
- name: Audit development dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --dev
continue-on-error: true
test:
if: ${{ always() }}
needs: security
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Test stable
run: |
npmci node install stable
npmci npm install
npmci npm test
- name: Test build
run: |
npmci node install stable
npmci npm install
npmci npm build
release:
needs: test
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Release
run: |
npmci node install stable
npmci npm publish
metadata:
needs: test
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
continue-on-error: true
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Code quality
run: |
npmci command npm install -g typescript
npmci npm install
- name: Trigger
run: npmci trigger
- name: Build docs and upload artifacts
run: |
npmci node install stable
npmci npm install
pnpm install -g @git.zone/tsdoc
npmci command tsdoc
continue-on-error: true

4
.gitignore vendored
View File

@ -15,8 +15,6 @@ node_modules/
# builds
dist/
dist_web/
dist_serve/
dist_ts_web/
dist_*/
# custom

View File

@ -1,119 +0,0 @@
# gitzone ci_default
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
cache:
paths:
- .npmci_cache/
key: "$CI_BUILD_STAGE"
stages:
- security
- test
- release
- metadata
# ====================
# security stage
# ====================
mirror:
stage: security
script:
- npmci git mirror
tags:
- docker
- notpriv
snyk:
stage: security
script:
- npmci npm prepare
- npmci command npm install -g snyk
- npmci command npm install --ignore-scripts
- npmci command snyk test
tags:
- docker
- notpriv
# ====================
# test stage
# ====================
testLTS:
stage: test
script:
- npmci npm prepare
- npmci node install lts
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
testBuild:
stage: test
script:
- npmci npm prepare
- npmci node install lts
- npmci npm install
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
- notpriv
release:
stage: release
script:
- npmci node install lts
- npmci npm publish
only:
- tags
tags:
- docker
- notpriv
# ====================
# metadata stage
# ====================
codequality:
stage: metadata
allow_failure: true
script:
- npmci command npm install -g tslint typescript
- npmci npm install
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
tags:
- docker
- priv
trigger:
stage: metadata
script:
- npmci trigger
only:
- tags
tags:
- docker
- notpriv
pages:
image: hosttoday/ht-docker-dbase:npmci
services:
- docker:18-dind
stage: metadata
script:
- npmci command npm install -g @gitzone/tsdoc
- npmci npm prepare
- npmci npm install
- npmci command tsdoc
tags:
- docker
- notpriv
only:
- tags
artifacts:
expire_in: 1 week
paths:
- public
allow_failure: true

View File

@ -1,4 +0,0 @@
node_modules/
coverage/
docs/

11
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,11 @@
{
"version": "0.2.0",
"configurations": [
{
"command": "npm test",
"name": "Run npm test",
"request": "launch",
"type": "node-terminal"
}
]
}

26
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,26 @@
{
"json.schemas": [
{
"fileMatch": ["/npmextra.json"],
"schema": {
"type": "object",
"properties": {
"npmci": {
"type": "object",
"description": "settings for npmci"
},
"gitzone": {
"type": "object",
"description": "settings for gitzone",
"properties": {
"projectType": {
"type": "string",
"enum": ["website", "element", "service", "npm", "wcc"]
}
}
}
}
}
}
]
}

View File

@ -1,43 +0,0 @@
# @pushrocks/qenv
easy promised environments
## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/qenv)
* [gitlab.com (source)](https://gitlab.com/pushrocks/qenv)
* [github.com (source mirror)](https://github.com/pushrocks/qenv)
* [docs (typedoc)](https://pushrocks.gitlab.io/qenv/)
## Status for master
[![build status](https://gitlab.com/pushrocks/qenv/badges/master/build.svg)](https://gitlab.com/pushrocks/qenv/commits/master)
[![coverage report](https://gitlab.com/pushrocks/qenv/badges/master/coverage.svg)](https://gitlab.com/pushrocks/qenv/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/qenv.svg)](https://www.npmjs.com/package/@pushrocks/qenv)
[![Known Vulnerabilities](https://snyk.io/test/npm/@pushrocks/qenv/badge.svg)](https://snyk.io/test/npm/@pushrocks/qenv)
[![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/)
## Usage
Use TypeScript for best in class instellisense.
qenv works with two files:
- **qenv.yml** - specifies which ENV vars are required.
- **env.yml** - specifies all env vars that are not already set in the current environment.
Now obviously you can set build specific env vars in many CI environments.
So there we do not need an **env.yml** since all ENV vars are in place
However when on another machine you can have a env.yml that will be added to the environment by qenv.
```javascript
import { Qenv } from 'qenv';
const myQenv = new Qenv('path/to/dir/where/qenv.yml/is/', 'path/to/dir/where/env.yml/is(');
```
For further information read the linked docs at the top of this readme.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://maintainedby.lossless.com)

66
changelog.md Normal file
View File

@ -0,0 +1,66 @@
# Changelog
## 2024-11-22 - 6.1.0 - feat(core)
Added new method getEnvVarOnDemandStrict to throw error for unset env vars
- Introduced getEnvVarOnDemandStrict method in Qenv class for strict retrieval of environment variables.
- Upgraded various @git.zone and @push.rocks dependencies for improved functionality and security.
## 2024-11-18 - 6.0.8 - fix(Qenv)
Fix environment file path initialization logic.
- Corrected the logic for setting the environment file paths to prevent overwriting each other.
## 2024-11-18 - 6.0.7 - fix(Qenv)
Fix file path initialization for environment variable files
- Corrected the logic for determining the absolute path for environment files
- Added missing initialization for env.yml file paths
## 2024-11-18 - 6.0.6 - fix(core)
Improve handling of env.json and env.yml file checks
- Check for existence of both env.json and env.yml files and prioritize env.json.
- Consolidate getFromEnvJsonFile and getFromEnvYamlFile methods into getFromEnvYamlOrJsonFile.
## 2024-05-29 to 2024-02-09 - 6.0.5 - update
Updates related to configuration files and data handling.
- Updated description
- Updated tsconfig
- Updated npmextra.json for githost
## 2023-08-09 - 6.0.0 to 6.0.4 - core
Various fixes within the core functionality.
- Fixes and improvements across multiple minor versions
## 2023-08-09 - 5.0.5 - core
Breaking change that impacts core functionality.
- Significant updates leading to breaking changes
## 2023-07-11 to 2022-07-28 - 5.0.2 - organization
Transition to a new organization scheme.
- Switched to new organizational scheme
## 2022-07-28 - 4.0.11 - core
Breaking change introducing ESM modules.
- Switch to ECMAScript modules
## 2019-01-15 - 3.1.1 - environment
Breaking change in environment handling.
- Treat environment variables as immutable
## 2019-01-14 - 3.0.7 - docker
New feature for Docker secret management.
- Allow Docker secret.json to be named flexibly
## 2018-08-13 - 1.1.7 - scope
Scope update for package management.
- Change scope to @pushrocks/

View File

@ -4,13 +4,26 @@
"npmAccessLevel": "public"
},
"gitzone": {
"projectType": "npm",
"module": {
"githost": "gitlab.com",
"gitscope": "pushrocks",
"githost": "code.foss.global",
"gitscope": "push.rocks",
"gitrepo": "qenv",
"shortDescription": "easy promised environments",
"npmPackagename": "@pushrocks/qenv",
"license": "MIT"
"description": "A module for easily handling environment variables in Node.js projects with support for .yml and .json configuration.",
"npmPackagename": "@push.rocks/qenv",
"license": "MIT",
"keywords": [
"environment variables",
"configuration management",
"Node.js",
"TypeScript",
"Docker secrets",
"CI/CD",
"testing"
]
}
},
"tsdoc": {
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy 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.\n"
}
}

1739
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,51 +1,62 @@
{
"name": "@pushrocks/qenv",
"version": "4.0.3",
"name": "@push.rocks/qenv",
"version": "6.1.0",
"private": false,
"description": "easy promised environments",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"description": "A module for easily handling environment variables in Node.js projects with support for .yml and .json configuration.",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"type": "module",
"scripts": {
"test": "(tstest test/)",
"build": "(tsbuild)"
"build": "(tsbuild --web --allowimplicitany)",
"buildDocs": "tsdoc"
},
"repository": {
"type": "git",
"url": "git+ssh://git@gitlab.com/pushrocks/qenv.git"
"url": "https://code.foss.global/push.rocks/qenv.git"
},
"keywords": [
"environment",
"git",
"ci"
"environment variables",
"configuration management",
"Node.js",
"TypeScript",
"Docker secrets",
"CI/CD",
"testing"
],
"author": "Lossless GmbH",
"license": "MIT",
"bugs": {
"url": "https://gitlab.com/pushrocks/qenv/issues"
},
"homepage": "https://gitlab.com/pushrocks/qenv#README",
"homepage": "https://code.foss.global/push.rocks/qenv",
"devDependencies": {
"@gitzone/tsbuild": "^2.1.11",
"@gitzone/tsrun": "^1.2.8",
"@gitzone/tstest": "^1.0.24",
"@pushrocks/tapbundle": "^3.0.11",
"@types/node": "^12.6.9",
"tslint": "^5.18.0",
"tslint-config-prettier": "^1.18.0"
"@git.zone/tsbuild": "^2.2.0",
"@git.zone/tsrun": "^1.3.3",
"@git.zone/tstest": "^1.0.90",
"@push.rocks/tapbundle": "^5.5.0",
"@types/node": "^22.9.1"
},
"dependencies": {
"@pushrocks/smartfile": "^7.0.4",
"@pushrocks/smartlog": "^2.0.19"
"@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"
},
"files": [
"ts/*",
"ts_web/*",
"dist/*",
"dist_web/*",
"dist_ts_web/*",
"assets/*",
"ts/**/*",
"ts_web/**/*",
"dist/**/*",
"dist_*/**/*",
"dist_ts/**/*",
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
"npmextra.json",
"readme.md"
],
"browserslist": [
"last 1 chrome versions"
]
}

9493
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

1
readme.hints.md Normal file
View File

@ -0,0 +1 @@

125
readme.md Normal file
View File

@ -0,0 +1,125 @@
# @push.rocks/qenv
easy promised environments
## 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:
```bash
npm install @push.rocks/qenv --save
```
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`:
```typescript
import { Qenv } from '@push.rocks/qenv';
```
#### 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.
##### qenv.yml
This file specifies the environment variables that are required by your application. An example `qenv.yml` might look like this:
```yaml
required:
- DB_HOST
- DB_USER
- DB_PASS
```
##### env.yml
This file contains the actual values for the environment variables in a development or testing environment. An example `env.yml` could be:
```yaml
DB_HOST: localhost
DB_USER: user
DB_PASS: pass
```
#### 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');
```
If the `env.yml` file is in the same directory as `qenv.yml`, you can omit the second argument:
```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';
};
// Use the function with getEnvVarOnDemand
(async () => {
const dbHost = await myQenv.getEnvVarOnDemand(fetchDbHost);
console.log(dbHost); // 'dynamic.host'
})();
```
#### 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.
### 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.
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.
## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
### Company Information
Task Venture Capital GmbH
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.

View File

@ -1,29 +1,35 @@
import * as path from 'path';
import { tap, expect } from '@pushrocks/tapbundle';
import * as qenv from '../ts/index';
import { tap, expect } from '@push.rocks/tapbundle';
import * as qenv from '../ts/index.js';
import * as smartpath from '@push.rocks/smartpath';
export { smartpath };
const testDir = smartpath.get.dirnameFromImportMetaUrl(import.meta.url);
process.env['key1'] = 'original';
let testQenv: qenv.Qenv;
tap.test('should create a new class', async () => {
testQenv = new qenv.Qenv(path.join(__dirname, 'assets'), path.join(__dirname, 'assets'), false);
expect(testQenv).to.be.instanceof(qenv.Qenv);
testQenv = new qenv.Qenv(path.join(testDir, 'assets'), path.join(testDir, 'assets'), false);
expect(testQenv).toBeInstanceOf(qenv.Qenv);
});
tap.test('key1 should be not be overwritten since it is already present', async () => {
expect(testQenv.getEnvVarRequired('key1')).to.equal('original');
expect(testQenv.getEnvVarOnDemand('key1')).to.equal('original');
expect(await testQenv.getEnvVarOnDemand('key1')).toEqual('original');
expect(await testQenv.getEnvVarOnDemand('key1')).toEqual('original');
});
tap.test('key2 should be read from Yml', async () => {
expect(testQenv.getEnvVarRequired('key2')).to.equal('fromJson');
expect(testQenv.getEnvVarOnDemand('key2')).to.equal('fromJson');
expect(await testQenv.getEnvVarOnDemand('key2')).toEqual('fromJson');
expect(await testQenv.getEnvVarOnDemand('key2')).toEqual('fromJson');
});
tap.test('keyValueObjectArray should hold all retrieved values', async () => {
expect(testQenv.keyValueObject.key1).to.equal('original');
expect(testQenv.keyValueObject.key2).to.equal('fromJson');
expect(await testQenv.keyValueObject.key1).toEqual('original');
expect(await testQenv.keyValueObject.key2).toEqual('fromJson');
});
tap.start();

8
ts/00_commitinfo_data.ts Normal file
View File

@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @push.rocks/commitinfo
*/
export const commitinfo = {
name: '@push.rocks/qenv',
version: '6.1.0',
description: 'A module for easily handling environment variables in Node.js projects with support for .yml and .json configuration.'
}

View File

@ -1 +1 @@
export * from './qenv.classes.qenv';
export * from './qenv.classes.qenv.js';

View File

@ -0,0 +1,30 @@
import * as plugins from './qenv.plugins.js';
export class CloudlyAdapter {
public configVaultUrl: string;
constructor(configVaultUrl?: string) {
this.configVaultUrl = configVaultUrl;
}
public async getConfigBundle(): Promise<plugins.configvaultInterfaces.data.IConfigBundle> {
if (this.configVaultUrl) {
console.log(`ConfigVault specified through constructor`)
} else if (process.env['CONFIGVAULT_URL']) {
this.configVaultUrl = process.env['CONFIGVAULT_URL'];
} else {
return null;
}
const parsedUrl = new URL(this.configVaultUrl);
const tr =
new plugins.typedrequest.TypedRequest<plugins.configvaultInterfaces.requests.IReq_GetEnvBundle>(
`${parsedUrl.host}/typedrequest`,
'getEnvBundle'
);
const response = await tr.fire({
authorization: parsedUrl.pathname.replace('/', ''),
})
}
}

View File

@ -1,190 +1,230 @@
import * as plugins from './qenv.plugins';
plugins.smartlog.defaultLogger.enableConsole();
import { CloudlyAdapter } from './qenv.classes.configvaultadapter.js';
import * as plugins from './qenv.plugins.js';
export type TEnvVarRef = string | (() => Promise<string>);
/**
* class Qenv
* allows to make assertions about the environments while being more flexibel in how to meet them
*/
export class Qenv {
public requiredEnvVars: string[] = [];
public availableEnvVars: string[] = [];
public missingEnvVars: string[] = [];
public keyValueObject: { [key: string]: any } = {};
public logger: plugins.smartlog.Smartlog;
public logger = new plugins.smartlog.ConsoleLog();
public cloudlyAdapter: CloudlyAdapter;
// filePaths
public qenvFilePathAbsolute: string;
public envFilePathAbsolute: string;
constructor(
qenvFileBasePathArg = process.cwd(),
envFileBasePathArg,
failOnMissing = true,
loggerArg: plugins.smartlog.Smartlog = plugins.smartlog.defaultLogger
qenvFileBasePathArg: string = process.cwd(),
envFileBasePathArg?: string,
failOnMissing: boolean = true
) {
this.logger = loggerArg;
this.cloudlyAdapter = new CloudlyAdapter();
this.initializeFilePaths(qenvFileBasePathArg, envFileBasePathArg);
this.loadRequiredEnvVars();
this.loadAvailableEnvVars();
this.checkForMissingEnvVars(failOnMissing);
}
// lets make sure paths are absolute
private initializeFilePaths(qenvFileBasePathArg: string, envFileBasePathArg: string) {
this.qenvFilePathAbsolute = plugins.path.join(
plugins.path.resolve(qenvFileBasePathArg),
'qenv.yml'
);
this.envFilePathAbsolute = plugins.path.join(
plugins.path.resolve(envFileBasePathArg),
'env.json'
);
this.getRequiredEnvVars();
this.getAvailableEnvVars();
this.missingEnvVars = this.getMissingEnvVars();
// handle missing variables
if (this.missingEnvVars.length > 0) {
console.info('Required Env Vars are:');
console.log(this.requiredEnvVars);
console.error('However some Env variables could not be resolved:');
console.log(this.missingEnvVars);
if (failOnMissing) {
console.error('Exiting!');
process.exit(1);
} else {
console.log('qenv is not set to fail on missing environment variables');
if (envFileBasePathArg) {
const envFileBasePath = plugins.path.resolve(envFileBasePathArg);
const envFileJsonPath = plugins.path.join(envFileBasePath, 'env.json');
const envFileYmlPath = plugins.path.join(envFileBasePath, 'env.yml');
const envFileYamlPath = plugins.path.join(envFileBasePath, 'env.yaml');
const envFileJsonExists = plugins.smartfile.fs.fileExistsSync(envFileJsonPath);
const envFileYmlExists = plugins.smartfile.fs.fileExistsSync(envFileYmlPath);
const envFileYamlExists = plugins.smartfile.fs.fileExistsSync(envFileYamlPath);
if (envFileJsonExists && (envFileYmlExists || envFileYamlExists)) {
this.logger.log('warn', 'Both env.json and env.yml files exist! Using env.json');
this.envFilePathAbsolute = envFileJsonPath;
} else if (envFileJsonExists) {
this.envFilePathAbsolute = envFileJsonPath;
} else if (envFileYmlExists) {
this.envFilePathAbsolute = envFileYmlPath;
} else if (envFileYamlExists) {
this.envFilePathAbsolute = envFileYamlPath;
}
}
}
/**
* only gets an environment variable if it is required within a read qenv.yml file
* @param envVarName
*/
public getEnvVarRequired(envVarName): string {
return this.keyValueObject[envVarName];
private loadRequiredEnvVars() {
if (plugins.smartfile.fs.fileExistsSync(this.qenvFilePathAbsolute)) {
const qenvFile = plugins.smartfile.fs.toObjectSync(this.qenvFilePathAbsolute);
if (qenvFile?.required && Array.isArray(qenvFile.required)) {
this.requiredEnvVars.push(...qenvFile.required);
} else {
this.logger.log('warn', 'qenv.yml does not contain a "required" Array!');
}
}
}
private loadAvailableEnvVars() {
for (const envVar of this.requiredEnvVars) {
const value = this.getEnvVarOnDemand(envVar);
if (value) {
this.availableEnvVars.push(envVar);
this.keyValueObject[envVar] = value;
}
}
}
private checkForMissingEnvVars(failOnMissing: boolean) {
this.missingEnvVars = this.requiredEnvVars.filter(
(envVar) => !this.availableEnvVars.includes(envVar)
);
if (this.missingEnvVars.length > 0) {
console.info('Required Env Vars are:', this.requiredEnvVars);
console.error('Missing Env Vars:', this.missingEnvVars);
if (failOnMissing) {
this.logger.log('error', 'Exiting due to missing env vars!');
process.exit(1);
} else {
this.logger.log('warn', 'qenv is not set to fail on missing environment variables');
}
}
}
public async getEnvVarOnDemand(
envVarNameOrNames: TEnvVarRef | TEnvVarRef[]
): Promise<string | undefined> {
if (Array.isArray(envVarNameOrNames)) {
for (const envVarName of envVarNameOrNames) {
const value = await this.tryGetEnvVar(envVarName);
if (value) {
return value;
}
}
return undefined;
} else {
return await this.tryGetEnvVar(envVarNameOrNames);
}
}
/**
* tries to get any env var even if it is not required
* @param requiredEnvVar
* Like getEnvVarOnDemand, but throws an error if the env var is not set.
* @param envVarNameOrNames
* @returns
*/
public getEnvVarOnDemand(requiredEnvVar: string): string {
// lets determine the actual env yml
let envYml;
public async getEnvVarOnDemandStrict(
envVarNameOrNames: TEnvVarRef | TEnvVarRef[]
): Promise<string> {
const value = await this.getEnvVarOnDemand(envVarNameOrNames);
if (!value) {
throw new Error(`Env var ${envVarNameOrNames} is not set!`);
}
return value;
}
public getEnvVarOnDemandSync(envVarNameOrNames: string | string[]): string | undefined {
console.warn('requesting env var sync leaves out potentially important async env sources.');
if (Array.isArray(envVarNameOrNames)) {
for (const envVarName of envVarNameOrNames) {
const value = this.tryGetEnvVarSync(envVarName);
if (value) {
return value;
}
}
return undefined;
} else {
return this.tryGetEnvVarSync(envVarNameOrNames);
}
}
public async getEnvVarOnDemandAsObject(envVarNameOrNames: string | string[]): Promise<any> {
const rawValue = await this.getEnvVarOnDemand(envVarNameOrNames);
if (rawValue && rawValue.startsWith('base64Object:')) {
const base64Part = rawValue.split('base64Object:')[1];
return this.decodeBase64(base64Part);
}
return rawValue;
}
private async tryGetEnvVar(envVarRefArg: TEnvVarRef): Promise<string | undefined> {
if (typeof envVarRefArg === 'function') {
return await envVarRefArg();
}
return (
this.getFromEnvironmentVariable(envVarRefArg) ||
this.getFromEnvYamlOrJsonFile(envVarRefArg) ||
this.getFromDockerSecret(envVarRefArg) ||
this.getFromDockerSecretJson(envVarRefArg)
);
}
private tryGetEnvVarSync(envVarName: string): string | undefined {
return (
this.getFromEnvironmentVariable(envVarName) ||
this.getFromEnvYamlOrJsonFile(envVarName) ||
this.getFromDockerSecret(envVarName) ||
this.getFromDockerSecretJson(envVarName)
);
}
private getFromEnvironmentVariable(envVarName: string): string | undefined {
return process.env[envVarName];
}
private getFromEnvYamlOrJsonFile(envVarName: string): string | undefined {
if (!plugins.smartfile.fs.fileExistsSync(this.envFilePathAbsolute)) {
return undefined;
}
try {
envYml = plugins.smartfile.fs.toObjectSync(this.envFilePathAbsolute);
} catch (err) {
envYml = {};
const envJson = plugins.smartfile.fs.toObjectSync(this.envFilePathAbsolute);
const value = envJson[envVarName];
if (typeof value === 'object') {
return 'base64Object:' + this.encodeBase64(value);
}
return value;
} catch (error) {
return undefined;
}
let envVar: string;
let envFileVar: string;
let dockerSecret: string;
let dockerSecretJson: string;
}
// env var check
if (process.env[requiredEnvVar]) {
this.availableEnvVars.push(requiredEnvVar);
envVar = process.env[requiredEnvVar];
private getFromDockerSecret(envVarName: string): string | undefined {
const secretPath = `/run/secrets/${envVarName}`;
if (plugins.smartfile.fs.fileExistsSync(secretPath)) {
return plugins.smartfile.fs.toStringSync(secretPath);
}
return undefined;
}
// env file check
if (envYml.hasOwnProperty(requiredEnvVar)) {
envFileVar = envYml[requiredEnvVar];
this.availableEnvVars.push(requiredEnvVar);
}
// docker secret check
if (
plugins.smartfile.fs.isDirectory('/run') &&
plugins.smartfile.fs.isDirectory('/run/secrets') &&
plugins.smartfile.fs.fileExistsSync(`/run/secrets/${requiredEnvVar}`)
) {
dockerSecret = plugins.smartfile.fs.toStringSync(`/run/secrets/${requiredEnvVar}`);
}
// docker secret.json
if (
plugins.smartfile.fs.isDirectory('/run') &&
plugins.smartfile.fs.isDirectory('/run/secrets')
) {
private getFromDockerSecretJson(envVarName: string): string | undefined {
if (plugins.smartfile.fs.isDirectory('/run/secrets')) {
const availableSecrets = plugins.smartfile.fs.listAllItemsSync('/run/secrets');
for (const secret of availableSecrets) {
if (secret.includes('secret.json') && !dockerSecret) {
if (secret.includes('secret.json')) {
const secretObject = plugins.smartfile.fs.toObjectSync(`/run/secrets/${secret}`);
dockerSecret = secretObject[requiredEnvVar];
const value = secretObject[envVarName];
if (typeof value === 'object') {
return 'base64Object:' + this.encodeBase64(value);
}
return value;
}
}
}
// warn if there is more than one candidate
let candidatesCounter = 0;
[envVar, envFileVar, dockerSecret, dockerSecretJson].forEach(candidate => {
if (candidate) {
candidatesCounter++;
}
});
if (candidatesCounter > 1) {
this.logger.log(
'warn',
`found multiple candidates for ${requiredEnvVar} Choosing in the order of envVar, envFileVar, dockerSecret, dockerSecretJson`
);
}
let chosenVar: string = null;
if (envVar) {
this.logger.log('ok', `found ${requiredEnvVar} as environment variable`);
chosenVar = envVar;
} else if (envFileVar) {
this.logger.log('ok', `found ${requiredEnvVar} as env.json variable`);
chosenVar = envFileVar;
} else if (dockerSecret) {
this.logger.log('ok', `found ${requiredEnvVar} as docker secret`);
chosenVar = dockerSecret;
} else if (dockerSecretJson) {
this.logger.log('ok', `found ${requiredEnvVar} as docker secret.json`);
chosenVar = dockerSecretJson;
}
return chosenVar;
return undefined;
}
/**
* gets the required env values
*/
private getRequiredEnvVars = () => {
let qenvFile: any = {};
if (plugins.smartfile.fs.fileExistsSync(this.qenvFilePathAbsolute)) {
qenvFile = plugins.smartfile.fs.toObjectSync(this.qenvFilePathAbsolute);
}
if (!qenvFile || !qenvFile.required || !Array.isArray(qenvFile.required)) {
this.logger.log('warn', `env File does not contain a 'required' Array!`);
} else {
for (const keyArg of Object.keys(qenvFile.required)) {
this.requiredEnvVars.push(qenvFile.required[keyArg]);
}
}
};
private encodeBase64(data: any): string {
const jsonString = JSON.stringify(data);
return Buffer.from(jsonString).toString('base64');
}
/**
* gets the available env vars
*/
private getAvailableEnvVars = () => {
for (const requiredEnvVar of this.requiredEnvVars) {
const chosenVar = this.getEnvVarOnDemand(requiredEnvVar);
if (chosenVar) {
this.availableEnvVars.push(requiredEnvVar);
this.keyValueObject[requiredEnvVar] = chosenVar;
}
}
};
/**
* gets missing env vars
*/
private getMissingEnvVars = (): string[] => {
const missingEnvVars: string[] = [];
for (const envVar of this.requiredEnvVars) {
if (!this.availableEnvVars.includes(envVar)) {
missingEnvVars.push(envVar);
}
}
return missingEnvVars;
};
private decodeBase64(encodedString: string): any {
const decodedString = Buffer.from(encodedString, 'base64').toString('utf-8');
return JSON.parse(decodedString);
}
}

View File

@ -3,8 +3,20 @@ import * as path from 'path';
export { path };
// @api.global scope
import * as typedrequest from '@api.global/typedrequest';
export {
typedrequest,
}
// @pushrocks scope
import * as smartfile from '@pushrocks/smartfile';
import * as smartlog from '@pushrocks/smartlog';
import * as smartfile from '@push.rocks/smartfile';
import * as smartlog from '@push.rocks/smartlog';
export { smartfile, smartlog };
// @configvault.io scope
import * as configvaultInterfaces from '@configvault.io/interfaces';
export { configvaultInterfaces };

14
tsconfig.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"verbatimModuleSyntax": true
},
"exclude": [
"dist_*/**/*.d.ts"
]
}

View File

@ -1,17 +0,0 @@
{
"extends": ["tslint:latest", "tslint-config-prettier"],
"rules": {
"semicolon": [true, "always"],
"no-console": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"member-ordering": {
"options":{
"order": [
"static-method"
]
}
}
},
"defaultSeverity": "warning"
}