Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
removed dep. + support for CoffeeScript
- Loading branch information
John Le Drew
committed
Sep 7, 2014
1 parent
fa5665d
commit 4b639a4
Showing
4 changed files
with
191 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,45 @@ | ||
{ | ||
"author": "John Le Drew <jp@antz29.com> (http://antz29.com)", | ||
"name": "configr", | ||
"description": "A library parse JSON configuration files.", | ||
"keywords": ["configuration", "json"], | ||
"version": "0.2.2", | ||
"homepage": "http://github.com/antz29/node-configr", | ||
"repository": { | ||
"type": "git", | ||
"url": "git:/git://github.com/antz29/node-configr.git" | ||
}, | ||
|
||
"bugs": { | ||
"email": "configr-bugs@antz29.com", | ||
"url": "http://github.com/antz29/node-configr/issues" | ||
}, | ||
|
||
"engines": { | ||
"node": ">=0.6.0" | ||
}, | ||
|
||
"directories": { | ||
"lib": "./src/lib" | ||
}, | ||
|
||
"main" : "./src/lib/configr.coffee", | ||
|
||
"dependencies": { | ||
"underscore" : "*", | ||
"coffee-script" : "*" | ||
}, | ||
|
||
"devDependencies": { | ||
"expresso" : "*" | ||
}, | ||
"scripts" : { | ||
"test" : "./node_modules/expresso/bin/expresso" | ||
} | ||
"author": { | ||
"name": "John Le Drew", | ||
"email": "jp@antz29.com", | ||
"url": "http://antz29.com" | ||
}, | ||
"name": "configr", | ||
"description": "A library parse JSON configuration files.", | ||
"keywords": [ | ||
"configuration", | ||
"json" | ||
], | ||
"version": "0.3.0", | ||
"homepage": "http://github.com/antz29/node-configr", | ||
"repository": { | ||
"type": "git", | ||
"url": "git:/git://github.com/antz29/node-configr.git" | ||
}, | ||
"bugs": { | ||
"email": "configr-bugs@antz29.com", | ||
"url": "http://github.com/antz29/node-configr/issues" | ||
}, | ||
"engines": { | ||
"node": "~0.10.0" | ||
}, | ||
"directories": { | ||
"lib": "./src/lib" | ||
}, | ||
"main": "./src/lib/configr.js", | ||
"dependencies": { | ||
"underscore": "*" | ||
}, | ||
"devDependencies": { | ||
"expresso": "*" | ||
}, | ||
"scripts": { | ||
"test": "./node_modules/expresso/bin/expresso" | ||
}, | ||
"maintainers": [ | ||
{ | ||
"name": "antz29", | ||
"email": "jp@antz29.com" | ||
} | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// converted from CoffeeScript (so looks a bit weird at the moment...) | ||
|
||
var Configr, EventEmitter, fs, isPlainObject, loadConfig, loadConfigFromDir, mergeConfig, path, _, | ||
__hasProp = {}.hasOwnProperty, | ||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; | ||
|
||
fs = require('fs'); | ||
path = require('path'); | ||
|
||
_ = require('underscore'); | ||
|
||
EventEmitter = require('events').EventEmitter; | ||
|
||
loadConfigFromDir = function(ext, dir, callback) { | ||
var out; | ||
out = {}; | ||
return fs.readdir(dir, function(err, files) { | ||
var file, _fn, _i, _len; | ||
_fn = function(file) { | ||
var error, file_path, name; | ||
if (path.extname(file) !== ("." + ext)) { | ||
return null; | ||
} | ||
file_path = dir + '/' + file; | ||
name = file.replace("." + ext, ''); | ||
try { | ||
return out[name] = require(file_path).config; | ||
} catch (_error) { | ||
error = _error; | ||
throw error; | ||
} | ||
}; | ||
for (_i = 0, _len = files.length; _i < _len; _i++) { | ||
file = files[_i]; | ||
_fn(file); | ||
} | ||
return callback(out); | ||
}); | ||
}; | ||
|
||
loadConfig = function(ext, root, env, callback) { | ||
return loadConfigFromDir(ext, root, function(data) { | ||
var dir; | ||
if (!env) { | ||
return callback(data); | ||
} | ||
dir = root + '/' + env; | ||
return fs.exists(dir, function(exists) { | ||
if (!exists) { | ||
return callback(data); | ||
} | ||
return loadConfigFromDir(ext, dir, function(env_data) { | ||
return callback(mergeConfig(data, env_data)); | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
isPlainObject = function(obj) { | ||
if (!obj) { | ||
return false; | ||
} | ||
if (_.isArray(obj)) { | ||
return false; | ||
} | ||
if (_.isFunction(obj)) { | ||
return false; | ||
} | ||
if (_.isString(obj)) { | ||
return false; | ||
} | ||
if (_.isBoolean(obj)) { | ||
return false; | ||
} | ||
if (_.isNumber(obj)) { | ||
return false; | ||
} | ||
if (_.isDate(obj)) { | ||
return false; | ||
} | ||
if (_.isRegExp(obj)) { | ||
return false; | ||
} | ||
if (_.isNaN(obj)) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
|
||
mergeConfig = function(conf1, conf2) { | ||
_.map(conf2, function(val, key) { | ||
if (conf1[key] && isPlainObject(val)) { | ||
return conf1[key] = mergeConfig(conf1[key], conf2[key]); | ||
} else { | ||
return conf1[key] = val; | ||
} | ||
}); | ||
return conf1; | ||
}; | ||
|
||
Configr = (function(_super) { | ||
__extends(Configr, _super); | ||
|
||
function Configr(opt) { | ||
if (opt == null) { | ||
opt = {}; | ||
} | ||
this.options = _.defaults(opt, { | ||
root: root, | ||
env: null, | ||
language: 'js' | ||
}); | ||
this.config = {}; | ||
this.loading = false; | ||
this._load(); | ||
} | ||
|
||
Configr.prototype.getRoot = function() { | ||
return this.options.root; | ||
}; | ||
|
||
Configr.prototype.getEnv = function() { | ||
return this.options.env; | ||
}; | ||
|
||
Configr.prototype._load = function() { | ||
if (this.loading) { | ||
return null; | ||
} | ||
this.loading = true; | ||
return loadConfig(this.options.language, this.options.root, this.options.env, (function(_this) { | ||
return function(data) { | ||
_this.config = mergeConfig(_this.config, data); | ||
_this.loading = false; | ||
return _this.emit('ready'); | ||
}; | ||
})(this)); | ||
}; | ||
|
||
Configr.prototype.get = function() { | ||
return this.config; | ||
}; | ||
|
||
return Configr; | ||
|
||
})(EventEmitter); | ||
|
||
module.exports = Configr; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters