qenv/ts/index.ts

72 lines
2.6 KiB
TypeScript
Raw Normal View History

2017-01-21 10:51:30 +00:00
import * as plugins from "./qenv.plugins"
2016-06-19 18:08:34 +00:00
2016-08-04 14:44:42 +00:00
export interface IKeyValueObject {
2016-08-04 14:09:39 +00:00
key: string,
value: string
};
2016-06-20 08:45:43 +00:00
export class Qenv {
2017-01-21 10:51:30 +00:00
requiredEnvVars: string[] = []
availableEnvVars:string[] = []
missingEnvVars:string[] = []
keyValueObjectArray:IKeyValueObject[] = []
2016-06-21 14:13:29 +00:00
constructor(basePathArg = process.cwd(),envYmlPathArg,failOnMissing = true){
2017-01-21 10:51:30 +00:00
getRequiredEnvVars(basePathArg,this.requiredEnvVars)
getAvailableEnvVars(this.requiredEnvVars,envYmlPathArg,this.availableEnvVars,this.keyValueObjectArray)
this.missingEnvVars = getMissingEnvVars(this.requiredEnvVars,this.availableEnvVars)
// handle missing variables
2016-06-21 14:13:29 +00:00
if (this.missingEnvVars.length > 0){
2017-01-21 10:51:30 +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)
if (failOnMissing) {
console.error('Exiting!')
process.exit(1)
2016-06-21 14:13:29 +00:00
}
2016-06-20 08:45:43 +00:00
}
2016-08-04 14:09:39 +00:00
};
2016-06-19 18:08:34 +00:00
};
2017-01-21 10:51:30 +00:00
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])
2016-08-04 14:09:39 +00:00
};
2016-06-19 18:08:34 +00:00
}
2016-08-04 14:09:39 +00:00
2017-01-21 10:51:30 +00:00
let getAvailableEnvVars = (requiredEnvVarsArg: string[],envYmlPathArg: string,availableEnvVarsArray: string[],keyValueObjectArrayArg: IKeyValueObject[]) => {
envYmlPathArg = plugins.path.join(envYmlPathArg,'env.yml')
let envYml
2016-06-20 05:06:15 +00:00
try {
2017-01-21 10:51:30 +00:00
envYml = plugins.smartfile.fs.toObjectSync(envYmlPathArg)
} catch (err) {
console.log("env file couldn't be found at " + envYmlPathArg)
envYml = {}
2016-06-20 05:06:15 +00:00
}
2017-01-21 10:51:30 +00:00
for (let requiredEnvVar of requiredEnvVarsArg){
if (process.env[requiredEnvVar]){
availableEnvVarsArray.push(requiredEnvVar)
2016-08-04 14:09:39 +00:00
keyValueObjectArrayArg.push({
key: requiredEnvVar,
value: process.env[requiredEnvVar]
2017-01-21 10:51:30 +00:00
})
} else if (envYml.hasOwnProperty(requiredEnvVar)){
process.env[requiredEnvVar] = envYml[requiredEnvVar]
availableEnvVarsArray.push(requiredEnvVar)
2016-08-04 14:09:39 +00:00
keyValueObjectArrayArg.push({
key: requiredEnvVar,
value: process.env[requiredEnvVar]
2017-01-21 10:51:30 +00:00
})
2016-06-19 18:49:13 +00:00
}
2016-08-04 14:09:39 +00:00
};
2017-01-21 10:51:30 +00:00
}
2016-06-20 05:06:15 +00:00
2017-01-21 10:51:30 +00:00
let getMissingEnvVars = (requiredEnvVarsArray: string[],availableEnvVarsArray: string[]) => {
return plugins.lodash.difference(requiredEnvVarsArray,availableEnvVarsArray)
}