qenv/ts/qenv.classes.qenv.ts

38 lines
1.2 KiB
TypeScript
Raw Normal View History

2017-05-12 16:17:22 +00:00
import * as plugins from './qenv.plugins'
import * as helpers from './qenv.helpers'
export interface IKeyValueObject {
key: string,
value: string
}
export class Qenv {
requiredEnvVars: string[] = []
availableEnvVars: string[] = []
missingEnvVars: string[] = []
keyValueObjectArray: IKeyValueObject[] = []
constructor (basePathArg = process.cwd(), envYmlPathArg, failOnMissing = true) {
2017-05-12 17:12:03 +00:00
basePathArg = plugins.path.resolve(basePathArg)
2017-05-13 09:40:10 +00:00
envYmlPathArg = plugins.path.resolve(envYmlPathArg)
2017-05-12 16:17:22 +00:00
helpers.getRequiredEnvVars(basePathArg, this.requiredEnvVars)
helpers.getAvailableEnvVars(this.requiredEnvVars, envYmlPathArg, this.availableEnvVars, this.keyValueObjectArray)
this.missingEnvVars = helpers.getMissingEnvVars(this.requiredEnvVars, this.availableEnvVars)
// 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)
}
}
}
getEnvVar (envVarName): string {
return process.env[envVarName]
}
}