Compare commits

...

25 Commits

Author SHA1 Message Date
a1aa1543a2 4.0.1 2019-08-06 17:37:07 +02:00
0e48cb8f89 fix(core): update 2019-08-06 17:37:07 +02:00
d76d1a263b 4.0.0 2019-01-15 23:54:29 +01:00
5f6f434998 BREAKING CHANGE(process.env): treat environment as immutable 2019-01-15 23:54:29 +01:00
8e7909e367 3.1.1 2019-01-14 22:42:25 +01:00
ff9302f745 fix(core): update 2019-01-14 22:42:24 +01:00
3577fa22e1 3.1.0 2019-01-14 01:54:05 +01:00
f5275adfbd feat(docker secret): docker secret.json can now also be named [anything].secret.json 2019-01-14 01:54:04 +01:00
31bf1b27a4 3.0.7 2019-01-13 22:33:19 +01:00
a77def5844 fix(core): update 2019-01-13 22:33:18 +01:00
2357699f3e 3.0.6 2019-01-13 22:04:20 +01:00
01f5784488 fix(core): update 2019-01-13 22:04:20 +01:00
80f35c39aa 3.0.5 2019-01-13 21:35:57 +01:00
87f55773bd fix(core): update 2019-01-13 21:35:57 +01:00
1a25341232 3.0.4 2019-01-13 18:25:09 +01:00
ccd41d86bf fix(core): update 2019-01-13 18:25:09 +01:00
0a3a7e480e 3.0.3 2019-01-13 00:00:32 +01:00
33a91c6ae7 fix(core): update 2019-01-13 00:00:32 +01:00
0cfbdf2c9e 3.0.2 2019-01-06 03:36:40 +01:00
f7362e5444 fix(core): update 2019-01-06 03:36:40 +01:00
7844fdb954 3.0.1 2019-01-06 03:20:59 +01:00
81d1a4f15e fix(core): update 2019-01-06 03:20:59 +01:00
da02923679 3.0.0 2019-01-06 01:30:56 +01:00
d7927ead86 2.0.3 2019-01-06 01:30:07 +01:00
9214ac49d5 fix(core): update 2019-01-06 01:30:07 +01:00
12 changed files with 1420 additions and 413 deletions

22
.gitignore vendored
View File

@ -1,4 +1,22 @@
node_modules/ .nogit/
# artifacts
coverage/ coverage/
pages/
public/ public/
pages/
# installs
node_modules/
# caches
.yarn/
.cache/
.rpt2_cache
# builds
dist/
dist_web/
dist_serve/
dist_ts_web/
# custom

View File

@ -1,5 +1,5 @@
# gitzone standard # gitzone ci_default
image: hosttoday/ht-docker-node:npmci image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
cache: cache:
paths: paths:
@ -26,6 +26,7 @@ mirror:
snyk: snyk:
stage: security stage: security
script: script:
- npmci npm prepare
- npmci command npm install -g snyk - npmci command npm install -g snyk
- npmci command npm install --ignore-scripts - npmci command npm install --ignore-scripts
- npmci command snyk test - npmci command snyk test
@ -36,21 +37,11 @@ snyk:
# ==================== # ====================
# 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: testLTS:
stage: test stage: test
script: script:
- npmci npm prepare
- npmci node install lts - npmci node install lts
- npmci npm install - npmci npm install
- npmci npm test - npmci npm test
@ -58,13 +49,14 @@ testLTS:
tags: tags:
- docker - docker
- notpriv - notpriv
testSTABLE: testBuild:
stage: test stage: test
script: script:
- npmci node install stable - npmci npm prepare
- npmci node install lts
- npmci npm install - npmci npm install
- npmci npm test - npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/ coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- docker - docker
@ -73,7 +65,7 @@ testSTABLE:
release: release:
stage: release stage: release
script: script:
- npmci node install stable - npmci node install lts
- npmci npm publish - npmci npm publish
only: only:
- tags - tags
@ -86,19 +78,11 @@ release:
# ==================== # ====================
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 install
--env SOURCE_CODE="$PWD" - npmci command "tslint -c tslint.json ./ts/**/*.ts"
--volume "$PWD":/code
--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 - docker
- priv - priv
@ -114,12 +98,15 @@ trigger:
- notpriv - notpriv
pages: pages:
image: hosttoday/ht-docker-node:npmci image: hosttoday/ht-docker-dbase:npmci
services:
- docker:18-dind
stage: metadata stage: metadata
script: script:
- npmci command npm install -g typedoc typescript - 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:
- docker - docker
- notpriv - notpriv
@ -130,13 +117,3 @@ pages:
paths: paths:
- public - public
allow_failure: true 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

View File

@ -1,25 +1,20 @@
# qenv # @pushrocks/qenv
easy promised environments easy promised environments
## Availabililty ## Availabililty and Links
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/qenv)
[![npm](https://pushrocks.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/qenv) * [gitlab.com (source)](https://gitlab.com/pushrocks/qenv)
[![git](https://pushrocks.gitlab.io/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/qenv) * [github.com (source mirror)](https://github.com/pushrocks/qenv)
[![git](https://pushrocks.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/pushrocks/qenv) * [docs (typedoc)](https://pushrocks.gitlab.io/qenv/)
[![docs](https://pushrocks.gitlab.io/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/qenv/)
## Status for master ## Status for master
[![build status](https://gitlab.com/pushrocks/qenv/badges/master/build.svg)](https://gitlab.com/pushrocks/qenv/commits/master)
[![build status](https://GitLab.com/pushrocks/qenv/badges/master/build.svg)](https://GitLab.com/pushrocks/qenv/commits/master) [![coverage report](https://gitlab.com/pushrocks/qenv/badges/master/coverage.svg)](https://gitlab.com/pushrocks/qenv/commits/master)
[![coverage report](https://GitLab.com/pushrocks/qenv/badges/master/coverage.svg)](https://GitLab.com/pushrocks/qenv/commits/master) [![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/qenv.svg)](https://www.npmjs.com/package/@pushrocks/qenv)
[![npm downloads per month](https://img.shields.io/npm/dm/qenv.svg)](https://www.npmjs.com/package/qenv) [![Known Vulnerabilities](https://snyk.io/test/npm/@pushrocks/qenv/badge.svg)](https://snyk.io/test/npm/@pushrocks/qenv)
[![Dependency Status](https://david-dm.org/pushrocks/qenv.svg)](https://david-dm.org/pushrocks/qenv) [![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/qenv/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/qenv/master/dependencies/npm) [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![bitHound Code](https://www.bithound.io/github/pushrocks/qenv/badges/code.svg)](https://www.bithound.io/github/pushrocks/qenv) [![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](https://prettier.io/)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
## Usage ## Usage
@ -40,7 +35,9 @@ import { Qenv } from 'qenv';
const 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(');
``` ```
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh) For further information read the linked docs at the top of this readme.
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://push.rocks) > MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://maintainedby.lossless.com)

View File

@ -2,5 +2,15 @@
"npmci": { "npmci": {
"npmGlobalTools": [], "npmGlobalTools": [],
"npmAccessLevel": "public" "npmAccessLevel": "public"
},
"gitzone": {
"module": {
"githost": "gitlab.com",
"gitscope": "pushrocks",
"gitrepo": "qenv",
"shortDescription": "easy promised environments",
"npmPackagename": "@pushrocks/qenv",
"license": "MIT"
}
} }
} }

1376
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/qenv", "name": "@pushrocks/qenv",
"version": "2.0.2", "version": "4.0.1",
"private": false, "private": false,
"description": "easy promised environments", "description": "easy promised environments",
"main": "dist/index.js", "main": "dist/index.js",
@ -25,13 +25,27 @@
}, },
"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.11",
"@gitzone/tsrun": "^1.1.12", "@gitzone/tsrun": "^1.2.8",
"@gitzone/tstest": "^1.0.13", "@gitzone/tstest": "^1.0.24",
"@pushrocks/tapbundle": "^3.0.5", "@pushrocks/tapbundle": "^3.0.11",
"@types/node": "^10.5.8" "@types/node": "^12.6.9",
"tslint": "^5.18.0",
"tslint-config-prettier": "^1.18.0"
}, },
"dependencies": { "dependencies": {
"@pushrocks/smartfile": "^6.0.6" "@pushrocks/smartfile": "^7.0.4",
} "@pushrocks/smartlog": "^2.0.19"
},
"files": [
"ts/*",
"ts_web/*",
"dist/*",
"dist_web/*",
"dist_ts_web/*",
"assets/*",
"cli.js",
"npmextra.json",
"readme.md"
]
} }

View File

@ -1,4 +1,4 @@
vars: required:
- key1 - key1
- key2 - key2
- key3 - key3

View File

@ -4,28 +4,26 @@ import * as qenv from '../ts/index';
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).to.be.instanceof(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')).to.equal('original');
expect(testQenv.getEnvVarOnDemand('key1')).to.equal('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')).to.equal('fromYml');
expect(testQenv.getEnvVarOnDemand('key2')).to.equal('fromYml');
}); });
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).to.equal('original');
expect(qenvTestObject.keyValueObjectArray[1].value).to.equal('fromYml'); expect(testQenv.keyValueObject.key2).to.equal('fromYml');
}); });
tap.start(); tap.start();

View File

@ -1,27 +1,43 @@
import * as plugins from './qenv.plugins'; import * as plugins from './qenv.plugins';
import * as helpers from './qenv.helpers'; plugins.smartlog.defaultLogger.enableConsole();
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: plugins.smartlog.Smartlog;
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(
this.availableEnvVars, qenvFileBasePathArg = process.cwd(),
this.keyValueObjectArray 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.missingEnvVars = helpers.getMissingEnvVars(this.requiredEnvVars, this.availableEnvVars); this.envFilePathAbsolute = plugins.path.join(
plugins.path.resolve(envFileBasePathArg),
'env.yml'
);
this.getRequiredEnvVars();
this.getAvailableEnvVars();
this.missingEnvVars = this.getMissingEnvVars();
// handle missing variables // handle missing variables
if (this.missingEnvVars.length > 0) { if (this.missingEnvVars.length > 0) {
@ -36,7 +52,137 @@ export class Qenv {
} }
} }
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 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.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;
}
/**
* 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;
};
} }

View File

@ -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;
};

View File

@ -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 };

View File

@ -1,3 +1,17 @@
{ {
"extends": "tslint-config-standard" "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"
} }