refactor code

This commit is contained in:
Philipp Kunz 2017-05-12 18:17:22 +02:00
parent e34d37da19
commit 0a4bad1ad0
9 changed files with 176 additions and 148 deletions

12
dist/index.d.ts vendored
View File

@ -1,11 +1 @@
export interface IKeyValueObject {
key: string;
value: string;
}
export declare class Qenv {
requiredEnvVars: string[];
availableEnvVars: string[];
missingEnvVars: string[];
keyValueObjectArray: IKeyValueObject[];
constructor(basePathArg: string, envYmlPathArg: any, failOnMissing?: boolean);
}
export * from './qenv.classes.qenv';

68
dist/index.js vendored
View File

@ -1,65 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
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);
this.missingEnvVars = getMissingEnvVars(this.requiredEnvVars, this.availableEnvVars);
// handle missing variables
if (this.missingEnvVars.length > 0) {
console.info('Required Env Vars are:');
console.log(this.requiredEnvVars);
console.error('However some Env variables could not be resolved:');
console.log(this.missingEnvVars);
if (failOnMissing) {
console.error('Exiting!');
process.exit(1);
}
}
}
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
exports.Qenv = Qenv;
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]);
}
};
let getAvailableEnvVars = (requiredEnvVarsArg, envYmlPathArg, availableEnvVarsArray, keyValueObjectArrayArg) => {
envYmlPathArg = plugins.path.join(envYmlPathArg, 'env.yml');
let envYml;
try {
envYml = plugins.smartfile.fs.toObjectSync(envYmlPathArg);
}
catch (err) {
console.log("env file couldn't be found at " + envYmlPathArg);
envYml = {};
}
for (let requiredEnvVar of requiredEnvVarsArg) {
if (process.env[requiredEnvVar]) {
availableEnvVarsArray.push(requiredEnvVar);
keyValueObjectArrayArg.push({
key: requiredEnvVar,
value: process.env[requiredEnvVar]
});
}
else if (envYml.hasOwnProperty(requiredEnvVar)) {
process.env[requiredEnvVar] = envYml[requiredEnvVar];
availableEnvVarsArray.push(requiredEnvVar);
keyValueObjectArrayArg.push({
key: requiredEnvVar,
value: process.env[requiredEnvVar]
});
}
}
};
let getMissingEnvVars = (requiredEnvVarsArray, availableEnvVarsArray) => {
return plugins.lodash.difference(requiredEnvVarsArray, availableEnvVarsArray);
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLDBDQUF5QztBQU96QztJQUtFLFlBQWEsV0FBVyxHQUFHLE9BQU8sQ0FBQyxHQUFHLEVBQUUsRUFBRSxhQUFhLEVBQUUsYUFBYSxHQUFHLElBQUk7UUFKN0Usb0JBQWUsR0FBYSxFQUFFLENBQUE7UUFDOUIscUJBQWdCLEdBQWEsRUFBRSxDQUFBO1FBQy9CLG1CQUFjLEdBQWEsRUFBRSxDQUFBO1FBQzdCLHdCQUFtQixHQUFzQixFQUFFLENBQUE7UUFFekMsa0JBQWtCLENBQUMsV0FBVyxFQUFFLElBQUksQ0FBQyxlQUFlLENBQUMsQ0FBQTtRQUNyRCxtQkFBbUIsQ0FBQyxJQUFJLENBQUMsZUFBZSxFQUFFLGFBQWEsRUFBRSxJQUFJLENBQUMsZ0JBQWdCLEVBQUUsSUFBSSxDQUFDLG1CQUFtQixDQUFDLENBQUE7UUFDekcsSUFBSSxDQUFDLGNBQWMsR0FBRyxpQkFBaUIsQ0FB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QUMsY0FBYyxDQUFDLGNBQWMsQ0FBQyxDQUFDLENBQUMsQ0FBQztZQUNqRCxPQUFPLENBQUMsR0FBRyxDQUFFLGNBQWMsQ0FBRSxHQUFHLE1BQU0sQ0FBRSxjQUFjLENBQUUsQ0FBQTtZQUN4RCxxQkFBcUIsQ0FBQyxJQUFJLENBQUMsY0FBYyxDQUFDLENBQUE7WUFDMUMsc0JBQXNCLENBQUMsSUFBSSxDQUFDO2dCQUMxQixHQUFHLEVBQUUsY0FBYztnQkFDbkIsS0FBSyxFQUFFLE9BQU8sQ0FBQyxHQUFHLENBQUUsY0FBYyxDQUFFO2FBQ3JDLENBQUMsQ0FBQTtRQUNKLENBQUM7SUFDSCxDQUFDO0FBQ0gsQ0FBQyxDQUFBO0FBRUQsSUFBSSxpQkFBaUIsR0FBRyxDQUFDLG9CQUE4QixFQUFFLHFCQUErQjtJQUN0RixNQUFNLENBQUMsT0FBTyxDQUFDLE1BQU0sQ0FBQyxVQUFVLENBQUMsb0JBQW9CLEVBQUUscUJBQXFCLENBQUMsQ0FBQTtBQUMvRSxDQUFDLENBQUEifQ==
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./qenv.classes.qenv"));
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLHlDQUFtQyJ9

12
dist/qenv.classes.qenv.d.ts vendored Normal file
View File

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

30
dist/qenv.classes.qenv.js vendored Normal file
View File

@ -0,0 +1,30 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const helpers = require("./qenv.helpers");
class Qenv {
constructor(basePathArg = process.cwd(), envYmlPathArg, failOnMissing = true) {
this.requiredEnvVars = [];
this.availableEnvVars = [];
this.missingEnvVars = [];
this.keyValueObjectArray = [];
helpers.getRequiredEnvVars(basePathArg, this.requiredEnvVars);
helpers.getAvailableEnvVars(this.requiredEnvVars, envYmlPathArg, this.availableEnvVars, this.keyValueObjectArray);
this.missingEnvVars = helpers.getMissingEnvVars(this.requiredEnvVars, this.availableEnvVars);
// handle missing variables
if (this.missingEnvVars.length > 0) {
console.info('Required Env Vars are:');
console.log(this.requiredEnvVars);
console.error('However some Env variables could not be resolved:');
console.log(this.missingEnvVars);
if (failOnMissing) {
console.error('Exiting!');
process.exit(1);
}
}
}
getEnvVar(envVarName) {
return process.env[envVarName];
}
}
exports.Qenv = Qenv;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicWVudi5jbGFzc2VzLnFlbnYuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9xZW52LmNsYXNzZXMucWVudi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUNBLDBDQUF5QztBQU96QztJQUtFLFlBQWEsV0FBVyxHQUFHLE9BQU8sQ0FBQyxHQUFHLEVBQUUsRUFBRSxhQUFhLEVBQUUsYUFBYSxHQUFHLElBQUk7UUFKN0Usb0JBQWUsR0FBYSxFQUFFLENBQUE7UUFDOUIscUJBQWdCLEdBQWEsRUFBRSxDQUFBO1FBQy9CLG1CQUFjLEdBQWEsRUFBRSxDQUFBO1FBQzdCLHdCQUFtQixHQUFzQixFQUFFLENBQUE7UUFFekMsT0FBTyxDQUFDLGtCQUFrQixDQUFDLFdBQVcsRUFBRSxJQUFJLENBQUMsZUFBZSxDQUFDLENBQUE7UUFDN0QsT0FBTyxDQUFDLG1CQUFtQixDQUFDLElBQUksQ0FBQyxlQUFlLEVBQUUsYUFBYSxFQUFFLElBQUksQ0FBQyxnQkFBZ0IsRUFBRSxJQUFJLENBQUMsbUJBQW1CLENBQUMsQ0FBQTtRQUNqSCxJQUFJLENBQUMsY0FBYyxHQUFHLE9BQU8sQ0FBQyxpQkFBaUIsQ0FBQyxJQUFJLENBQUMsZUFBZSxFQUFFLElBQUksQ0FBQyxnQkFBZ0IsQ0FBQyxDQUFBO1FBRTVGLDJCQUEyQjtRQUMzQixFQUFFLENBQUMsQ0FBQyxJQUFJLENBQUMsY0FBYyxDQUFDLE1BQU0sR0FBRyxDQUFDLENBQUMsQ0FBQyxDQUFDO1lBQ25DLE9BQU8sQ0FBQyxJQUFJLENBQUMsd0JBQXdCLENBQUMsQ0FBQTtZQUN0QyxPQUFPLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxlQUFlLENBQUMsQ0FBQTtZQUNqQyxPQUFPLENBQUMsS0FBSyxDQUFDLG1EQUFtRCxDQUFDLENBQUE7WUFDbEUsT0FBTyxDQUFDLEdBQUcsQ0FBQyxJQUFJLENBQUMsY0FBYyxDQUFDLENBQUE7WUFDaEMsRUFBRSxDQUFDLENBQUMsYUFBYSxDQUFDLENBQUMsQ0FBQztnQkFDbEIsT0FBTyxDQUFDLEtBQUssQ0FBQyxVQUFVLENBQUMsQ0FBQTtnQkFDekIsT0FBTyxDQUFDLElBQUksQ0FBQyxDQUFDLENBQUMsQ0FBQTtZQUNqQixDQUFDO1FBQ0gsQ0FBQztJQUNILENBQUM7SUFFRCxTQUFTLENBQUUsVUFBVTtRQUNuQixNQUFNLENBQUMsT0FBTyxDQUFDLEdBQUcsQ0FBQyxVQUFVLENBQUMsQ0FBQTtJQUNoQyxDQUFDO0NBQ0Y7QUExQkQsb0JBMEJDIn0=

4
dist/qenv.helpers.d.ts vendored Normal file
View File

@ -0,0 +1,4 @@
import { IKeyValueObject } from './qenv.classes.qenv';
export declare let getRequiredEnvVars: (pathArg: string, requiredEnvVarsArray: string[]) => void;
export declare let getAvailableEnvVars: (requiredEnvVarsArg: string[], envYmlPathArg: string, availableEnvVarsArray: string[], keyValueObjectArrayArg: IKeyValueObject[]) => void;
export declare let getMissingEnvVars: (requiredEnvVarsArray: string[], availableEnvVarsArray: string[]) => any;

42
dist/qenv.helpers.js vendored Normal file
View File

@ -0,0 +1,42 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const plugins = require("./qenv.plugins");
exports.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]);
}
};
exports.getAvailableEnvVars = (requiredEnvVarsArg, envYmlPathArg, availableEnvVarsArray, keyValueObjectArrayArg) => {
envYmlPathArg = plugins.path.join(envYmlPathArg, 'env.yml');
let envYml;
try {
envYml = plugins.smartfile.fs.toObjectSync(envYmlPathArg);
}
catch (err) {
console.log("env file couldn't be found at " + envYmlPathArg);
envYml = {};
}
for (let requiredEnvVar of requiredEnvVarsArg) {
if (process.env[requiredEnvVar]) {
availableEnvVarsArray.push(requiredEnvVar);
keyValueObjectArrayArg.push({
key: requiredEnvVar,
value: process.env[requiredEnvVar]
});
}
else if (envYml.hasOwnProperty(requiredEnvVar)) {
process.env[requiredEnvVar] = envYml[requiredEnvVar];
availableEnvVarsArray.push(requiredEnvVar);
keyValueObjectArrayArg.push({
key: requiredEnvVar,
value: process.env[requiredEnvVar]
});
}
}
};
exports.getMissingEnvVars = (requiredEnvVarsArray, availableEnvVarsArray) => {
return plugins.lodash.difference(requiredEnvVarsArray, availableEnvVarsArray);
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicWVudi5oZWxwZXJzLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvcWVudi5oZWxwZXJzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3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,
value: string
}
export class Qenv {
requiredEnvVars: string[] = []
availableEnvVars: string[] = []
missingEnvVars: string[] = []
keyValueObjectArray: IKeyValueObject[] = []
constructor (basePathArg = process.cwd(), envYmlPathArg, failOnMissing = true) {
getRequiredEnvVars(basePathArg, this.requiredEnvVars)
getAvailableEnvVars(this.requiredEnvVars, envYmlPathArg, this.availableEnvVars, this.keyValueObjectArray)
this.missingEnvVars = getMissingEnvVars(this.requiredEnvVars, this.availableEnvVars)
// handle missing variables
if (this.missingEnvVars.length > 0) {
console.info('Required Env Vars are:')
console.log(this.requiredEnvVars)
console.error('However some Env variables could not be resolved:')
console.log(this.missingEnvVars)
if (failOnMissing) {
console.error('Exiting!')
process.exit(1)
}
}
}
}
let getRequiredEnvVars = (pathArg: string, requiredEnvVarsArray: string[]) => {
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 ])
}
}
let getAvailableEnvVars = (
requiredEnvVarsArg: string[],
envYmlPathArg: string,
availableEnvVarsArray: string[],
keyValueObjectArrayArg: IKeyValueObject[]
) => {
envYmlPathArg = plugins.path.join(envYmlPathArg, 'env.yml')
let envYml
try {
envYml = plugins.smartfile.fs.toObjectSync(envYmlPathArg)
} catch (err) {
console.log("env file couldn't be found at " + envYmlPathArg)
envYml = {}
}
for (let requiredEnvVar of requiredEnvVarsArg) {
if (process.env[ requiredEnvVar ]) {
availableEnvVarsArray.push(requiredEnvVar)
keyValueObjectArrayArg.push({
key: requiredEnvVar,
value: process.env[ requiredEnvVar ]
})
} else if (envYml.hasOwnProperty(requiredEnvVar)) {
process.env[ requiredEnvVar ] = envYml[ requiredEnvVar ]
availableEnvVarsArray.push(requiredEnvVar)
keyValueObjectArrayArg.push({
key: requiredEnvVar,
value: process.env[ requiredEnvVar ]
})
}
}
}
let getMissingEnvVars = (requiredEnvVarsArray: string[], availableEnvVarsArray: string[]) => {
return plugins.lodash.difference(requiredEnvVarsArray, availableEnvVarsArray)
}
export * from './qenv.classes.qenv'

35
ts/qenv.classes.qenv.ts Normal file
View File

@ -0,0 +1,35 @@
import * as plugins from './qenv.plugins'
import * as helpers from './qenv.helpers'
export interface IKeyValueObject {
key: string,
value: string
}
export class Qenv {
requiredEnvVars: string[] = []
availableEnvVars: string[] = []
missingEnvVars: string[] = []
keyValueObjectArray: IKeyValueObject[] = []
constructor (basePathArg = process.cwd(), envYmlPathArg, failOnMissing = true) {
helpers.getRequiredEnvVars(basePathArg, this.requiredEnvVars)
helpers.getAvailableEnvVars(this.requiredEnvVars, envYmlPathArg, this.availableEnvVars, this.keyValueObjectArray)
this.missingEnvVars = helpers.getMissingEnvVars(this.requiredEnvVars, this.availableEnvVars)
// handle missing variables
if (this.missingEnvVars.length > 0) {
console.info('Required Env Vars are:')
console.log(this.requiredEnvVars)
console.error('However some Env variables could not be resolved:')
console.log(this.missingEnvVars)
if (failOnMissing) {
console.error('Exiting!')
process.exit(1)
}
}
}
getEnvVar (envVarName): string {
return process.env[envVarName]
}
}

46
ts/qenv.helpers.ts Normal file
View File

@ -0,0 +1,46 @@
import * as plugins from './qenv.plugins'
import { IKeyValueObject } from './qenv.classes.qenv'
export let getRequiredEnvVars = (pathArg: string, requiredEnvVarsArray: string[]) => {
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 ])
}
}
export let getAvailableEnvVars = (
requiredEnvVarsArg: string[],
envYmlPathArg: string,
availableEnvVarsArray: string[],
keyValueObjectArrayArg: IKeyValueObject[]
) => {
envYmlPathArg = plugins.path.join(envYmlPathArg, 'env.yml')
let envYml
try {
envYml = plugins.smartfile.fs.toObjectSync(envYmlPathArg)
} catch (err) {
console.log("env file couldn't be found at " + envYmlPathArg)
envYml = {}
}
for (let requiredEnvVar of requiredEnvVarsArg) {
if (process.env[ requiredEnvVar ]) {
availableEnvVarsArray.push(requiredEnvVar)
keyValueObjectArrayArg.push({
key: requiredEnvVar,
value: process.env[ requiredEnvVar ]
})
} else if (envYml.hasOwnProperty(requiredEnvVar)) {
process.env[ requiredEnvVar ] = envYml[ requiredEnvVar ]
availableEnvVarsArray.push(requiredEnvVar)
keyValueObjectArrayArg.push({
key: requiredEnvVar,
value: process.env[ requiredEnvVar ]
})
}
}
}
export let getMissingEnvVars = (requiredEnvVarsArray: string[], availableEnvVarsArray: string[]) => {
return plugins.lodash.difference(requiredEnvVarsArray, availableEnvVarsArray)
}