qenv/dist/index.js

69 lines
6.0 KiB
JavaScript
Raw Permalink Normal View History

2016-06-20 05:06:15 +00:00
"use strict";
2016-08-04 14:09:39 +00:00
const plugins = require("./qenv.plugins");
;
class Qenv {
constructor(basePathArg = process.cwd(), envYmlPathArg, failOnMissing = true) {
this.requiredEnvVars = [];
this.availableEnvVars = [];
this.missingEnvVars = [];
this.keyValueObjectArray = [];
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-06-20 05:06:15 +00:00
}
2016-08-04 14:09:39 +00:00
;
}
2016-06-20 08:45:43 +00:00
exports.Qenv = Qenv;
2016-06-20 05:06:15 +00:00
;
2016-08-04 14:09:39 +00:00
let getRequiredEnvVars = (pathArg, requiredEnvVarsArray) => {
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-06-20 05:06:15 +00:00
}
2016-08-04 14:09:39 +00:00
;
2016-06-20 05:06:15 +00:00
};
2016-08-04 14:09:39 +00:00
let getAvailableEnvVars = (requiredEnvVarsArg, envYmlPathArg, availableEnvVarsArray, keyValueObjectArrayArg) => {
2016-06-20 08:45:43 +00:00
envYmlPathArg = plugins.path.join(envYmlPathArg, "env.yml");
2016-08-04 14:09:39 +00:00
let envYml;
2016-06-20 05:06:15 +00:00
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 05:06:15 +00:00
}
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-20 05:06:15 +00:00
}
}
2016-08-04 14:09:39 +00:00
;
2016-06-20 05:06:15 +00:00
};
2016-08-04 14:09:39 +00:00
let getMissingEnvVars = (requiredEnvVarsArray, availableEnvVarsArray) => {
2016-06-20 05:06:15 +00:00
return plugins.lodash.difference(requiredEnvVarsArray, availableEnvVarsArray);
};
2016-08-04 14:09:39 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsTUFBWSxPQUFPLFdBQU0sZ0JBQWdCLENBQUMsQ0FBQTtBQUt6QyxDQUFDO0FBRUY7SUFLSSxZQUFZLFdBQVcsR0FBRyxPQUFPLENBQUMsR0FBRyxFQUFFLEVBQUMsYUFBYSxFQUFDLGFBQWE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