fix(core): update
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
import * as plugins from './qenv.plugins';
|
||||
import * as helpers from './qenv.helpers';
|
||||
|
||||
export interface IKeyValueObject {
|
||||
key: string;
|
||||
@ -7,21 +6,32 @@ export interface IKeyValueObject {
|
||||
}
|
||||
|
||||
export class Qenv {
|
||||
requiredEnvVars: string[] = [];
|
||||
availableEnvVars: string[] = [];
|
||||
missingEnvVars: string[] = [];
|
||||
keyValueObjectArray: IKeyValueObject[] = [];
|
||||
constructor(basePathArg = process.cwd(), envYmlPathArg, failOnMissing = true) {
|
||||
basePathArg = plugins.path.resolve(basePathArg);
|
||||
envYmlPathArg = plugins.path.resolve(envYmlPathArg);
|
||||
helpers.getRequiredEnvVars(basePathArg, this.requiredEnvVars);
|
||||
helpers.getAvailableEnvVars(
|
||||
this.requiredEnvVars,
|
||||
envYmlPathArg,
|
||||
this.availableEnvVars,
|
||||
this.keyValueObjectArray
|
||||
);
|
||||
this.missingEnvVars = helpers.getMissingEnvVars(this.requiredEnvVars, this.availableEnvVars);
|
||||
public requiredEnvVars: string[] = [];
|
||||
public availableEnvVars: string[] = [];
|
||||
public missingEnvVars: string[] = [];
|
||||
public keyValueObjectArray: IKeyValueObject[] = [];
|
||||
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.yml');
|
||||
|
||||
this.getRequiredEnvVars();
|
||||
this.getAvailableEnvVars();
|
||||
|
||||
this.missingEnvVars = this.getMissingEnvVars();
|
||||
|
||||
// handle missing variables
|
||||
if (this.missingEnvVars.length > 0) {
|
||||
@ -36,7 +46,101 @@ export class Qenv {
|
||||
}
|
||||
}
|
||||
|
||||
getEnvVar(envVarName): string {
|
||||
public getEnvVar(envVarName): string {
|
||||
return process.env[envVarName];
|
||||
}
|
||||
|
||||
public getEnvVarOnDemand(requiredEnvVar: string): string {
|
||||
// lets determine the actual env yml
|
||||
let envYml;
|
||||
try {
|
||||
envYml = plugins.smartfile.fs.toObjectSync(this.envFilePathAbsolute);
|
||||
} catch (err) {
|
||||
console.log("env file couldn't be found at " + this.envFilePathAbsolute);
|
||||
envYml = {};
|
||||
}
|
||||
let envVar: string;
|
||||
let envFileVar: string;
|
||||
let dockerSecret: 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);
|
||||
}
|
||||
if (
|
||||
plugins.smartfile.fs.isDirectory('/run') &&
|
||||
plugins.smartfile.fs.isDirectory('/run/secrets') &&
|
||||
plugins.smartfile.fs.fileExists(`/run/secrets/${requiredEnvVar}`)
|
||||
) {
|
||||
dockerSecret = plugins.smartfile.fs.toStringSync(`/run/secrets/${requiredEnvVar}`);
|
||||
}
|
||||
|
||||
if ((envVar && envFileVar) || (envVar && dockerSecret) || (envFileVar && dockerSecret)) {
|
||||
this.logger.log(
|
||||
'warn',
|
||||
`found multiple candidates for ${requiredEnvVar} Choosing in the order of envVar, envFileVar, dockerSecret`
|
||||
);
|
||||
}
|
||||
|
||||
let chosenVar: string = null;
|
||||
if (envVar) {
|
||||
chosenVar = envVar;
|
||||
} else if (envFileVar) {
|
||||
chosenVar = envFileVar;
|
||||
} else if (dockerSecret) {
|
||||
chosenVar = dockerSecret;
|
||||
}
|
||||
return chosenVar;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets the required env values
|
||||
*/
|
||||
private getRequiredEnvVars = () => {
|
||||
const qenvFile = plugins.smartfile.fs.toObjectSync(this.qenvFilePathAbsolute);
|
||||
if (!qenvFile.required) {
|
||||
this.logger.log('warn', `env File does not contain a 'required' Array!`);
|
||||
}
|
||||
for (const keyArg of Reflect.ownKeys(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);
|
||||
process.env[requiredEnvVar] = chosenVar;
|
||||
this.keyValueObjectArray.push({
|
||||
key: requiredEnvVar,
|
||||
value: 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,16 @@
|
||||
export import path = require('path');
|
||||
export import smartfile = require('@pushrocks/smartfile');
|
||||
// native
|
||||
import * as path from 'path';
|
||||
|
||||
export {
|
||||
path
|
||||
}
|
||||
|
||||
// @pushrocks scope
|
||||
import * as smartfile from '@pushrocks/smartfile';
|
||||
import * as smartlog from '@pushrocks/smartlog';
|
||||
|
||||
export {
|
||||
smartfile,
|
||||
smartlog
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user