qenv/ts/qenv.classes.qenv.ts

43 lines
1.3 KiB
TypeScript
Raw Normal View History

import * as plugins from './qenv.plugins';
import * as helpers from './qenv.helpers';
2017-05-12 16:17:22 +00:00
export interface IKeyValueObject {
key: string;
value: string;
2017-05-12 16:17:22 +00:00
}
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);
2017-05-12 16:17:22 +00:00
// 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);
2017-05-12 16:17:22 +00:00
if (failOnMissing) {
console.error('Exiting!');
process.exit(1);
2017-05-12 16:17:22 +00:00
}
}
}
getEnvVar(envVarName): string {
return process.env[envVarName];
2017-05-12 16:17:22 +00:00
}
}