Compare commits
52 Commits
Author | SHA1 | Date | |
---|---|---|---|
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 | |||
309b3e100d | |||
eac42f50d6 | |||
c83b7688e9 | |||
524fc5265f | |||
692168c8c2 | |||
2b3198c5bb | |||
26104a231d | |||
44bc565efe | |||
09c6a0aa8a | |||
5a307f4c2c | |||
2925a88e5b | |||
0a4bad1ad0 | |||
e34d37da19 | |||
a029eeb6c1 | |||
7527049fd0 | |||
25589d4ca2 | |||
73c223af6c | |||
ead69f3bb9 | |||
7de17bbefa | |||
4e39970dd7 | |||
2a7c239176 | |||
35e71ccf00 | |||
11a2cf83ee |
23
.gitignore
vendored
23
.gitignore
vendored
@ -1,3 +1,22 @@
|
|||||||
node_modules/
|
.nogit/
|
||||||
|
|
||||||
|
# artifacts
|
||||||
coverage/
|
coverage/
|
||||||
docs/
|
public/
|
||||||
|
pages/
|
||||||
|
|
||||||
|
# installs
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# caches
|
||||||
|
.yarn/
|
||||||
|
.cache/
|
||||||
|
.rpt2_cache
|
||||||
|
|
||||||
|
# builds
|
||||||
|
dist/
|
||||||
|
dist_web/
|
||||||
|
dist_serve/
|
||||||
|
dist_ts_web/
|
||||||
|
|
||||||
|
# custom
|
112
.gitlab-ci.yml
112
.gitlab-ci.yml
@ -1,35 +1,119 @@
|
|||||||
image: hosttoday/ht-docker-node:npmts
|
# gitzone ci_default
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
|
cache:
|
||||||
|
paths:
|
||||||
|
- .npmci_cache/
|
||||||
|
key: "$CI_BUILD_STAGE"
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
|
- security
|
||||||
- test
|
- test
|
||||||
- release
|
- release
|
||||||
|
- metadata
|
||||||
|
|
||||||
testLEGACY:
|
# ====================
|
||||||
stage: test
|
# security stage
|
||||||
|
# ====================
|
||||||
|
mirror:
|
||||||
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci test lts
|
- npmci git mirror
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- 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:
|
testLTS:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci test lts
|
- npmci npm prepare
|
||||||
|
- npmci node install lts
|
||||||
|
- npmci npm install
|
||||||
|
- npmci npm test
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
testSTABLE:
|
|
||||||
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci test stable
|
- npmci npm prepare
|
||||||
|
- npmci node install lts
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command npm run build
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
|
- notpriv
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
script:
|
script:
|
||||||
- npmci publish
|
- 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:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
artifacts:
|
||||||
- docker
|
expire_in: 1 week
|
||||||
|
paths:
|
||||||
|
- public
|
||||||
|
allow_failure: true
|
||||||
|
40
README.md
40
README.md
@ -1,19 +1,43 @@
|
|||||||
# qenv
|
# @pushrocks/qenv
|
||||||
provides an easy way for promised environments
|
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
|
## Usage
|
||||||
|
|
||||||
|
Use TypeScript for best in class instellisense.
|
||||||
|
|
||||||
qenv works with two files:
|
qenv works with two files:
|
||||||
|
|
||||||
* **qenv.yml** - specifies which ENV vars are required.
|
- **qenv.yml** - specifies which ENV vars are required.
|
||||||
* **env.yml** - specifies all env vars that are not already set in the current environment.
|
- **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.
|
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
|
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.
|
However when on another machine you can have a env.yml that will be added to the environment by qenv.
|
||||||
|
|
||||||
```typescript
|
```javascript
|
||||||
import {Qenv} from "qenv";
|
import { Qenv } from 'qenv';
|
||||||
|
|
||||||
myQenv = new Qenv("path/to/dir/where/qenv.yml/is/","path/to/dir/where/env.yml/is(");
|
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)
|
||||||
|
6
dist/index.d.ts
vendored
6
dist/index.d.ts
vendored
@ -1,6 +0,0 @@
|
|||||||
export declare class Qenv {
|
|
||||||
requiredEnvVars: string[];
|
|
||||||
availableEnvVars: string[];
|
|
||||||
missingEnvVars: string[];
|
|
||||||
constructor(basePathArg: string, envYmlPathArg: any, failOnMissing?: boolean);
|
|
||||||
}
|
|
62
dist/index.js
vendored
62
dist/index.js
vendored
File diff suppressed because one or more lines are too long
5
dist/qenv.plugins.d.ts
vendored
5
dist/qenv.plugins.d.ts
vendored
@ -1,5 +0,0 @@
|
|||||||
import "typings-global";
|
|
||||||
export import beautylog = require("beautylog");
|
|
||||||
export declare let lodash: any;
|
|
||||||
export import path = require("path");
|
|
||||||
export import smartfile = require("smartfile");
|
|
8
dist/qenv.plugins.js
vendored
8
dist/qenv.plugins.js
vendored
@ -1,8 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require("typings-global");
|
|
||||||
exports.beautylog = require("beautylog");
|
|
||||||
exports.lodash = require("lodash");
|
|
||||||
exports.path = require("path");
|
|
||||||
exports.smartfile = require("smartfile");
|
|
||||||
|
|
||||||
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInFlbnYucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsUUFBTyxnQkFBZ0IsQ0FBQyxDQUFBO0FBQ1YsaUJBQVMsV0FBVyxXQUFXLENBQUMsQ0FBQztBQUNwQyxjQUFNLEdBQUcsT0FBTyxDQUFDLFFBQVEsQ0FBQyxDQUFDO0FBQ3hCLFlBQUksV0FBVyxNQUFNLENBQUMsQ0FBQztBQUN2QixpQkFBUyxXQUFXLFdBQVcsQ0FBQyxDQUFDIiwiZmlsZSI6InFlbnYucGx1Z2lucy5qcyIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCBcInR5cGluZ3MtZ2xvYmFsXCI7XG5leHBvcnQgaW1wb3J0IGJlYXV0eWxvZyA9IHJlcXVpcmUoXCJiZWF1dHlsb2dcIik7XG5leHBvcnQgbGV0IGxvZGFzaCA9IHJlcXVpcmUoXCJsb2Rhc2hcIik7XG5leHBvcnQgaW1wb3J0IHBhdGggPSByZXF1aXJlKFwicGF0aFwiKTtcbmV4cG9ydCBpbXBvcnQgc21hcnRmaWxlID0gcmVxdWlyZShcInNtYXJ0ZmlsZVwiKTtcbiJdfQ==
|
|
16
npmextra.json
Normal file
16
npmextra.json
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"npmci": {
|
||||||
|
"npmGlobalTools": [],
|
||||||
|
"npmAccessLevel": "public"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"module": {
|
||||||
|
"githost": "gitlab.com",
|
||||||
|
"gitscope": "pushrocks",
|
||||||
|
"gitrepo": "qenv",
|
||||||
|
"shortDescription": "easy promised environments",
|
||||||
|
"npmPackagename": "@pushrocks/qenv",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"mode":"default"
|
|
||||||
}
|
|
1739
package-lock.json
generated
Normal file
1739
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
40
package.json
40
package.json
@ -1,11 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "qenv",
|
"name": "@pushrocks/qenv",
|
||||||
"version": "1.0.8",
|
"version": "4.0.3",
|
||||||
"description": "promised environments",
|
"private": false,
|
||||||
|
"description": "easy promised environments",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(npmts)"
|
"test": "(tstest test/)",
|
||||||
|
"build": "(tsbuild)"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -23,15 +25,27 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/qenv#README",
|
"homepage": "https://gitlab.com/pushrocks/qenv#README",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"npmts-g": "^5.2.6",
|
"@gitzone/tsbuild": "^2.1.11",
|
||||||
"should": "^9.0.2",
|
"@gitzone/tsrun": "^1.2.8",
|
||||||
"typings-test": "^1.0.1"
|
"@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"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"beautylog": "^5.0.12",
|
"@pushrocks/smartfile": "^7.0.4",
|
||||||
"lodash": "^4.13.1",
|
"@pushrocks/smartlog": "^2.0.19"
|
||||||
"q": "^1.4.1",
|
},
|
||||||
"smartfile": "^3.0.10",
|
"files": [
|
||||||
"typings-global": "^1.0.3"
|
"ts/*",
|
||||||
}
|
"ts_web/*",
|
||||||
|
"dist/*",
|
||||||
|
"dist_web/*",
|
||||||
|
"dist_ts_web/*",
|
||||||
|
"assets/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
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
|
2
test/test.d.ts
vendored
2
test/test.d.ts
vendored
@ -1,2 +0,0 @@
|
|||||||
import "typings-test";
|
|
||||||
import "should";
|
|
23
test/test.js
23
test/test.js
@ -1,23 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
require("typings-test");
|
|
||||||
var path = require("path");
|
|
||||||
require("should");
|
|
||||||
var index_1 = require("../dist/index");
|
|
||||||
process.cwd = function () {
|
|
||||||
return path.join(__dirname, "./assets/");
|
|
||||||
};
|
|
||||||
process.env["key1"] = "original";
|
|
||||||
var qenvTestObject;
|
|
||||||
describe("Qenv class", function () {
|
|
||||||
it("should create a new class", function () {
|
|
||||||
qenvTestObject = new index_1.Qenv(process.cwd(), process.cwd(), false);
|
|
||||||
});
|
|
||||||
it("key1 should be not be overwritten since it is already present", function () {
|
|
||||||
process.env.key1.should.equal("original");
|
|
||||||
});
|
|
||||||
it("key2 should be read from Yml", function () {
|
|
||||||
process.env.key2.should.equal("fromYml");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyxDQUFDLENBQUE7QUFDdEIsSUFBWSxJQUFJLFdBQU0sTUFBTSxDQUFDLENBQUE7QUFDN0IsUUFBTyxRQUFRLENBQUMsQ0FBQTtBQUNoQixzQkFBbUIsZUFBZSxDQUFDLENBQUE7QUFFbkMsT0FBTyxDQUFDLEdBQUcsR0FBRztJQUNWLE1BQU0sQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLFNBQVMsRUFBQyxXQUFXLENBQUMsQ0FBQztBQUM1QyxDQUFDLENBQUE7QUFFRCxPQUFPLENBQUMsR0FBRyxDQUFDLE1BQU0sQ0FBQyxHQUFHLFVBQVUsQ0FBQTtBQUVoQyxJQUFJLGNBQW1CLENBQUM7QUFDeEIsUUFBUSxDQUFDLFlBQVksRUFBQztJQUNsQixFQUFFLENBQUMsMkJBQTJCLEVBQUM7UUFDM0IsY0FBYyxHQUFHLElBQUksWUFBSSxDQUFDLE9BQU8sQ0FBQyxHQUFHLEVBQUUsRUFBQyxPQUFPLENBQUMsR0FBRyxFQUFFLEVBQUMsS0FBSyxDQUFDLENBQUM7SUFDakUsQ0FBQyxDQUFDLENBQUM7SUFDSCxFQUFFLENBQUMsK0RBQStELEVBQUM7UUFDL0QsT0FBTyxDQUFDLEdBQUcsQ0FBQyxJQUFJLENBQUMsTUFBTSxDQUFDLEtBQUssQ0FBQyxVQUFVLENBQUMsQ0FBQztJQUM5QyxDQUFDLENBQUMsQ0FBQztJQUNILEVBQUUsQ0FBQyw4QkFBOEIsRUFBQztRQUM5QixPQUFPLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxNQUFNLENBQUMsS0FBSyxDQUFDLFNBQVMsQ0FBQyxDQUFDO0lBQzdDLENBQUMsQ0FBQyxDQUFBO0FBRU4sQ0FBQyxDQUFDLENBQUMiLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCBcInR5cGluZ3MtdGVzdFwiO1xuaW1wb3J0ICogYXMgcGF0aCBmcm9tIFwicGF0aFwiO1xuaW1wb3J0IFwic2hvdWxkXCI7XG5pbXBvcnQge1FlbnZ9IGZyb20gXCIuLi9kaXN0L2luZGV4XCI7XG5cbnByb2Nlc3MuY3dkID0gKCkgPT4ge1xuICAgIHJldHVybiBwYXRoLmpvaW4oX19kaXJuYW1lLFwiLi9hc3NldHMvXCIpO1xufVxuXG5wcm9jZXNzLmVudltcImtleTFcIl0gPSBcIm9yaWdpbmFsXCJcblxubGV0IHFlbnZUZXN0T2JqZWN0OlFlbnY7XG5kZXNjcmliZShcIlFlbnYgY2xhc3NcIixmdW5jdGlvbigpe1xuICAgIGl0KFwic2hvdWxkIGNyZWF0ZSBhIG5ldyBjbGFzc1wiLGZ1bmN0aW9uKCl7XG4gICAgICAgIHFlbnZUZXN0T2JqZWN0ID0gbmV3IFFlbnYocHJvY2Vzcy5jd2QoKSxwcm9jZXNzLmN3ZCgpLGZhbHNlKTtcbiAgICB9KTtcbiAgICBpdChcImtleTEgc2hvdWxkIGJlIG5vdCBiZSBvdmVyd3JpdHRlbiBzaW5jZSBpdCBpcyBhbHJlYWR5IHByZXNlbnRcIixmdW5jdGlvbigpe1xuICAgICAgICBwcm9jZXNzLmVudi5rZXkxLnNob3VsZC5lcXVhbChcIm9yaWdpbmFsXCIpO1xuICAgIH0pO1xuICAgIGl0KFwia2V5MiBzaG91bGQgYmUgcmVhZCBmcm9tIFltbFwiLGZ1bmN0aW9uKCl7XG4gICAgICAgIHByb2Nlc3MuZW52LmtleTIuc2hvdWxkLmVxdWFsKFwiZnJvbVltbFwiKTtcbiAgICB9KVxuXG59KTsiXX0=
|
|
45
test/test.ts
45
test/test.ts
@ -1,24 +1,29 @@
|
|||||||
import "typings-test";
|
import * as path from 'path';
|
||||||
import * as path from "path";
|
import { tap, expect } from '@pushrocks/tapbundle';
|
||||||
import "should";
|
import * as qenv from '../ts/index';
|
||||||
import {Qenv} from "../dist/index";
|
|
||||||
|
|
||||||
process.cwd = () => {
|
process.env['key1'] = 'original';
|
||||||
return path.join(__dirname,"./assets/");
|
|
||||||
}
|
|
||||||
|
|
||||||
process.env["key1"] = "original"
|
let testQenv: qenv.Qenv;
|
||||||
|
|
||||||
let qenvTestObject:Qenv;
|
tap.test('should create a new class', async () => {
|
||||||
describe("Qenv class",function(){
|
testQenv = new qenv.Qenv(path.join(__dirname, 'assets'), path.join(__dirname, 'assets'), false);
|
||||||
it("should create a new class",function(){
|
expect(testQenv).to.be.instanceof(qenv.Qenv);
|
||||||
qenvTestObject = new Qenv(process.cwd(),process.cwd(),false);
|
});
|
||||||
});
|
|
||||||
it("key1 should be not be overwritten since it is already present",function(){
|
|
||||||
process.env.key1.should.equal("original");
|
|
||||||
});
|
|
||||||
it("key2 should be read from Yml",function(){
|
|
||||||
process.env.key2.should.equal("fromYml");
|
|
||||||
})
|
|
||||||
|
|
||||||
});
|
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');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('key2 should be read from Yml', async () => {
|
||||||
|
expect(testQenv.getEnvVarRequired('key2')).to.equal('fromJson');
|
||||||
|
expect(testQenv.getEnvVarOnDemand('key2')).to.equal('fromJson');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('keyValueObjectArray should hold all retrieved values', async () => {
|
||||||
|
expect(testQenv.keyValueObject.key1).to.equal('original');
|
||||||
|
expect(testQenv.keyValueObject.key2).to.equal('fromJson');
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.start();
|
||||||
|
62
ts/index.ts
62
ts/index.ts
@ -1,61 +1 @@
|
|||||||
import * as plugins from "./qenv.plugins";
|
export * from './qenv.classes.qenv';
|
||||||
|
|
||||||
export class Qenv {
|
|
||||||
requiredEnvVars:string[];
|
|
||||||
availableEnvVars:string[];
|
|
||||||
missingEnvVars:string[];
|
|
||||||
constructor(basePathArg = process.cwd(),envYmlPathArg,failOnMissing = true){
|
|
||||||
this.requiredEnvVars = getRequiredEnvVars(basePathArg);
|
|
||||||
this.availableEnvVars = getAvailableEnvVars(this.requiredEnvVars,envYmlPathArg);
|
|
||||||
this.missingEnvVars = getMissingEnvVars(this.requiredEnvVars,this.availableEnvVars);
|
|
||||||
|
|
||||||
//handle missing variables
|
|
||||||
if (this.missingEnvVars.length > 0){
|
|
||||||
plugins.beautylog.info("Required Env Vars are:")
|
|
||||||
console.log(this.requiredEnvVars);
|
|
||||||
plugins.beautylog.error("However some Env variables could not be resolved:");
|
|
||||||
console.log(this.missingEnvVars);
|
|
||||||
if(failOnMissing){
|
|
||||||
plugins.beautylog.error("Exiting!")
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let getRequiredEnvVars = (pathArg:string):string[] => {
|
|
||||||
let result:string[] = [];
|
|
||||||
let qenvFilePath = plugins.path.join(pathArg,"qenv.yml");
|
|
||||||
let qenvFile = plugins.smartfile.local.toObjectSync(qenvFilePath);
|
|
||||||
for(let keyArg in qenvFile.vars){
|
|
||||||
result.push(qenvFile.vars[keyArg]);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
let getAvailableEnvVars = (requiredEnvVarsArg:string[],envYmlPathArg:string):string[] => {
|
|
||||||
let result = [];
|
|
||||||
envYmlPathArg = plugins.path.join(envYmlPathArg,"env.yml")
|
|
||||||
let envYml;
|
|
||||||
try {
|
|
||||||
envYml = plugins.smartfile.local.toObjectSync(envYmlPathArg);
|
|
||||||
}
|
|
||||||
catch(err){
|
|
||||||
plugins.beautylog.log("env file couldn't be found at " + envYmlPathArg)
|
|
||||||
envYml = {};
|
|
||||||
}
|
|
||||||
for(let keyArg in requiredEnvVarsArg){
|
|
||||||
let requiredEnvVar:string = requiredEnvVarsArg[keyArg];
|
|
||||||
if(process.env[requiredEnvVar]){
|
|
||||||
result.push(requiredEnvVar);
|
|
||||||
} else if(envYml.hasOwnProperty(requiredEnvVar)){
|
|
||||||
process.env[requiredEnvVar] = envYml[requiredEnvVar];
|
|
||||||
result.push(requiredEnvVar);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
let getMissingEnvVars = (requiredEnvVarsArray:string[],availableEnvVarsArray:string[]) => {
|
|
||||||
return plugins.lodash.difference(requiredEnvVarsArray,availableEnvVarsArray);
|
|
||||||
}
|
|
||||||
|
190
ts/qenv.classes.qenv.ts
Normal file
190
ts/qenv.classes.qenv.ts
Normal file
@ -0,0 +1,190 @@
|
|||||||
|
import * as plugins from './qenv.plugins';
|
||||||
|
plugins.smartlog.defaultLogger.enableConsole();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
// filePaths
|
||||||
|
public qenvFilePathAbsolute: string;
|
||||||
|
public envFilePathAbsolute: string;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
qenvFileBasePathArg = process.cwd(),
|
||||||
|
envFileBasePathArg,
|
||||||
|
failOnMissing = true,
|
||||||
|
loggerArg: plugins.smartlog.Smartlog = plugins.smartlog.defaultLogger
|
||||||
|
) {
|
||||||
|
this.logger = loggerArg;
|
||||||
|
|
||||||
|
// lets make sure paths are absolute
|
||||||
|
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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 requiredEnvVar
|
||||||
|
*/
|
||||||
|
public getEnvVarOnDemand(requiredEnvVar: string): string {
|
||||||
|
// lets determine the actual env yml
|
||||||
|
let envYml;
|
||||||
|
try {
|
||||||
|
envYml = plugins.smartfile.fs.toObjectSync(this.envFilePathAbsolute);
|
||||||
|
} catch (err) {
|
||||||
|
envYml = {};
|
||||||
|
}
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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')
|
||||||
|
) {
|
||||||
|
const availableSecrets = plugins.smartfile.fs.listAllItemsSync('/run/secrets');
|
||||||
|
for (const secret of availableSecrets) {
|
||||||
|
if (secret.includes('secret.json') && !dockerSecret) {
|
||||||
|
const secretObject = plugins.smartfile.fs.toObjectSync(`/run/secrets/${secret}`);
|
||||||
|
dockerSecret = secretObject[requiredEnvVar];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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,5 +1,10 @@
|
|||||||
import "typings-global";
|
// native
|
||||||
export import beautylog = require("beautylog");
|
import * as path from 'path';
|
||||||
export let lodash = require("lodash");
|
|
||||||
export import path = require("path");
|
export { path };
|
||||||
export import smartfile = require("smartfile");
|
|
||||||
|
// @pushrocks scope
|
||||||
|
import * as smartfile from '@pushrocks/smartfile';
|
||||||
|
import * as smartlog from '@pushrocks/smartlog';
|
||||||
|
|
||||||
|
export { smartfile, smartlog };
|
||||||
|
17
tslint.json
Normal file
17
tslint.json
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"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