Compare commits
40 Commits
Author | SHA1 | Date | |
---|---|---|---|
765011ad2a | |||
d807cc6de2 | |||
53721a41c2 | |||
c9f79e6ea4 | |||
3c7e3e2589 | |||
205d27f9a0 | |||
56ce78f794 | |||
9d33054f03 | |||
072ca59ab0 | |||
59e3759a3a | |||
bc95ba3f2d | |||
1e6b9779b8 | |||
3988887a37 | |||
5a26ba7771 | |||
f61c0da30a | |||
3dfb07e875 | |||
fde1e90440 | |||
f06c9f186f | |||
e539489901 | |||
a1dcfba0a2 | |||
cff79e56c0 | |||
44252ab0d3 | |||
d4c3ec6c90 | |||
42aa1c2831 | |||
adf602ab86 | |||
8bc8285430 | |||
63d9434a39 | |||
32ddc9cfed | |||
936a719682 | |||
56c4b43f3c | |||
5ed11a280f | |||
346809d5be | |||
90add506e3 | |||
adea8d1d69 | |||
1b2eb1d763 | |||
cbc974b3d1 | |||
76e72e2a28 | |||
697eb83390 | |||
00ec15ccf7 | |||
b54a5e2fef |
66
.gitea/workflows/default_nottags.yaml
Normal file
66
.gitea/workflows/default_nottags.yaml
Normal 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
|
124
.gitea/workflows/default_tags.yaml
Normal file
124
.gitea/workflows/default_tags.yaml
Normal 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 @gitzone/tsdoc
|
||||
npmci command tsdoc
|
||||
continue-on-error: true
|
4
.gitignore
vendored
4
.gitignore
vendored
@ -15,8 +15,6 @@ node_modules/
|
||||
|
||||
# builds
|
||||
dist/
|
||||
dist_web/
|
||||
dist_serve/
|
||||
dist_ts_web/
|
||||
dist_*/
|
||||
|
||||
# custom
|
119
.gitlab-ci.yml
119
.gitlab-ci.yml
@ -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
|
@ -1,4 +0,0 @@
|
||||
node_modules/
|
||||
coverage/
|
||||
docs/
|
||||
|
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal 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
26
.vscode/settings.json
vendored
Normal 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"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
43
README.md
43
README.md
@ -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
|
||||
[](https://gitlab.com/pushrocks/qenv/commits/master)
|
||||
[](https://gitlab.com/pushrocks/qenv/commits/master)
|
||||
[](https://www.npmjs.com/package/@pushrocks/qenv)
|
||||
[](https://snyk.io/test/npm/@pushrocks/qenv)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](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)
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
@ -4,12 +4,13 @@
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "pushrocks",
|
||||
"gitscope": "push.rocks",
|
||||
"gitrepo": "qenv",
|
||||
"shortDescription": "easy promised environments",
|
||||
"npmPackagename": "@pushrocks/qenv",
|
||||
"description": "easy promised environments",
|
||||
"npmPackagename": "@push.rocks/qenv",
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
|
1739
package-lock.json
generated
1739
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
47
package.json
47
package.json
@ -1,13 +1,15 @@
|
||||
{
|
||||
"name": "@pushrocks/qenv",
|
||||
"version": "4.0.1",
|
||||
"name": "@push.rocks/qenv",
|
||||
"version": "6.0.2",
|
||||
"private": false,
|
||||
"description": "easy promised environments",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"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",
|
||||
@ -25,27 +27,32 @@
|
||||
},
|
||||
"homepage": "https://gitlab.com/pushrocks/qenv#README",
|
||||
"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"
|
||||
"@gitzone/tsbuild": "^2.1.66",
|
||||
"@gitzone/tsrun": "^1.2.44",
|
||||
"@gitzone/tstest": "^1.0.77",
|
||||
"@push.rocks/tapbundle": "^5.0.12",
|
||||
"@types/node": "^20.4.6"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartfile": "^7.0.4",
|
||||
"@pushrocks/smartlog": "^2.0.19"
|
||||
"@api.global/typedrequest": "^3.0.1",
|
||||
"@configvault.io/interfaces": "^1.0.2",
|
||||
"@push.rocks/smartfile": "^10.0.28",
|
||||
"@push.rocks/smartlog": "^3.0.3",
|
||||
"@push.rocks/smartpath": "^5.0.11"
|
||||
},
|
||||
"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"
|
||||
]
|
||||
}
|
||||
|
4627
pnpm-lock.yaml
generated
Normal file
4627
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
52
readme.md
Normal file
52
readme.md
Normal file
@ -0,0 +1,52 @@
|
||||
# @push.rocks/qenv
|
||||
easy promised environments
|
||||
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@push.rocks/qenv)
|
||||
* [gitlab.com (source)](https://gitlab.com/push.rocks/qenv)
|
||||
* [github.com (source mirror)](https://github.com/push.rocks/qenv)
|
||||
* [docs (typedoc)](https://push.rocks.gitlab.io/qenv/)
|
||||
|
||||
## Status for master
|
||||
|
||||
Status Category | Status Badge
|
||||
-- | --
|
||||
GitLab Pipelines | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
|
||||
## 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(');
|
||||
```
|
||||
|
||||
## Contribution
|
||||
|
||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
|
||||
## Legal
|
||||
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
4
test/assets/env.json
Normal file
4
test/assets/env.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"key1": "fromJson",
|
||||
"key2" :"fromJson"
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
key1: fromYml
|
||||
key2: fromYml
|
26
test/test.ts
26
test/test.ts
@ -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('fromYml');
|
||||
expect(testQenv.getEnvVarOnDemand('key2')).to.equal('fromYml');
|
||||
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('fromYml');
|
||||
expect(await testQenv.keyValueObject.key1).toEqual('original');
|
||||
expect(await testQenv.keyValueObject.key2).toEqual('fromJson');
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/qenv',
|
||||
version: '6.0.2',
|
||||
description: 'easy promised environments'
|
||||
}
|
@ -1 +1 @@
|
||||
export * from './qenv.classes.qenv';
|
||||
export * from './qenv.classes.qenv.js';
|
||||
|
30
ts/qenv.classes.configvaultadapter.ts
Normal file
30
ts/qenv.classes.configvaultadapter.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import * as plugins from './qenv.plugins.js';
|
||||
|
||||
export class ConfigVaultAdapter {
|
||||
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_GetConfigBundle>(
|
||||
`${parsedUrl.host}/typedrequest`,
|
||||
'getConfigBundle'
|
||||
);
|
||||
const response = await tr.fire({
|
||||
authorization: parsedUrl.pathname.replace('/', ''),
|
||||
})
|
||||
}
|
||||
}
|
@ -1,188 +1,187 @@
|
||||
import * as plugins from './qenv.plugins';
|
||||
plugins.smartlog.defaultLogger.enableConsole();
|
||||
import { ConfigVaultAdapter } from './qenv.classes.configvaultadapter.js';
|
||||
import * as plugins from './qenv.plugins.js';
|
||||
|
||||
/**
|
||||
* 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 configVaultAdapter: ConfigVaultAdapter;
|
||||
|
||||
// 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.configVaultAdapter = new ConfigVaultAdapter();
|
||||
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.yml'
|
||||
'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);
|
||||
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!');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 loadAvailableEnvVars() {
|
||||
for (const envVar of this.requiredEnvVars) {
|
||||
const value = this.getEnvVarOnDemand(envVar);
|
||||
if (value) {
|
||||
this.availableEnvVars.push(envVar);
|
||||
this.keyValueObject[envVar] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* tries to get any env var even if it is not required
|
||||
* @param requiredEnvVar
|
||||
*/
|
||||
public getEnvVarOnDemand(requiredEnvVar: string): string {
|
||||
// lets determine the actual env yml
|
||||
let envYml;
|
||||
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: string | string[]
|
||||
): 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);
|
||||
}
|
||||
}
|
||||
|
||||
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(envVarName: string): Promise<string | undefined> {
|
||||
return (
|
||||
this.getFromEnvironmentVariable(envVarName) ||
|
||||
this.getFromEnvJsonFile(envVarName) ||
|
||||
this.getFromDockerSecret(envVarName) ||
|
||||
this.getFromDockerSecretJson(envVarName)
|
||||
);
|
||||
}
|
||||
|
||||
private tryGetEnvVarSync(envVarName: string): string | undefined {
|
||||
return (
|
||||
this.getFromEnvironmentVariable(envVarName) ||
|
||||
this.getFromEnvJsonFile(envVarName) ||
|
||||
this.getFromDockerSecret(envVarName) ||
|
||||
this.getFromDockerSecretJson(envVarName)
|
||||
);
|
||||
}
|
||||
|
||||
private getFromEnvironmentVariable(envVarName: string): string | undefined {
|
||||
return process.env[envVarName];
|
||||
}
|
||||
|
||||
private getFromEnvJsonFile(envVarName: string): string | 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.yml 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);
|
||||
}
|
||||
}
|
||||
|
@ -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 };
|
||||
|
11
tsconfig.json
Normal file
11
tsconfig.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"experimentalDecorators": true,
|
||||
"useDefineForClassFields": false,
|
||||
"target": "ES2022",
|
||||
"module": "ES2022",
|
||||
"moduleResolution": "nodenext",
|
||||
"esModuleInterop": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
}
|
||||
}
|
17
tslint.json
17
tslint.json
@ -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"
|
||||
}
|
Reference in New Issue
Block a user