From 4b639a4632eed2274bc709a893c6ce100bbf9f30 Mon Sep 17 00:00:00 2001 From: John Le Drew Date: Mon, 8 Sep 2014 00:15:26 +0100 Subject: [PATCH] removed dep. + support for CoffeeScript --- package.json | 80 +++++++++++----------- src/lib/configr.coffee | 91 ------------------------- src/lib/configr.js | 148 +++++++++++++++++++++++++++++++++++++++++ test/configr.js | 11 --- 4 files changed, 191 insertions(+), 139 deletions(-) delete mode 100644 src/lib/configr.coffee create mode 100644 src/lib/configr.js diff --git a/package.json b/package.json index a5926b2..2bbf0d3 100644 --- a/package.json +++ b/package.json @@ -1,39 +1,45 @@ { - "author": "John Le Drew (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" + } + ] } diff --git a/src/lib/configr.coffee b/src/lib/configr.coffee deleted file mode 100644 index 92148b4..0000000 --- a/src/lib/configr.coffee +++ /dev/null @@ -1,91 +0,0 @@ -# Deps -fs = require('fs') -path = require('path') -_ = require('underscore') -EventEmitter = require('events').EventEmitter - -# Helper Functions -loadConfigFromDir = (ext, dir, callback) -> - out = {} - - fs.readdir dir,(err,files) -> - for file in files - do (file) -> - if path.extname(file) != ".#{ext}" then return null - file_path = dir + '/' + file - name = file.replace(".#{ext}",'') - try - out[name] = require(file_path).config - catch error - return throw error - callback(out) - -loadConfig = (ext, root,env,callback) -> - loadConfigFromDir ext, root, (data) -> - if !env then return callback(data) - dir = root + '/' + env - path.exists dir, (exists) -> - if !exists then return callback(data); - loadConfigFromDir ext, dir, (env_data) -> - callback mergeConfig(data,env_data); - -isPlainObject = (obj) -> - if !obj then return false - if _.isArray(obj) then return false - if _.isFunction(obj) then return false - if _.isString(obj) then return false - if _.isBoolean(obj) then return false - if _.isNumber(obj) then return false - if _.isDate(obj) then return false - if _.isRegExp(obj) then return false - if _.isNaN(obj) then return false - - return true - -mergeConfig = (conf1, conf2) -> - _.map conf2, (val,key) -> - if conf1[key] && isPlainObject(val) - conf1[key] = mergeConfig(conf1[key],conf2[key]) - else - conf1[key] = val - - return conf1 - -# The main Configr class -class Configr extends EventEmitter - constructor: (opt = {}) -> - - @options = _.defaults(opt,{ - root: root, - env: null, - language: 'js' - }); - - @config = {} - @loading = false - - this._load() - - #this._watch() see below - - getRoot: -> - return @options.root; - - getEnv: -> - return @options.env; - - _load: () -> - if (@loading) then return null - - @loading = true - - loadConfig @options.language, @options.root, @options.env, (data) => - @config = mergeConfig(@config,data) - @loading = false - this.emit('ready') - - get: -> - return @config - - -module.exports = Configr diff --git a/src/lib/configr.js b/src/lib/configr.js new file mode 100644 index 0000000..3be6932 --- /dev/null +++ b/src/lib/configr.js @@ -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; \ No newline at end of file diff --git a/test/configr.js b/test/configr.js index fd035c5..cf9ea81 100644 --- a/test/configr.js +++ b/test/configr.js @@ -59,17 +59,6 @@ var tests = { assert.equal(prod.get().db.obj.foo,'bar'); delete prod }); - }, - 'You can load coffee config' : function(beforeExit, assert) { - console.log('* You can load coffee config'); - var Configr = require('../'); - var c = new Configr({root:test_dir, language:'coffee'}); - - c.on('ready',function() { - assert.equal(c.get().test.foo,'bar'); - }) - - delete c } };