easy-configuration
Advanced tools
Comparing version 1.2.0 to 1.3.0
// Generated by CoffeeScript 1.6.3 | ||
(function() { | ||
var EasyConfiguration, Extension, merge; | ||
var EasyConfiguration, Extension, Helpers, merge; | ||
@@ -9,2 +9,4 @@ merge = require('tea-merge'); | ||
Helpers = require('./Helpers'); | ||
EasyConfiguration = (function() { | ||
@@ -17,3 +19,3 @@ EasyConfiguration.prototype.fileName = null; | ||
EasyConfiguration.prototype._parameters = {}; | ||
EasyConfiguration.prototype.files = []; | ||
@@ -41,10 +43,15 @@ EasyConfiguration.prototype.parameters = {}; | ||
EasyConfiguration.prototype.invalidate = function() { | ||
this.data = null; | ||
return this; | ||
}; | ||
EasyConfiguration.prototype.load = function() { | ||
var config; | ||
var config, data; | ||
if (this.data === null) { | ||
config = this.loadConfig(this.fileName); | ||
this._parameters = config._parameters; | ||
this.parameters = config.parameters; | ||
config.data = this.parse(config.data); | ||
this.data = config.data; | ||
data = this.parse(config); | ||
this.files = data.files; | ||
this.parameters = data.parameters; | ||
this.data = data.sections; | ||
} | ||
@@ -54,62 +61,29 @@ return this.data; | ||
EasyConfiguration.prototype.invalidate = function() { | ||
this.data = null; | ||
return this; | ||
}; | ||
EasyConfiguration.prototype.loadConfig = function(file) { | ||
var data; | ||
data = { | ||
includes: [], | ||
_parameters: {}, | ||
parameters: {}, | ||
data: require(file) | ||
}; | ||
if (typeof data.data.includes !== 'undefined') { | ||
data.includes = data.data.includes; | ||
delete data.data.includes; | ||
var data, include, path, _i, _len, _ref; | ||
data = require(file); | ||
if (typeof data.includes !== 'undefined') { | ||
_ref = data.includes; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
include = _ref[_i]; | ||
path = Helpers.normalizePath(Helpers.dirName(file) + '/' + include); | ||
data = this.merge(data, this.loadConfig(path)); | ||
} | ||
} | ||
if (typeof data.data.parameters !== 'undefined') { | ||
data.parameters = data.data.parameters; | ||
data._parameters = this.parseParameters(data.parameters); | ||
delete data.data.parameters; | ||
} | ||
data = this.prepare(data, file); | ||
return data; | ||
}; | ||
EasyConfiguration.prototype.parseParameters = function(parameters, parent) { | ||
var name, result, value; | ||
if (parent == null) { | ||
parent = null; | ||
EasyConfiguration.prototype.parse = function(data) { | ||
var name, result, section, sections, _ref; | ||
result = { | ||
files: [], | ||
parameters: {}, | ||
sections: {} | ||
}; | ||
if (typeof data.includes !== 'undefined') { | ||
result.files = data.includes; | ||
} | ||
result = {}; | ||
if (Object.prototype.toString.call(parameters) === '[object Object]') { | ||
for (name in parameters) { | ||
value = parameters[name]; | ||
result = this.merge(result, this.parseParameters(value, parent === null ? name : parent + '.' + name)); | ||
} | ||
} else { | ||
result[parent] = parameters; | ||
if (typeof data.parameters !== 'undefined') { | ||
result.parameters = this.expandParameters(data.parameters); | ||
} | ||
return result; | ||
}; | ||
EasyConfiguration.prototype.prepare = function(data, parent) { | ||
var config, file, _i, _len, _ref; | ||
_ref = data.includes; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
file = _ref[_i]; | ||
file = this.normalizePath(this.dirName(parent) + '/' + file); | ||
config = this.loadConfig(file); | ||
data.includes = this.merge(config.includes, data.includes); | ||
data.parameters = this.merge(config.parameters, data.parameters); | ||
data._parameters = this.merge(config._parameters, data._parameters); | ||
data.data = this.merge(config.data, data.data); | ||
} | ||
return data; | ||
}; | ||
EasyConfiguration.prototype.parse = function(data) { | ||
var name, section, _ref; | ||
_ref = this.extensions; | ||
@@ -122,75 +96,34 @@ for (name in _ref) { | ||
} | ||
for (name in data) { | ||
section = data[name]; | ||
sections = data; | ||
if (typeof sections.parameters !== 'undefined') { | ||
delete sections.parameters; | ||
} | ||
if (typeof sections.includes !== 'undefined') { | ||
delete sections.includes; | ||
} | ||
for (name in sections) { | ||
section = sections[name]; | ||
if (typeof this.extensions[name] === 'undefined') { | ||
throw new Error('Found section ' + name + ' but there is no coresponding extension.'); | ||
} | ||
result.sections[name] = {}; | ||
this.extensions[name].data = section; | ||
data[name] = this.extensions[name].loadConfiguration(); | ||
data[name] = this.expand(data[name]); | ||
section = this.extensions[name].loadConfiguration(); | ||
section = Helpers.expandWithParameters(section, result.parameters); | ||
result.sections[name] = section; | ||
} | ||
return data; | ||
return result; | ||
}; | ||
EasyConfiguration.prototype.expand = function(data) { | ||
var i, value, _i, _len; | ||
switch (Object.prototype.toString.call(data)) { | ||
case '[object String]': | ||
data = this.expandParameter(data); | ||
break; | ||
case '[object Array]': | ||
for (i = _i = 0, _len = data.length; _i < _len; i = ++_i) { | ||
value = data[i]; | ||
data[i] = this.expand(value); | ||
} | ||
break; | ||
case '[object Object]': | ||
for (i in data) { | ||
value = data[i]; | ||
data[i] = this.expand(value); | ||
} | ||
} | ||
return data; | ||
EasyConfiguration.prototype.expandParameters = function(parameters) { | ||
parameters = Helpers.stringifyParameters(parameters); | ||
parameters = Helpers.expandParameters(parameters); | ||
parameters = Helpers.objectifyParameters(parameters); | ||
return parameters; | ||
}; | ||
EasyConfiguration.prototype.expandParameter = function(parameter) { | ||
var _this = this; | ||
parameter = parameter.replace(/%([a-zA-Z\.]+)%/g, function(match, param, offset, s) { | ||
if (typeof _this._parameters[param] === 'undefined') { | ||
throw new Error('Parameter ' + param + ' is not defined.'); | ||
} | ||
return _this._parameters[param]; | ||
}); | ||
return parameter; | ||
}; | ||
EasyConfiguration.prototype.merge = function(left, right) { | ||
return merge(left, right); | ||
return Helpers.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; | ||
} | ||
return '/' + result.join('/'); | ||
}; | ||
return EasyConfiguration; | ||
@@ -197,0 +130,0 @@ |
{ | ||
"name": "easy-configuration", | ||
"description": "Simply extensible loader for json config files", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "David Kudera", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18579
10
251
1