add keyValueObjectArray

This commit is contained in:
Philipp Kunz 2016-08-04 16:09:39 +02:00
parent d7a0541d81
commit 11a2cf83ee
7 changed files with 94 additions and 65 deletions

5
dist/index.d.ts vendored
View File

@ -1,6 +1,11 @@
export interface keyValueObject {
key: string;
value: string;
}
export declare class Qenv { export declare class Qenv {
requiredEnvVars: string[]; requiredEnvVars: string[];
availableEnvVars: string[]; availableEnvVars: string[];
missingEnvVars: string[]; missingEnvVars: string[];
keyValueObjectArray: keyValueObject[];
constructor(basePathArg: string, envYmlPathArg: any, failOnMissing?: boolean); constructor(basePathArg: string, envYmlPathArg: any, failOnMissing?: boolean);
} }

63
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@ -4,5 +4,4 @@ exports.beautylog = require("beautylog");
exports.lodash = require("lodash"); exports.lodash = require("lodash");
exports.path = require("path"); exports.path = require("path");
exports.smartfile = require("smartfile"); exports.smartfile = require("smartfile");
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicWVudi5wbHVnaW5zLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvcWVudi5wbHVnaW5zLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxRQUFPLGdCQUFnQixDQUFDLENBQUE7QUFDVixpQkFBUyxXQUFXLFdBQVcsQ0FBQyxDQUFDO0FBQ3BDLGNBQU0sR0FBRyxPQUFPLENBQUMsUUFBUSxDQUFDLENBQUM7QUFDeEIsWUFBSSxXQUFXLE1BQU0sQ0FBQyxDQUFDO0FBQ3ZCLGlCQUFTLFdBQVcsV0FBVyxDQUFDLENBQUMifQ==
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInFlbnYucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsUUFBTyxnQkFBZ0IsQ0FBQyxDQUFBO0FBQ1YsaUJBQVMsV0FBVyxXQUFXLENBQUMsQ0FBQztBQUNwQyxjQUFNLEdBQUcsT0FBTyxDQUFDLFFBQVEsQ0FBQyxDQUFDO0FBQ3hCLFlBQUksV0FBVyxNQUFNLENBQUMsQ0FBQztBQUN2QixpQkFBUyxXQUFXLFdBQVcsQ0FBQyxDQUFDIiwiZmlsZSI6InFlbnYucGx1Z2lucy5qcyIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCBcInR5cGluZ3MtZ2xvYmFsXCI7XG5leHBvcnQgaW1wb3J0IGJlYXV0eWxvZyA9IHJlcXVpcmUoXCJiZWF1dHlsb2dcIik7XG5leHBvcnQgbGV0IGxvZGFzaCA9IHJlcXVpcmUoXCJsb2Rhc2hcIik7XG5leHBvcnQgaW1wb3J0IHBhdGggPSByZXF1aXJlKFwicGF0aFwiKTtcbmV4cG9ydCBpbXBvcnQgc21hcnRmaWxlID0gcmVxdWlyZShcInNtYXJ0ZmlsZVwiKTtcbiJdfQ==

View File

@ -23,15 +23,15 @@
}, },
"homepage": "https://gitlab.com/pushrocks/qenv#README", "homepage": "https://gitlab.com/pushrocks/qenv#README",
"devDependencies": { "devDependencies": {
"npmts-g": "^5.2.6", "npmts-g": "^5.2.8",
"should": "^9.0.2", "should": "^10.0.0",
"typings-test": "^1.0.1" "typings-test": "^1.0.1"
}, },
"dependencies": { "dependencies": {
"beautylog": "^5.0.12", "beautylog": "^5.0.20",
"lodash": "^4.13.1", "lodash": "^4.14.1",
"q": "^1.4.1", "q": "^1.4.1",
"smartfile": "^3.0.10", "smartfile": "^4.0.13",
"typings-global": "^1.0.3" "typings-global": "^1.0.6"
} }
} }

View File

@ -1,13 +1,13 @@
"use strict"; "use strict";
require("typings-test"); require("typings-test");
var path = require("path"); const path = require("path");
require("should"); require("should");
var index_1 = require("../dist/index"); const index_1 = require("../dist/index");
process.cwd = function () { process.cwd = () => {
return path.join(__dirname, "./assets/"); return path.join(__dirname, "./assets/");
}; };
process.env["key1"] = "original"; process.env["key1"] = "original";
var qenvTestObject; let qenvTestObject;
describe("Qenv class", function () { describe("Qenv class", function () {
it("should create a new class", function () { it("should create a new class", function () {
qenvTestObject = new index_1.Qenv(process.cwd(), process.cwd(), false); qenvTestObject = new index_1.Qenv(process.cwd(), process.cwd(), false);
@ -18,6 +18,9 @@ describe("Qenv class", function () {
it("key2 should be read from Yml", function () { it("key2 should be read from Yml", function () {
process.env.key2.should.equal("fromYml"); process.env.key2.should.equal("fromYml");
}); });
it("keyValueObjectArray should hold all retrieved values", function () {
qenvTestObject.keyValueObjectArray[0].value.should.equal("original");
qenvTestObject.keyValueObjectArray[1].value.should.equal("fromYml");
});
}); });
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyxDQUFDLENBQUE7QUFDdEIsTUFBWSxJQUFJLFdBQU0sTUFBTSxDQUFDLENBQUE7QUFDN0IsUUFBTyxRQUFRLENBQUMsQ0FBQTtBQUNoQix3QkFBbUIsZUFBZSxDQUFDLENBQUE7QUFFbkMsT0FBTyxDQUFDLEdBQUcsR0FBRztJQUNWLE1BQU0sQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLFNBQVMsRUFBQyxXQUFXLENBQUMsQ0FBQztBQUM1QyxDQUFDLENBQUE7QUFFRCxPQUFPLENBQUMsR0FBRyxDQUFDLE1BQU0sQ0FBQyxHQUFHLFVBQVUsQ0FBQTtBQUVoQyxJQUFJLGNBQW1CLENBQUM7QUFDeEIsUUFBUSxDQUFDLFlBQVksRUFBQztJQUNsQixFQUFFLENBQUMsMkJBQTJCLEVBQUM7UUFDM0IsY0FBYyxHQUFHLElBQUksWUFBSSxDQUFDLE9BQU8sQ0FBQyxHQUFHLEVBQUUsRUFBQyxPQUFPLENBQUMsR0FBRyxFQUFFLEVBQUMsS0F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JcblxubGV0IHFlbnZUZXN0T2JqZWN0OlFlbnY7XG5kZXNjcmliZShcIlFlbnYgY2xhc3NcIixmdW5jdGlvbigpe1xuICAgIGl0KFwic2hvdWxkIGNyZWF0ZSBhIG5ldyBjbGFzc1wiLGZ1bmN0aW9uKCl7XG4gICAgICAgIHFlbnZUZXN0T2JqZWN0ID0gbmV3IFFlbnYocHJvY2Vzcy5jd2QoKSxwcm9jZXNzLmN3ZCgpLGZhbHNlKTtcbiAgICB9KTtcbiAgICBpdChcImtleTEgc2hvdWxkIGJlIG5vdCBiZSBvdmVyd3JpdHRlbiBzaW5jZSBpdCBpcyBhbHJlYWR5IHByZXNlbnRcIixmdW5jdGlvbigpe1xuICAgICAgICBwcm9jZXNzLmVudi5rZXkxLnNob3VsZC5lcXVhbChcIm9yaWdpbmFsXCIpO1xuICAgIH0pO1xuICAgIGl0KFwia2V5MiBzaG91bGQgYmUgcmVhZCBmcm9tIFltbFwiLGZ1bmN0aW9uKCl7XG4gICAgICAgIHByb2Nlc3MuZW52LmtleTIuc2hvdWxkLmVxdWFsKFwiZnJvbVltbFwiKTtcbiAgICB9KVxuXG59KTsiXX0=

View File

@ -19,6 +19,10 @@ describe("Qenv class",function(){
}); });
it("key2 should be read from Yml",function(){ it("key2 should be read from Yml",function(){
process.env.key2.should.equal("fromYml"); process.env.key2.should.equal("fromYml");
});
it("keyValueObjectArray should hold all retrieved values",function(){
qenvTestObject.keyValueObjectArray[0].value.should.equal("original");
qenvTestObject.keyValueObjectArray[1].value.should.equal("fromYml");
}) })
}); });

View File

@ -1,12 +1,18 @@
import * as plugins from "./qenv.plugins"; import * as plugins from "./qenv.plugins";
export interface keyValueObject {
key: string,
value: string
};
export class Qenv { export class Qenv {
requiredEnvVars:string[]; requiredEnvVars:string[] = [];
availableEnvVars:string[]; availableEnvVars:string[] = [];
missingEnvVars:string[]; missingEnvVars:string[] = [];
keyValueObjectArray:keyValueObject[] = [];
constructor(basePathArg = process.cwd(),envYmlPathArg,failOnMissing = true){ constructor(basePathArg = process.cwd(),envYmlPathArg,failOnMissing = true){
this.requiredEnvVars = getRequiredEnvVars(basePathArg); getRequiredEnvVars(basePathArg,this.requiredEnvVars);
this.availableEnvVars = getAvailableEnvVars(this.requiredEnvVars,envYmlPathArg); getAvailableEnvVars(this.requiredEnvVars,envYmlPathArg,this.availableEnvVars,this.keyValueObjectArray);
this.missingEnvVars = getMissingEnvVars(this.requiredEnvVars,this.availableEnvVars); this.missingEnvVars = getMissingEnvVars(this.requiredEnvVars,this.availableEnvVars);
//handle missing variables //handle missing variables
@ -20,42 +26,47 @@ export class Qenv {
process.exit(1); process.exit(1);
} }
} }
} };
}; };
let getRequiredEnvVars = (pathArg:string):string[] => { let getRequiredEnvVars = (pathArg:string, requiredEnvVarsArray:string[]) => {
let result:string[] = [];
let qenvFilePath = plugins.path.join(pathArg,"qenv.yml"); let qenvFilePath = plugins.path.join(pathArg,"qenv.yml");
let qenvFile = plugins.smartfile.local.toObjectSync(qenvFilePath); let qenvFile = plugins.smartfile.fs.toObjectSync(qenvFilePath);
for(let keyArg in qenvFile.vars){ for(let keyArg in qenvFile.vars){
result.push(qenvFile.vars[keyArg]); requiredEnvVarsArray.push(qenvFile.vars[keyArg]);
} };
return result;
} }
let getAvailableEnvVars = (requiredEnvVarsArg:string[],envYmlPathArg:string):string[] => {
let result = [];
let getAvailableEnvVars = (requiredEnvVarsArg:string[],envYmlPathArg:string,availableEnvVarsArray:string[],keyValueObjectArrayArg:keyValueObject[]) => {
envYmlPathArg = plugins.path.join(envYmlPathArg,"env.yml") envYmlPathArg = plugins.path.join(envYmlPathArg,"env.yml")
let envYml; let envYml;
try { try {
envYml = plugins.smartfile.local.toObjectSync(envYmlPathArg); envYml = plugins.smartfile.fs.toObjectSync(envYmlPathArg);
} }
catch(err){ catch(err){
plugins.beautylog.log("env file couldn't be found at " + envYmlPathArg) plugins.beautylog.log("env file couldn't be found at " + envYmlPathArg)
envYml = {}; envYml = {};
} }
for(let keyArg in requiredEnvVarsArg){ for(let requiredEnvVar of requiredEnvVarsArg){
let requiredEnvVar:string = requiredEnvVarsArg[keyArg];
if(process.env[requiredEnvVar]){ if(process.env[requiredEnvVar]){
result.push(requiredEnvVar); availableEnvVarsArray.push(requiredEnvVar);
keyValueObjectArrayArg.push({
key: requiredEnvVar,
value: process.env[requiredEnvVar]
});
} else if(envYml.hasOwnProperty(requiredEnvVar)){ } else if(envYml.hasOwnProperty(requiredEnvVar)){
process.env[requiredEnvVar] = envYml[requiredEnvVar]; process.env[requiredEnvVar] = envYml[requiredEnvVar];
result.push(requiredEnvVar); availableEnvVarsArray.push(requiredEnvVar);
keyValueObjectArrayArg.push({
key: requiredEnvVar,
value: process.env[requiredEnvVar]
});
} }
} };
return result; };
}
let getMissingEnvVars = (requiredEnvVarsArray:string[],availableEnvVarsArray:string[]) => { let getMissingEnvVars = (requiredEnvVarsArray:string[],availableEnvVarsArray:string[]) => {
return plugins.lodash.difference(requiredEnvVarsArray,availableEnvVarsArray); return plugins.lodash.difference(requiredEnvVarsArray,availableEnvVarsArray);
} };