From 0a4bad1ad0df5fb3b0f07be52c248d6e1358522c Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Fri, 12 May 2017 18:17:22 +0200 Subject: [PATCH] refactor code --- dist/index.d.ts | 12 +----- dist/index.js | 68 +++------------------------------ dist/qenv.classes.qenv.d.ts | 12 ++++++ dist/qenv.classes.qenv.js | 30 +++++++++++++++ dist/qenv.helpers.d.ts | 4 ++ dist/qenv.helpers.js | 42 +++++++++++++++++++++ ts/index.ts | 75 +------------------------------------ ts/qenv.classes.qenv.ts | 35 +++++++++++++++++ ts/qenv.helpers.ts | 46 +++++++++++++++++++++++ 9 files changed, 176 insertions(+), 148 deletions(-) create mode 100644 dist/qenv.classes.qenv.d.ts create mode 100644 dist/qenv.classes.qenv.js create mode 100644 dist/qenv.helpers.d.ts create mode 100644 dist/qenv.helpers.js create mode 100644 ts/qenv.classes.qenv.ts create mode 100644 ts/qenv.helpers.ts diff --git a/dist/index.d.ts b/dist/index.d.ts index c750f18..8acb947 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -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'; diff --git a/dist/index.js b/dist/index.js index 562b1e2..b78e5ac 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbm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W9CLEVBQUUscUJBQXFCLENBQUMsQ0FBQTtBQUMvRSxDQUFDLENBQUEifQ== \ No newline at end of file +Object.defineProperty(exports, "__esModule", { value: true }); +__export(require("./qenv.classes.qenv")); +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLHlDQUFtQyJ9 \ No newline at end of file diff --git a/dist/qenv.classes.qenv.d.ts b/dist/qenv.classes.qenv.d.ts new file mode 100644 index 0000000..aee0d50 --- /dev/null +++ b/dist/qenv.classes.qenv.d.ts @@ -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; +} diff --git a/dist/qenv.classes.qenv.js b/dist/qenv.classes.qenv.js new file mode 100644 index 0000000..1e69441 --- /dev/null +++ b/dist/qenv.classes.qenv.js @@ -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= \ No newline at end of file diff --git a/dist/qenv.helpers.d.ts b/dist/qenv.helpers.d.ts new file mode 100644 index 0000000..c4ae803 --- /dev/null +++ b/dist/qenv.helpers.d.ts @@ -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; diff --git a/dist/qenv.helpers.js b/dist/qenv.helpers.js new file mode 100644 index 0000000..278fa73 --- /dev/null +++ b/dist/qenv.helpers.js @@ -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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicWVudi5oZWxwZXJzLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvcWVudi5oZWxwZXJzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsMENBQXlDO0FBRzlCLFFBQUEsa0JBQWtCLEdBQUcsQ0FBQyxPQUFlLEVBQUUsb0JBQThCO0lBQzlFLElBQUksWUFBWSxHQUFHLE9BQU8sQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLE9BQU8sRUFBRSxVQUFVLENBQUMsQ0FBQTtJQUN6RCxJQUFJLFFBQVEsR0FBRyxPQUFPLENBQUMsU0FBUyxDQ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kFDMUIsR0FBRyxFQUFFLGNBQWM7Z0JBQ25CLEtBQUssRUFBRSxPQUFPLENBQUMsR0FBRyxDQUFFLGNBQWMsQ0FBRTthQUNyQyxDQUFDLENBQUE7UUFDSixDQUFDO1FBQUMsSUFBSSxDQUFDLEVBQUUsQ0FBQyxDQUFDLE1BQU0sQ0FBQyxjQUFjLENBQUMsY0FBYyxDQUFDLENBQUMsQ0FBQyxDQUFDO1lBQ2pELE9BQU8sQ0FBQyxHQUFHLENBQUUsY0FBYyxDQUFFLEdBQUcsTUFBTSxDQUFFLGNBQWMsQ0FBRSxDQUFBO1lBQ3hELHFCQUFxQixDQUFDLElBQUksQ0FBQyxjQUFjLENBQUMsQ0FBQTtZQUMxQyxzQkFBc0IsQ0FBQyxJQUFJLENBQUM7Z0JBQzFCLEdBQUcsRUFBRSxjQUFjO2dCQUNuQixLQUFLLEVBQUUsT0FBTyxDQUFDLEdBQUcsQ0FBRSxjQUFjLENBQUU7YUFDckMsQ0FBQyxDQUFBO1FBQ0osQ0FBQztJQUNILENBQUM7QUFDSCxDQUFDLENBQUE7QUFFVSxRQUFBLGlCQUFpQixHQUFHLENBQUMsb0JBQThCLEVBQUUscUJBQStCO0lBQzdGLE1BQU0sQ0FBQyxPQUFPLENBQUMsTUFBTSxDQUFDLFVBQVUsQ0FBQyxvQkFBb0IsRUFBRSxxQkFBcUIsQ0FBQyxDQUFBO0FBQy9FLENBQUMsQ0FBQSJ9 \ No newline at end of file diff --git a/ts/index.ts b/ts/index.ts index 57e799c..6932278 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1,74 +1 @@ -import * as plugins from './qenv.plugins' - -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) { - 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' diff --git a/ts/qenv.classes.qenv.ts b/ts/qenv.classes.qenv.ts new file mode 100644 index 0000000..5e2ad9a --- /dev/null +++ b/ts/qenv.classes.qenv.ts @@ -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] + } +} diff --git a/ts/qenv.helpers.ts b/ts/qenv.helpers.ts new file mode 100644 index 0000000..b7d7934 --- /dev/null +++ b/ts/qenv.helpers.ts @@ -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) +}