@cheevr/config
Advanced tools
Comparing version 1.1.3 to 1.2.0
43
index.js
@@ -6,2 +6,3 @@ const args = require('minimist')(process.argv.slice(2)); | ||
const path = require('path'); | ||
const yaml = require('yaml'); | ||
@@ -32,3 +33,3 @@ | ||
* | ||
* @fires {Config} change | ||
* @fires Config#Config:change | ||
*/ | ||
@@ -56,8 +57,8 @@ class Config extends EventEmitter { | ||
if (file.startsWith(this.default)) { | ||
defaultConfig = require(fullPath); | ||
defaultConfig = this._readFile(fullPath); | ||
this._tier = this._tier || path.parse(file).name; | ||
} else if (file.startsWith(this.override)) { | ||
overrideConfig = require(fullPath); | ||
overrideConfig = this._readFile(fullPath); | ||
} else if (file.startsWith(tier)) { | ||
tierConfig = require(fullPath); | ||
tierConfig = this._readFile(fullPath); | ||
this._tier = path.parse(file).name; | ||
@@ -79,3 +80,29 @@ } else { | ||
} | ||
/** | ||
* Reads a file and return the content as json object. Supports .js, JSON and YAML. | ||
* @param {String} fullPath | ||
* @returns {object} | ||
* @private | ||
*/ | ||
_readFile(fullPath) { | ||
let ext = path.extname(fullPath); | ||
switch (ext) { | ||
case '.js': | ||
case '.json': | ||
return require(fullPath); | ||
case '.yml': | ||
case '.yaml': | ||
return yaml.parse(fs.readFileSync(fullPath, 'utf8')); | ||
default: | ||
throw new Error('Unknown config file format'); | ||
} | ||
} | ||
/** | ||
* Applies all configuration in the given object to this instance of Config, so that they can be used directly via | ||
* properties (e.g. config.myprop). | ||
* @param {object} config | ||
* @private | ||
*/ | ||
_apply(config) { | ||
@@ -190,6 +217,6 @@ for (let prop in config) { | ||
let ext = path.extname(file); | ||
if (ext === '.js' || ext === '.json') { | ||
if (ext === '.js' || ext === '.json' || ext === '.yml' || ext === '.yaml') { | ||
let fullPath = path.join(dir, file); | ||
let name = path.basename(file, ext); | ||
let result = name.split('.').reduceRight((prev, curr) => ({[curr]: prev}), require(fullPath)); | ||
let result = name.split('.').reduceRight((prev, curr) => ({[curr]: prev}), this._readFile(fullPath)); | ||
this._apply(result); | ||
@@ -202,3 +229,3 @@ this._sources.push(fullPath); | ||
let name = path.basename(config, ext); | ||
let result = name.split('.').reduceRight((prev, curr) => ({[curr]: prev}), require(dir)); | ||
let result = name.split('.').reduceRight((prev, curr) => ({[curr]: prev}), this._readFile(dir)); | ||
this._apply(result); | ||
@@ -205,0 +232,0 @@ this._sources.push(dir); |
{ | ||
"name": "@cheevr/config", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"description": "A module that reads configuration files from disks", | ||
@@ -27,9 +27,10 @@ "main": "index.js", | ||
"lodash": "^4", | ||
"minimist": "^1" | ||
"minimist": "^1", | ||
"yaml": "^1" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4", | ||
"coveralls": "^2", | ||
"mocha": "^3", | ||
"nyc": "^11" | ||
"coveralls": "^3", | ||
"mocha": "^5", | ||
"nyc": "^13" | ||
}, | ||
@@ -36,0 +37,0 @@ "scripts": { |
@@ -1,2 +0,2 @@ | ||
# Cheevr-Config | ||
# Config | ||
[![npm version](https://badge.fury.io/js/%40cheevr%2Fconfig.svg)](https://badge.fury.io/js/%40cheevr%2Fconfig) | ||
@@ -17,2 +17,3 @@ [![Build Status](https://travis-ci.org/Cheevr/Config.svg?branch=master)](https://travis-ci.org/Cheevr/Config) | ||
on the tier you're running your software (see examples). | ||
The library supports reading configurations from .js, .json and .yaml files transparently. | ||
@@ -223,4 +224,3 @@ | ||
* Support for splitting up config files not just module configurations | ||
* YAML support | ||
* Setters for ```dir```, ```override``` and ```default``` | ||
* Wrap with a Proxy so that setting properties can fire an event on change |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
30814
240
8
3
+ Addedyaml@^1
+ Addedyaml@1.10.2(transitive)