2018-08-12 22:09:37 +00:00
|
|
|
import * as plugins from './qenv.plugins';
|
|
|
|
import * as helpers from './qenv.helpers';
|
2017-05-12 16:17:22 +00:00
|
|
|
|
|
|
|
export interface IKeyValueObject {
|
2018-08-12 22:09:37 +00:00
|
|
|
key: string;
|
|
|
|
value: string;
|
2017-05-12 16:17:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Qenv {
|
2018-08-12 22:09:37 +00:00
|
|
|
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);
|
2017-05-12 16:17:22 +00:00
|
|
|
|
|
|
|
// handle missing variables
|
|
|
|
if (this.missingEnvVars.length > 0) {
|
2018-08-12 22:09:37 +00:00
|
|
|
console.info('Required Env Vars are:');
|
|
|
|
console.log(this.requiredEnvVars);
|
|
|
|
console.error('However some Env variables could not be resolved:');
|
|
|
|
console.log(this.missingEnvVars);
|
2017-05-12 16:17:22 +00:00
|
|
|
if (failOnMissing) {
|
2018-08-12 22:09:37 +00:00
|
|
|
console.error('Exiting!');
|
|
|
|
process.exit(1);
|
2017-05-12 16:17:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 22:09:37 +00:00
|
|
|
getEnvVar(envVarName): string {
|
|
|
|
return process.env[envVarName];
|
2017-05-12 16:17:22 +00:00
|
|
|
}
|
|
|
|
}
|