All files index.js

86.15% Statements 56/65
76.79% Branches 43/56
93.33% Functions 14/15
86.15% Lines 56/65
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152                                  11x 11x 11x 11x   11x 1x 1x           30x 30x 4x   26x         425x 4x 46x     46x     421x 5x   416x         4x       3x 3x   3x 2x 2x 1x           3x 1x   2x         1x 4x   1x 1x 1x     1x           5x 5x                         5x   5x 1010x 606x 606x 5x 3x   2x       404x 404x     404x               1x 2x   1x 1x 2x 2x 1x     1x       6x          
'use strict';
 
import fs from 'fs';
import path from 'path';
 
import {
    get as lodashGet,
    set as lodashSet
} from 'lodash';
 
import {
    weakToJSON,
    guessUpcast
} from './utils';
 
class Cz {
    constructor(options) {
        this._config = new WeakMap();
        this._defaults = new WeakMap();
        this._path = null;
        this._options = new WeakMap();
 
        if (options) {
            Eif ('upcast' in options) {
                this._options.upcast = options.upcast;
            }
        }
    }
 
    get(prop) {
        const wholeObj = Object.assign(weakToJSON(this._defaults), weakToJSON(this._config));
        if (arguments.length === 0) {
            return wholeObj;
        }
        return lodashGet(wholeObj, prop.replace(':', '.'));
    }
 
    set(prop, value) {
        // Handles if prop is key or obj
        if (value === undefined) {
            for (const key of Object.keys(prop)) {
                Iif ('upcast' in this._options && this._options.upcast === true) {
                    lodashSet(this._config, key, guessUpcast(prop[key]));
                } else {
                    lodashSet(this._config, key, prop[key]);
                }
            }
        } else if ('upcast' in this._options && this._options.upcast === true) {
            lodashSet(this._config, prop.replace(':', '.'), guessUpcast(value));
        } else {
            lodashSet(this._config, prop.replace(':', '.'), value);
        }
    }
 
    defaults(obj) {
        this._defaults = obj;
    }
 
    load(newPath) {
        const file = fs.readFileSync(newPath, 'utf-8');
        this._path = newPath;
        // We use 2 as an empty file with a new line would return 1
        if (file.length >= 2) {
            const data = JSON.parse(file);
            for (const prop of Object.keys(data)) {
                this._config[prop] = data[prop];
            }
        }
    }
 
    save(newPath) {
        if (arguments.length === 0 && this._path === null) {
            throw new Error('No path provided.');
        } else {
            fs.writeFileSync(path.normalize(newPath || this._path), JSON.stringify(this._config, null, 4) + '\n', 'utf8');
        }
    }
 
    args(options = {}) {
        const args = process.argv.filter(arg => {
            return arg.startsWith('--') && arg.includes('=');
        });
        args.forEach(arg => {
            arg = arg.slice(2).split('='); // Gets us ['db:host', 'localhost'] from '--db:host=localhost'
            Iif ('upcast' in options && options.upcast === true) {
                this.set(arg[0], guessUpcast(arg[1]));
            } else {
                this.set(arg[0], arg[1]);
            }
        });
    }
 
    env(options = {}) {
        let envs = {};
        Iif (options.caseInsensative) {
            Object.keys(process.env).forEach(key => {
                if (process.env[key.toLowerCase()]) {
                    if (process.env[key.toLowerCase()] === process.env[key]) {
                        envs[key.toLowerCase()] = process.env[key];
                    } else {
                        envs[key] = process.env[key];
                    }
                } else {
                    envs[key.toLowerCase()] = process.env[key];
                }
            });
        } else {
            envs = process.env;
        }
        Object.keys(envs).forEach(key => {
            if (options.allowed) {
                key = key.replace(options.seperator || '__', ':');
                if (options.allowed.includes(key.split(':')[0])) {
                    if ('upcast' in options && options.upcast === true) {
                        this.set(key, guessUpcast(envs[key]));
                    } else {
                        this.set(key, envs[key]);
                    }
                }
            } else {
                key = key.replace(options.seperator || '__', ':');
                Iif ('upcast' in options && options.upcast === true) {
                    this.set(key, guessUpcast(envs[key]));
                } else {
                    this.set(key, envs[key]);
                }
            }
        });
    }
 
    joinGets(gets, joins) {
        // @TODO: Add support for a single join param instead of just array
        const results = gets.map(get => {
            return this.get(get);
        });
        let finalResult = '';
        for (let i = 0; i < gets.length; i++) {
            finalResult += results[i];
            if (i + 1 !== gets.length) {
                finalResult += joins[i];
            }
        }
        return finalResult;
    }
 
    reset() {
        this._config = new WeakMap();
    }
}
 
export default Cz;