qenv/ts/index.ts

51 lines
1.8 KiB
TypeScript
Raw Normal View History

2016-06-19 18:08:34 +00:00
import * as plugins from "./qenv.plugins";
2016-06-20 08:45:43 +00:00
export class Qenv {
2016-06-20 05:06:15 +00:00
requiredEnvVars:string[];
availableEnvVars:string[];
missingEnvVars:string[];
constructor(basePathArg = process.cwd(),envYmlPathArg){
this.requiredEnvVars = getRequiredEnvVars(basePathArg);
this.availableEnvVars = getAvailableEnvVars(this.requiredEnvVars,envYmlPathArg);
this.missingEnvVars = getMissingEnvVars(this.requiredEnvVars,this.availableEnvVars);
2016-06-20 08:45:43 +00:00
for(let keyArg in this.missingEnvVars){
plugins.beautylog.warn(this.missingEnvVars[keyArg] + " is required, but missing!")
}
2016-06-19 18:08:34 +00:00
}
};
2016-06-20 05:06:15 +00:00
let getRequiredEnvVars = (pathArg:string):string[] => {
2016-06-19 18:08:34 +00:00
let result:string[] = [];
let qenvFilePath = plugins.path.join(pathArg,"qenv.yml");
let qenvFile = plugins.smartfile.local.toObjectSync(qenvFilePath);
for(let keyArg in qenvFile.vars){
result.push(qenvFile.vars[keyArg]);
}
return result;
}
2016-06-20 05:06:15 +00:00
let getAvailableEnvVars = (requiredEnvVarsArg:string[],envYmlPathArg:string):string[] => {
2016-06-19 18:08:34 +00:00
let result = [];
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 {
envYml = plugins.smartfile.local.toObjectSync(envYmlPathArg);
}
catch(err){
envYml = {};
}
2016-06-19 18:49:13 +00:00
for(let keyArg in requiredEnvVarsArg){
2016-06-20 08:45:43 +00:00
let requiredEnvVar:string = requiredEnvVarsArg[keyArg];
if(process.env[requiredEnvVar]){
result.push(requiredEnvVar);
} else if(envYml.hasOwnProperty(requiredEnvVar)){
process.env[requiredEnvVar] = envYml[requiredEnvVar];
result.push(requiredEnvVar);
2016-06-19 18:49:13 +00:00
}
}
2016-06-19 18:08:34 +00:00
return result;
2016-06-20 05:06:15 +00:00
}
let getMissingEnvVars = (requiredEnvVarsArray:string[],availableEnvVarsArray:string[]) => {
return plugins.lodash.difference(requiredEnvVarsArray,availableEnvVarsArray);
2016-06-19 18:08:34 +00:00
}