easy-configuration
Advanced tools
Comparing version 1.1.3 to 1.2.0
// Generated by CoffeeScript 1.6.3 | ||
(function() { | ||
var EasyConfiguration, Extension, fs, path; | ||
var EasyConfiguration, Extension, merge; | ||
path = require('path'); | ||
merge = require('tea-merge'); | ||
fs = require('fs'); | ||
Extension = require('./Extension'); | ||
@@ -25,3 +23,3 @@ | ||
function EasyConfiguration(fileName) { | ||
this.fileName = path.resolve(fileName); | ||
this.fileName = fileName; | ||
} | ||
@@ -37,3 +35,3 @@ | ||
} | ||
extension.setConfigurator(this); | ||
extension.configurator = this; | ||
this.extensions[name] = extension; | ||
@@ -62,5 +60,2 @@ return this.extensions[name]; | ||
var data; | ||
if (!fs.existsSync(file)) { | ||
throw new Error('Config file ' + file + ' does not exists.'); | ||
} | ||
data = { | ||
@@ -70,3 +65,3 @@ includes: [], | ||
parameters: {}, | ||
data: JSON.parse(fs.readFileSync(file)) | ||
data: require(file) | ||
}; | ||
@@ -108,3 +103,3 @@ if (typeof data.data.includes !== 'undefined') { | ||
file = _ref[_i]; | ||
file = path.resolve(path.dirname(parent), file); | ||
file = this.normalizePath(this.dirName(parent) + '/' + file); | ||
config = this.loadConfig(file); | ||
@@ -133,3 +128,3 @@ data.includes = this.merge(config.includes, data.includes); | ||
} | ||
this.extensions[name].setData(section); | ||
this.extensions[name].data = section; | ||
data[name] = this.extensions[name].loadConfiguration(); | ||
@@ -174,29 +169,28 @@ data[name] = this.expand(data[name]); | ||
EasyConfiguration.prototype.merge = function(left, right) { | ||
var i, name, type, value, _i, _len; | ||
type = Object.prototype.toString; | ||
if (type.call(left) !== type.call(right)) { | ||
throw new Error('Can not merge two different objects.'); | ||
return merge(left, right); | ||
}; | ||
EasyConfiguration.prototype.dirName = function(path) { | ||
var num; | ||
num = path.lastIndexOf('/'); | ||
return path.substr(0, num); | ||
}; | ||
EasyConfiguration.prototype.normalizePath = function(path) { | ||
var part, parts, prev, result, _i, _len; | ||
parts = path.split('/'); | ||
result = []; | ||
prev = null; | ||
for (_i = 0, _len = parts.length; _i < _len; _i++) { | ||
part = parts[_i]; | ||
if (part === '.' || part === '') { | ||
continue; | ||
} else if (part === '..' && prev) { | ||
result.pop(); | ||
} else { | ||
result.push(part); | ||
} | ||
prev = part; | ||
} | ||
switch (type.call(left)) { | ||
case '[object Array]': | ||
for (i = _i = 0, _len = right.length; _i < _len; i = ++_i) { | ||
value = right[i]; | ||
if (left.indexOf(value) === -1) { | ||
left.push(value); | ||
} else if (type.call(value) === '[object Array]' || type.call(value) === '[object Object]') { | ||
left[i] = this.merge(left[i], value); | ||
} | ||
} | ||
break; | ||
case '[object Object]': | ||
for (name in right) { | ||
value = right[name]; | ||
if (typeof left[name] === 'undefined') { | ||
left[name] = value; | ||
} else if (type.call(value) === '[object Array]' || type.call(value) === '[object Object]') { | ||
left[name] = this.merge(left[name], value); | ||
} | ||
} | ||
} | ||
return left; | ||
return '/' + result.join('/'); | ||
}; | ||
@@ -203,0 +197,0 @@ |
@@ -12,10 +12,2 @@ // Generated by CoffeeScript 1.6.3 | ||
Extension.prototype.setConfigurator = function(configurator) { | ||
this.configurator = configurator; | ||
}; | ||
Extension.prototype.setData = function(data) { | ||
this.data = data; | ||
}; | ||
Extension.prototype.getConfig = function(defaults) { | ||
@@ -22,0 +14,0 @@ if (defaults == null) { |
{ | ||
"name": "easy-configuration", | ||
"description": "Simply extensible loader for json config files", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"author": { | ||
@@ -25,3 +25,6 @@ "name": "David Kudera", | ||
}, | ||
"main": "./lib/EasyConfiguration.js" | ||
"main": "./lib/EasyConfiguration.js", | ||
"dependencies": { | ||
"tea-merge": "0.1.0" | ||
} | ||
} |
@@ -15,3 +15,3 @@ # Easy Configuration | ||
var Configuration = require('easy-configuration'); | ||
var config = new Configuration('./config.json'); | ||
var config = new Configuration('/var/data/config.json'); | ||
@@ -21,2 +21,6 @@ var data = config.load(); | ||
Be carefull with setting your path to the config file. Easy-Configuration uses required instead of fs module, because of | ||
ability to use it in browser. If you will set this path relativelly, then it will be relative to the Easy-Configuration | ||
file, not to your actual file. | ||
## Parameters | ||
@@ -126,3 +130,3 @@ In default, this configurator contains two basic sections: parameters and includes. | ||
EasyConfiguration class has got one other useful method and it is merge. | ||
EasyConfiguration class has got one other useful method and it is merge (using [tea-merge](https://npmjs.org/package/tea-merge)). | ||
@@ -129,0 +133,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
173
15918
1
197
+ Addedtea-merge@0.1.0
+ Addedtea-merge@0.1.0(transitive)
+ Addedtea-type@0.1.0(transitive)