'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;
|