qenv/dist/index.js
2016-06-21 16:13:29 +02:00

61 lines
8.1 KiB
JavaScript

"use strict";
var plugins = require("./qenv.plugins");
var Qenv = (function () {
function Qenv(basePathArg, envYmlPathArg, failOnMissing) {
if (basePathArg === void 0) { basePathArg = process.cwd(); }
if (failOnMissing === void 0) { failOnMissing = true; }
this.requiredEnvVars = getRequiredEnvVars(basePathArg);
this.availableEnvVars = getAvailableEnvVars(this.requiredEnvVars, envYmlPathArg);
this.missingEnvVars = getMissingEnvVars(this.requiredEnvVars, this.availableEnvVars);
//handle missing variables
if (this.missingEnvVars.length > 0) {
plugins.beautylog.error("Some Env variables could not be resolved:");
console.log(this.missingEnvVars);
if (failOnMissing) {
plugins.beautylog.error("Exiting!");
process.exit(1);
}
}
}
return Qenv;
}());
exports.Qenv = Qenv;
;
var getRequiredEnvVars = function (pathArg) {
var result = [];
var qenvFilePath = plugins.path.join(pathArg, "qenv.yml");
var qenvFile = plugins.smartfile.local.toObjectSync(qenvFilePath);
for (var keyArg in qenvFile.vars) {
result.push(qenvFile.vars[keyArg]);
}
return result;
};
var getAvailableEnvVars = function (requiredEnvVarsArg, envYmlPathArg) {
var result = [];
envYmlPathArg = plugins.path.join(envYmlPathArg, "env.yml");
var envYml;
try {
envYml = plugins.smartfile.local.toObjectSync(envYmlPathArg);
}
catch (err) {
plugins.beautylog.log("env file couldn't be found at " + envYmlPathArg);
envYml = {};
}
for (var keyArg in requiredEnvVarsArg) {
var requiredEnvVar = requiredEnvVarsArg[keyArg];
if (process.env[requiredEnvVar]) {
result.push(requiredEnvVar);
}
else if (envYml.hasOwnProperty(requiredEnvVar)) {
process.env[requiredEnvVar] = envYml[requiredEnvVar];
result.push(requiredEnvVar);
}
}
return result;
};
var getMissingEnvVars = function (requiredEnvVarsArray, availableEnvVarsArray) {
return plugins.lodash.difference(requiredEnvVarsArray, availableEnvVarsArray);
};
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImluZ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