Compare commits
47 Commits
Author | SHA1 | Date | |
---|---|---|---|
e539489901 | |||
a1dcfba0a2 | |||
cff79e56c0 | |||
44252ab0d3 | |||
d4c3ec6c90 | |||
42aa1c2831 | |||
adf602ab86 | |||
8bc8285430 | |||
63d9434a39 | |||
32ddc9cfed | |||
936a719682 | |||
56c4b43f3c | |||
5ed11a280f | |||
346809d5be | |||
90add506e3 | |||
adea8d1d69 | |||
1b2eb1d763 | |||
cbc974b3d1 | |||
76e72e2a28 | |||
697eb83390 | |||
00ec15ccf7 | |||
b54a5e2fef | |||
a1aa1543a2 | |||
0e48cb8f89 | |||
d76d1a263b | |||
5f6f434998 | |||
8e7909e367 | |||
ff9302f745 | |||
3577fa22e1 | |||
f5275adfbd | |||
31bf1b27a4 | |||
a77def5844 | |||
2357699f3e | |||
01f5784488 | |||
80f35c39aa | |||
87f55773bd | |||
1a25341232 | |||
ccd41d86bf | |||
0a3a7e480e | |||
33a91c6ae7 | |||
0cfbdf2c9e | |||
f7362e5444 | |||
7844fdb954 | |||
81d1a4f15e | |||
da02923679 | |||
d7927ead86 | |||
9214ac49d5 |
20
.gitignore
vendored
20
.gitignore
vendored
@ -1,4 +1,20 @@
|
|||||||
node_modules/
|
.nogit/
|
||||||
|
|
||||||
|
# artifacts
|
||||||
coverage/
|
coverage/
|
||||||
pages/
|
|
||||||
public/
|
public/
|
||||||
|
pages/
|
||||||
|
|
||||||
|
# installs
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# caches
|
||||||
|
.yarn/
|
||||||
|
.cache/
|
||||||
|
.rpt2_cache
|
||||||
|
|
||||||
|
# builds
|
||||||
|
dist/
|
||||||
|
dist_*/
|
||||||
|
|
||||||
|
# custom
|
137
.gitlab-ci.yml
137
.gitlab-ci.yml
@ -1,16 +1,16 @@
|
|||||||
# gitzone standard
|
# gitzone ci_default
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
paths:
|
paths:
|
||||||
- .npmci_cache/
|
- .npmci_cache/
|
||||||
key: "$CI_BUILD_STAGE"
|
key: '$CI_BUILD_STAGE'
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- security
|
- security
|
||||||
- test
|
- test
|
||||||
- release
|
- release
|
||||||
- metadata
|
- metadata
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# security stage
|
# security stage
|
||||||
@ -18,109 +18,104 @@ stages:
|
|||||||
mirror:
|
mirror:
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci git mirror
|
- npmci git mirror
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
snyk:
|
audit:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g snyk
|
- npmci npm prepare
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm install --ignore-scripts
|
||||||
- npmci command snyk test
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command npm audit --audit-level=high
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
# ====================
|
# ====================
|
||||||
testLEGACY:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci node install legacy
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
allow_failure: true
|
|
||||||
|
|
||||||
testLTS:
|
testStable:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci node install lts
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci node install stable
|
||||||
- npmci npm test
|
- npmci npm install
|
||||||
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- priv
|
||||||
testSTABLE:
|
|
||||||
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci node install stable
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci node install stable
|
||||||
- npmci npm test
|
- npmci npm install
|
||||||
|
- npmci command npm run build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
script:
|
script:
|
||||||
- npmci node install stable
|
- npmci node install stable
|
||||||
- npmci npm publish
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# metadata stage
|
# metadata stage
|
||||||
# ====================
|
# ====================
|
||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
image: docker:stable
|
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
services:
|
|
||||||
- docker:stable-dind
|
|
||||||
script:
|
script:
|
||||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
- npmci command npm install -g tslint typescript
|
||||||
- docker run
|
- npmci npm prepare
|
||||||
--env SOURCE_CODE="$PWD"
|
- npmci npm install
|
||||||
--volume "$PWD":/code
|
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
|
||||||
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
|
||||||
artifacts:
|
|
||||||
paths: [codeclimate.json]
|
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- priv
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
trigger:
|
trigger:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci trigger
|
- npmci trigger
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- lossless
|
||||||
- notpriv
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-node:npmci
|
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g typedoc typescript
|
- npmci node install lts
|
||||||
|
- npmci command npm install -g @gitzone/tsdoc
|
||||||
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
- npmci command tsdoc
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
only:
|
only:
|
||||||
@ -128,15 +123,5 @@ pages:
|
|||||||
artifacts:
|
artifacts:
|
||||||
expire_in: 1 week
|
expire_in: 1 week
|
||||||
paths:
|
paths:
|
||||||
- public
|
- public
|
||||||
allow_failure: true
|
|
||||||
|
|
||||||
windowsCompatibility:
|
|
||||||
image: stefanscherer/node-windows:10-build-tools
|
|
||||||
stage: metadata
|
|
||||||
script:
|
|
||||||
- npm install & npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- windows
|
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
29
.vscode/launch.json
vendored
Normal file
29
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "current file",
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [
|
||||||
|
"${relativeFile}"
|
||||||
|
],
|
||||||
|
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||||
|
"cwd": "${workspaceRoot}",
|
||||||
|
"protocol": "inspector",
|
||||||
|
"internalConsoleOptions": "openOnSessionStart"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "test.ts",
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [
|
||||||
|
"test/test.ts"
|
||||||
|
],
|
||||||
|
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||||
|
"cwd": "${workspaceRoot}",
|
||||||
|
"protocol": "inspector",
|
||||||
|
"internalConsoleOptions": "openOnSessionStart"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
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"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
46
README.md
46
README.md
@ -1,46 +0,0 @@
|
|||||||
# qenv
|
|
||||||
|
|
||||||
easy promised environments
|
|
||||||
|
|
||||||
## Availabililty
|
|
||||||
|
|
||||||
[](https://www.npmjs.com/package/qenv)
|
|
||||||
[](https://GitLab.com/pushrocks/qenv)
|
|
||||||
[](https://github.com/pushrocks/qenv)
|
|
||||||
[](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/qenv)
|
|
||||||
[](https://david-dm.org/pushrocks/qenv)
|
|
||||||
[](https://www.bithound.io/github/pushrocks/qenv/master/dependencies/npm)
|
|
||||||
[](https://www.bithound.io/github/pushrocks/qenv)
|
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
||||||
[](http://standardjs.com/)
|
|
||||||
|
|
||||||
## 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(');
|
|
||||||
```
|
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
|
||||||
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
|
||||||
|
|
||||||
[](https://push.rocks)
|
|
@ -2,5 +2,16 @@
|
|||||||
"npmci": {
|
"npmci": {
|
||||||
"npmGlobalTools": [],
|
"npmGlobalTools": [],
|
||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"projectType": "npm",
|
||||||
|
"module": {
|
||||||
|
"githost": "gitlab.com",
|
||||||
|
"gitscope": "pushrocks",
|
||||||
|
"gitrepo": "qenv",
|
||||||
|
"description": "easy promised environments",
|
||||||
|
"npmPackagename": "@pushrocks/qenv",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
14512
package-lock.json
generated
14512
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
39
package.json
39
package.json
@ -1,13 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/qenv",
|
"name": "@pushrocks/qenv",
|
||||||
"version": "2.0.2",
|
"version": "5.0.0",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "easy promised environments",
|
"description": "easy promised environments",
|
||||||
"main": "dist/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/)",
|
"test": "(tstest test/)",
|
||||||
"build": "(tsbuild)"
|
"build": "(tsbuild --web)"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -25,13 +26,29 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/qenv#README",
|
"homepage": "https://gitlab.com/pushrocks/qenv#README",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.0.22",
|
"@gitzone/tsbuild": "^2.1.63",
|
||||||
"@gitzone/tsrun": "^1.1.12",
|
"@gitzone/tsrun": "^1.2.37",
|
||||||
"@gitzone/tstest": "^1.0.13",
|
"@gitzone/tstest": "^1.0.72",
|
||||||
"@pushrocks/tapbundle": "^3.0.5",
|
"@pushrocks/tapbundle": "^5.0.4",
|
||||||
"@types/node": "^10.5.8"
|
"@types/node": "^18.6.2",
|
||||||
|
"tslint": "^6.1.3",
|
||||||
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartfile": "^6.0.6"
|
"@pushrocks/smartfile": "^10.0.4",
|
||||||
}
|
"@pushrocks/smartlog": "^3.0.1",
|
||||||
|
"@pushrocks/smartpath": "^5.0.5"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"ts/**/*",
|
||||||
|
"ts_web/**/*",
|
||||||
|
"dist/**/*",
|
||||||
|
"dist_*/**/*",
|
||||||
|
"dist_ts/**/*",
|
||||||
|
"dist_ts_web/**/*",
|
||||||
|
"assets/**/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
54
readme.md
Normal file
54
readme.md
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
# @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
|
||||||
|
|
||||||
|
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)
|
||||||
|
Platform support | [](https://lossless.cloud) [](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.
|
||||||
|
|
||||||
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||||
|
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||||
|
|
||||||
|
[](https://maintainedby.lossless.com)
|
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
|
|
@ -1,4 +1,4 @@
|
|||||||
vars:
|
required:
|
||||||
- key1
|
- key1
|
||||||
- key2
|
- key2
|
||||||
- key3
|
- key3
|
31
test/test.ts
31
test/test.ts
@ -1,31 +1,38 @@
|
|||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import { tap, expect } from '@pushrocks/tapbundle';
|
import { tap, expect } from '@pushrocks/tapbundle';
|
||||||
import * as qenv from '../ts/index';
|
import * as qenv from '../ts/index.js';
|
||||||
|
|
||||||
|
import * as smartpath from '@pushrocks/smartpath';
|
||||||
|
|
||||||
|
export {
|
||||||
|
smartpath
|
||||||
|
}
|
||||||
|
|
||||||
|
const __dirname = smartpath.get.dirnameFromImportMetaUrl(import.meta.url);
|
||||||
|
|
||||||
|
|
||||||
process.env['key1'] = 'original';
|
process.env['key1'] = 'original';
|
||||||
|
|
||||||
let qenvTestObject: qenv.Qenv;
|
let testQenv: qenv.Qenv;
|
||||||
|
|
||||||
tap.test('should create a new class', async () => {
|
tap.test('should create a new class', async () => {
|
||||||
qenvTestObject = new qenv.Qenv(
|
testQenv = new qenv.Qenv(path.join(__dirname, 'assets'), path.join(__dirname, 'assets'), false);
|
||||||
path.join(__dirname, 'assets'),
|
expect(testQenv).toBeInstanceOf(qenv.Qenv);
|
||||||
path.join(__dirname, 'assets'),
|
|
||||||
false
|
|
||||||
);
|
|
||||||
expect(qenvTestObject).to.be.instanceof(qenv.Qenv);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('key1 should be not be overwritten since it is already present', async () => {
|
tap.test('key1 should be not be overwritten since it is already present', async () => {
|
||||||
expect(process.env.key1).to.equal('original');
|
expect(testQenv.getEnvVarRequired('key1')).toEqual('original');
|
||||||
|
expect(testQenv.getEnvVarOnDemand('key1')).toEqual('original');
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('key2 should be read from Yml', async () => {
|
tap.test('key2 should be read from Yml', async () => {
|
||||||
expect(process.env.key2).to.equal('fromYml');
|
expect(testQenv.getEnvVarRequired('key2')).toEqual('fromJson');
|
||||||
|
expect(testQenv.getEnvVarOnDemand('key2')).toEqual('fromJson');
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('keyValueObjectArray should hold all retrieved values', async () => {
|
tap.test('keyValueObjectArray should hold all retrieved values', async () => {
|
||||||
expect(qenvTestObject.keyValueObjectArray[0].value).to.equal('original');
|
expect(testQenv.keyValueObject.key1).toEqual('original');
|
||||||
expect(qenvTestObject.keyValueObjectArray[1].value).to.equal('fromYml');
|
expect(testQenv.keyValueObject.key2).toEqual('fromJson');
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
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: '@pushrocks/qenv',
|
||||||
|
version: '5.0.0',
|
||||||
|
description: 'easy promised environments'
|
||||||
|
}
|
@ -1 +1 @@
|
|||||||
export * from './qenv.classes.qenv';
|
export * from './qenv.classes.qenv.js';
|
||||||
|
@ -1,27 +1,35 @@
|
|||||||
import * as plugins from './qenv.plugins';
|
import * as plugins from './qenv.plugins.js';
|
||||||
import * as helpers from './qenv.helpers';
|
|
||||||
|
|
||||||
export interface IKeyValueObject {
|
|
||||||
key: string;
|
|
||||||
value: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class Qenv
|
||||||
|
* allows to make assertions about the environments while being more flexibel in how to meet them
|
||||||
|
*/
|
||||||
export class Qenv {
|
export class Qenv {
|
||||||
requiredEnvVars: string[] = [];
|
public requiredEnvVars: string[] = [];
|
||||||
availableEnvVars: string[] = [];
|
public availableEnvVars: string[] = [];
|
||||||
missingEnvVars: string[] = [];
|
public missingEnvVars: string[] = [];
|
||||||
keyValueObjectArray: IKeyValueObject[] = [];
|
public keyValueObject: { [key: string]: any } = {};
|
||||||
constructor(basePathArg = process.cwd(), envYmlPathArg, failOnMissing = true) {
|
public logger = new plugins.smartlog.ConsoleLog();
|
||||||
basePathArg = plugins.path.resolve(basePathArg);
|
|
||||||
envYmlPathArg = plugins.path.resolve(envYmlPathArg);
|
// filePaths
|
||||||
helpers.getRequiredEnvVars(basePathArg, this.requiredEnvVars);
|
public qenvFilePathAbsolute: string;
|
||||||
helpers.getAvailableEnvVars(
|
public envFilePathAbsolute: string;
|
||||||
this.requiredEnvVars,
|
|
||||||
envYmlPathArg,
|
constructor(qenvFileBasePathArg = process.cwd(), envFileBasePathArg, failOnMissing = true) {
|
||||||
this.availableEnvVars,
|
// lets make sure paths are absolute
|
||||||
this.keyValueObjectArray
|
this.qenvFilePathAbsolute = plugins.path.join(
|
||||||
|
plugins.path.resolve(qenvFileBasePathArg),
|
||||||
|
'qenv.yml'
|
||||||
);
|
);
|
||||||
this.missingEnvVars = helpers.getMissingEnvVars(this.requiredEnvVars, this.availableEnvVars);
|
this.envFilePathAbsolute = plugins.path.join(
|
||||||
|
plugins.path.resolve(envFileBasePathArg),
|
||||||
|
'env.json'
|
||||||
|
);
|
||||||
|
|
||||||
|
this.getRequiredEnvVars();
|
||||||
|
this.getAvailableEnvVars();
|
||||||
|
|
||||||
|
this.missingEnvVars = this.getMissingEnvVars();
|
||||||
|
|
||||||
// handle missing variables
|
// handle missing variables
|
||||||
if (this.missingEnvVars.length > 0) {
|
if (this.missingEnvVars.length > 0) {
|
||||||
@ -30,13 +38,158 @@ export class Qenv {
|
|||||||
console.error('However some Env variables could not be resolved:');
|
console.error('However some Env variables could not be resolved:');
|
||||||
console.log(this.missingEnvVars);
|
console.log(this.missingEnvVars);
|
||||||
if (failOnMissing) {
|
if (failOnMissing) {
|
||||||
console.error('Exiting!');
|
this.logger.log('error', 'Exiting!');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
} else {
|
||||||
|
this.logger.log('warn', 'qenv is not set to fail on missing environment variables');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getEnvVar(envVarName): string {
|
/**
|
||||||
return process.env[envVarName];
|
* 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];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* tries to get any env var even if it is not required
|
||||||
|
* @param wantedEnvVar
|
||||||
|
*/
|
||||||
|
public getEnvVarOnDemand(wantedEnvVar: string): string {
|
||||||
|
let envVarFromEnvironmentVariable: string;
|
||||||
|
let envVarFromEnvJsonFile: string;
|
||||||
|
let envVarFromDockerSecret: string;
|
||||||
|
let dockerSecretJson: string;
|
||||||
|
|
||||||
|
// env var check
|
||||||
|
if (process.env[wantedEnvVar]) {
|
||||||
|
this.availableEnvVars.push(wantedEnvVar);
|
||||||
|
envVarFromEnvironmentVariable = process.env[wantedEnvVar];
|
||||||
|
}
|
||||||
|
|
||||||
|
// env file check
|
||||||
|
// lets determine the actual env yml
|
||||||
|
let envJsonFileAsObject;
|
||||||
|
try {
|
||||||
|
envJsonFileAsObject = plugins.smartfile.fs.toObjectSync(this.envFilePathAbsolute);
|
||||||
|
} catch (err) {
|
||||||
|
envJsonFileAsObject = {};
|
||||||
|
}
|
||||||
|
if (envJsonFileAsObject.hasOwnProperty(wantedEnvVar)) {
|
||||||
|
envVarFromEnvJsonFile = envJsonFileAsObject[wantedEnvVar];
|
||||||
|
}
|
||||||
|
|
||||||
|
// docker secret check
|
||||||
|
if (
|
||||||
|
plugins.smartfile.fs.isDirectory('/run') &&
|
||||||
|
plugins.smartfile.fs.isDirectory('/run/secrets') &&
|
||||||
|
plugins.smartfile.fs.fileExistsSync(`/run/secrets/${wantedEnvVar}`)
|
||||||
|
) {
|
||||||
|
envVarFromDockerSecret = plugins.smartfile.fs.toStringSync(`/run/secrets/${wantedEnvVar}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// docker secret.json
|
||||||
|
if (
|
||||||
|
plugins.smartfile.fs.isDirectory('/run') &&
|
||||||
|
plugins.smartfile.fs.isDirectory('/run/secrets')
|
||||||
|
) {
|
||||||
|
const availableSecrets = plugins.smartfile.fs.listAllItemsSync('/run/secrets');
|
||||||
|
for (const secret of availableSecrets) {
|
||||||
|
if (secret.includes('secret.json') && !envVarFromDockerSecret) {
|
||||||
|
const secretObject = plugins.smartfile.fs.toObjectSync(`/run/secrets/${secret}`);
|
||||||
|
envVarFromDockerSecret = secretObject[wantedEnvVar];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// warn if there is more than one candidate
|
||||||
|
const availableCcandidates: any[] = [];
|
||||||
|
[
|
||||||
|
envVarFromEnvironmentVariable,
|
||||||
|
envVarFromEnvJsonFile,
|
||||||
|
envVarFromDockerSecret,
|
||||||
|
dockerSecretJson
|
||||||
|
].forEach(candidate => {
|
||||||
|
if (candidate) {
|
||||||
|
availableCcandidates.push(candidate);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (availableCcandidates.length > 1) {
|
||||||
|
this.logger.log(
|
||||||
|
'warn',
|
||||||
|
`found multiple candidates for ${wantedEnvVar} Choosing in the order of envVar, envFileVar, dockerSecret, dockerSecretJson`
|
||||||
|
);
|
||||||
|
console.log(availableCcandidates);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (true) {
|
||||||
|
case !!envVarFromEnvironmentVariable:
|
||||||
|
this.logger.log('ok', `found ${wantedEnvVar} as environment variable`);
|
||||||
|
return envVarFromEnvironmentVariable;
|
||||||
|
case !!envVarFromEnvJsonFile:
|
||||||
|
this.logger.log('ok', `found ${wantedEnvVar} as env.json variable`);
|
||||||
|
return envVarFromEnvJsonFile;
|
||||||
|
case !!envVarFromDockerSecret:
|
||||||
|
this.logger.log('ok', `found ${wantedEnvVar} as docker secret`);
|
||||||
|
return envVarFromDockerSecret;
|
||||||
|
case !!dockerSecretJson:
|
||||||
|
this.logger.log('ok', `found ${wantedEnvVar} as docker secret.json`);
|
||||||
|
return dockerSecretJson;
|
||||||
|
default:
|
||||||
|
this.logger.log(
|
||||||
|
'warn',
|
||||||
|
`could not find the wanted environment variable ${wantedEnvVar} anywhere`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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',
|
||||||
|
`qenv (promised environment): ./qenv.yml File does not contain a 'required' Array! This might be ok though.`
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
for (const keyArg of Object.keys(qenvFile.required)) {
|
||||||
|
this.requiredEnvVars.push(qenvFile.required[keyArg]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
@ -1,55 +0,0 @@
|
|||||||
import * as plugins from './qenv.plugins';
|
|
||||||
import { IKeyValueObject } from './qenv.classes.qenv';
|
|
||||||
|
|
||||||
export let getRequiredEnvVars = (pathArg: string, requiredEnvVarsArray: string[]) => {
|
|
||||||
let qenvFilePath = plugins.path.join(pathArg, 'qenv.yml');
|
|
||||||
let qenvFile = plugins.smartfile.fs.toObjectSync(qenvFilePath);
|
|
||||||
for (let keyArg in qenvFile.vars) {
|
|
||||||
requiredEnvVarsArray.push(qenvFile.vars[keyArg]);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export let getAvailableEnvVars = (
|
|
||||||
requiredEnvVarsArg: string[],
|
|
||||||
envYmlPathArg: string,
|
|
||||||
availableEnvVarsArray: string[],
|
|
||||||
keyValueObjectArrayArg: IKeyValueObject[]
|
|
||||||
) => {
|
|
||||||
envYmlPathArg = plugins.path.join(envYmlPathArg, 'env.yml');
|
|
||||||
let envYml;
|
|
||||||
try {
|
|
||||||
envYml = plugins.smartfile.fs.toObjectSync(envYmlPathArg);
|
|
||||||
} catch (err) {
|
|
||||||
console.log("env file couldn't be found at " + envYmlPathArg);
|
|
||||||
envYml = {};
|
|
||||||
}
|
|
||||||
for (let requiredEnvVar of requiredEnvVarsArg) {
|
|
||||||
if (process.env[requiredEnvVar]) {
|
|
||||||
availableEnvVarsArray.push(requiredEnvVar);
|
|
||||||
keyValueObjectArrayArg.push({
|
|
||||||
key: requiredEnvVar,
|
|
||||||
value: process.env[requiredEnvVar]
|
|
||||||
});
|
|
||||||
} else if (envYml.hasOwnProperty(requiredEnvVar)) {
|
|
||||||
process.env[requiredEnvVar] = envYml[requiredEnvVar];
|
|
||||||
availableEnvVarsArray.push(requiredEnvVar);
|
|
||||||
keyValueObjectArrayArg.push({
|
|
||||||
key: requiredEnvVar,
|
|
||||||
value: process.env[requiredEnvVar]
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export let getMissingEnvVars = (
|
|
||||||
requiredEnvVarsArray: string[],
|
|
||||||
availableEnvVarsArray: string[]
|
|
||||||
): string[] => {
|
|
||||||
const missingEnvVars: string[] = [];
|
|
||||||
for (const envVar of requiredEnvVarsArray) {
|
|
||||||
if (!availableEnvVarsArray.includes(envVar)) {
|
|
||||||
missingEnvVars.push(envVar);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return missingEnvVars;
|
|
||||||
};
|
|
@ -1,2 +1,10 @@
|
|||||||
export import path = require('path');
|
// native
|
||||||
export import smartfile = require('@pushrocks/smartfile');
|
import * as path from 'path';
|
||||||
|
|
||||||
|
export { path };
|
||||||
|
|
||||||
|
// @pushrocks scope
|
||||||
|
import * as smartfile from '@pushrocks/smartfile';
|
||||||
|
import * as smartlog from '@pushrocks/smartlog';
|
||||||
|
|
||||||
|
export { smartfile, smartlog };
|
||||||
|
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"useDefineForClassFields": false,
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "ES2022",
|
||||||
|
"moduleResolution": "nodenext",
|
||||||
|
"esModuleInterop": true
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "tslint-config-standard"
|
|
||||||
}
|
|
Reference in New Issue
Block a user