qenv/ts/index.ts

72 lines
2.6 KiB
TypeScript
Raw Normal View History

2016-06-19 18:08:34 +00:00
import * as plugins from "./qenv.plugins";
2016-08-04 14:09:39 +00:00
export interface keyValueObject {
key: string,
value: string
};
2016-06-20 08:45:43 +00:00
export class Qenv {
2016-08-04 14:09:39 +00:00
requiredEnvVars:string[] = [];
availableEnvVars:string[] = [];
missingEnvVars:string[] = [];
keyValueObjectArray:keyValueObject[] = [];
2016-06-21 14:13:29 +00:00
constructor(basePathArg = process.cwd(),envYmlPathArg,failOnMissing = true){
2016-08-04 14:09:39 +00:00
getRequiredEnvVars(basePathArg,this.requiredEnvVars);
getAvailableEnvVars(this.requiredEnvVars,envYmlPathArg,this.availableEnvVars,this.keyValueObjectArray);
2016-06-20 05:06:15 +00:00
this.missingEnvVars = getMissingEnvVars(this.requiredEnvVars,this.availableEnvVars);
2016-06-21 14:13:29 +00:00
//handle missing variables
if (this.missingEnvVars.length > 0){
2016-06-21 14:25:03 +00:00
plugins.beautylog.info("Required Env Vars are:")
console.log(this.requiredEnvVars);
plugins.beautylog.error("However some Env variables could not be resolved:");
2016-06-21 14:13:29 +00:00
console.log(this.missingEnvVars);
if(failOnMissing){
plugins.beautylog.error("Exiting!")
process.exit(1);
}
2016-06-20 08:45:43 +00:00
}
2016-08-04 14:09:39 +00:00
};
2016-06-19 18:08:34 +00:00
};
2016-08-04 14:09:39 +00:00
let getRequiredEnvVars = (pathArg:string, requiredEnvVarsArray:string[]) => {
2016-06-19 18:08:34 +00:00
let qenvFilePath = plugins.path.join(pathArg,"qenv.yml");
2016-08-04 14:09:39 +00:00
let qenvFile = plugins.smartfile.fs.toObjectSync(qenvFilePath);
2016-06-19 18:08:34 +00:00
for(let keyArg in qenvFile.vars){
2016-08-04 14:09:39 +00:00
requiredEnvVarsArray.push(qenvFile.vars[keyArg]);
};
2016-06-19 18:08:34 +00:00
}
2016-08-04 14:09:39 +00:00
let getAvailableEnvVars = (requiredEnvVarsArg:string[],envYmlPathArg:string,availableEnvVarsArray:string[],keyValueObjectArrayArg:keyValueObject[]) => {
2016-06-20 08:45:43 +00:00
envYmlPathArg = plugins.path.join(envYmlPathArg,"env.yml")
2016-06-20 05:06:15 +00:00
let envYml;
try {
2016-08-04 14:09:39 +00:00
envYml = plugins.smartfile.fs.toObjectSync(envYmlPathArg);
2016-06-20 05:06:15 +00:00
}
catch(err){
2016-06-21 14:13:29 +00:00
plugins.beautylog.log("env file couldn't be found at " + envYmlPathArg)
2016-06-20 05:06:15 +00:00
envYml = {};
}
2016-08-04 14:09:39 +00:00
for(let requiredEnvVar of requiredEnvVarsArg){
2016-06-20 08:45:43 +00:00
if(process.env[requiredEnvVar]){
2016-08-04 14:09:39 +00:00
availableEnvVarsArray.push(requiredEnvVar);
keyValueObjectArrayArg.push({
key: requiredEnvVar,
value: process.env[requiredEnvVar]
});
2016-06-20 08:45:43 +00:00
} else if(envYml.hasOwnProperty(requiredEnvVar)){
process.env[requiredEnvVar] = envYml[requiredEnvVar];
2016-08-04 14:09:39 +00:00
availableEnvVarsArray.push(requiredEnvVar);
keyValueObjectArrayArg.push({
key: requiredEnvVar,
value: process.env[requiredEnvVar]
});
2016-06-19 18:49:13 +00:00
}
2016-08-04 14:09:39 +00:00
};
};
2016-06-20 05:06:15 +00:00
let getMissingEnvVars = (requiredEnvVarsArray:string[],availableEnvVarsArray:string[]) => {
return plugins.lodash.difference(requiredEnvVarsArray,availableEnvVarsArray);
2016-08-04 14:09:39 +00:00
};