Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First Release

  • Loading branch information...
commit 7fcf9e922e4ee09a1f7ab55ab8705a5dc60caa7f 0 parents
@Qard authored
38 README.md
@@ -0,0 +1,38 @@
+# Fig
+Fig makes it dead easy to manage a folder full of json config files your app depends on.
+
+## Requirements
+* Node.js 0.4+
+
+## Install
+
+ npm install fig
+
+## Usage
+
+ fig.env('production').load(__dirname+'/config', function (conf) {
+ console.log(conf.get('redis.host'))
+ })
+
+#### fig.env(name)
+Sets the environment name to scope to in your json configs, if available.
+
+#### fig.path(path)
+Sets the folder to load json configs. NOTE: sub directories aren't supported yet. I'll deal with that later.
+
+#### fig.load([path], callback)
+This tells fig to start loading the json files and run the callback when it's complete. The callback takes two arguments (err, conf)
+
+#### conf.get(path)
+As a little bonus, the conf object passed into the fig.load() callback includes a handy little helper for searching for values using a dot-path string. For example; conf.get('redis.host')
+
+---
+
+### Copyright (c) 2011 Stephen Belanger
+#### Licensed under MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3  config/all_envs.json
@@ -0,0 +1,3 @@
+{
+ "some":"var"
+}
5 config/redis.json
@@ -0,0 +1,5 @@
+{
+ "production": {
+ "host":"localhost"
+ }
+}
18 example.js
@@ -0,0 +1,18 @@
+var jsonfig = require('./index')
+
+// The env is switchable. Will be 'development' by default.
+// You can also use fig.path() to set the path rather
+// than supplying it as the first argument of load().
+jsonfig.env('production').load(__dirname+'config', function (conf) {
+ // Configs are loaded, let's use them.
+ console.log(conf.redis.host)
+
+ // You can also use the get() dot-location utility.
+ console.log(conf.get('redis.host'))
+ console.log(conf.get('redis').host)
+
+ // If an object of the matching name
+ // is not found in the json config,
+ // the whole object will be used.
+ console.log(conf.all_envs)
+})
105 index.js
@@ -0,0 +1,105 @@
+/**
+ * jsonfig - json config, simplified.
+ *
+ * TODO: Support subdirectories
+ */
+// Load dependencies.
+var p = require('path'), fs = require('fs')
+
+// Adds dot-path location to ordinary objects.
+function Obj (o) {
+ for (var p in o) {
+ this[p] = (typeof o[p] === 'object') ? new Obj(o[p]) : o[p]
+ }
+}
+Obj.prototype.get = function (p) {
+ var o = p.split('.').reduce(function (obj, i) { return obj[i] }, this)
+ return (typeof o === 'object') ? new Obj(o) : o
+}
+
+// Copy after() from underscore,
+// since we don't need anything else.
+function after (t, cb) {
+ return function () {
+ if (--t < 1) { return cb.apply(this, arguments) }
+ }
+}
+
+// Add chainable masked property modifier to obj.
+function addMProp (o, n, d) {
+ var m = d
+ o[n] = function (v) {
+ if (typeof v === 'undefined') {
+ return m
+ }
+ m = v
+ return this
+ }
+}
+
+// Construct jsonfig object.
+var jsonfig = {
+ load: function (cb) {
+ if (typeof cb === 'string') {
+ this.path(cb)
+ cb = arguments[1]
+ }
+
+ var self = this, path = this.path(), obj = {}, noop = function(){}
+
+ // Get list of all files in the defined path.
+ fs.readdir(path, function (err, files) {
+ if (err) { return cb(err) }
+
+ // Filter out files not ending in .json
+ var files = files.filter(function (f) {
+ return p.extname(f) === '.json'
+ })
+
+ // This gets called after loading is done.
+ var attempt = after(files.length, function () {
+ finish(null, obj)
+ })
+
+ // Handles completions and errors.
+ function finish (err, o) {
+ attempt = noop
+ if (err) return cb(err)
+ cb(null, new Obj(o))
+ }
+
+ // Start loading files.
+ files.forEach(function (file) {
+
+ // Get file info.
+ fs.stat(path+'/'+file, function (err, stats) {
+ // Ensure no errors occured and
+ // that the file really is a file.
+ if (err || ! stats.isFile()) {
+ return finish(err || new Error('Not a file'))
+ }
+
+ // Read the contents.
+ fs.readFile(path+'/'+file, function (err, data) {
+ if (err) {
+ return finish(err)
+ }
+
+ // Parse and insert env data or whole structure into obj.
+ var json = JSON.parse(data)
+ obj[p.basename(file, '.json')] = json[self.env()] || json
+
+ attempt()
+ })
+ })
+ })
+ })
+ }
+}
+
+// Add chainable masked property modifiers.
+addMProp(jsonfig, 'env', 'development')
+addMProp(jsonfig, 'path', __dirname)
+
+// Export jsonfig.
+module.exports = jsonfig
5,007 npm-debug.log
5,007 additions, 0 deletions not shown
14 package.json
@@ -0,0 +1,14 @@
+{
+ "author": "Stephen Belanger <admin@stephenbelanger.com> (http://stephenbelanger.com)",
+ "name": "jsonfig",
+ "description": "Simple json config folder manager",
+ "version": "0.0.0",
+ "repository": {
+ "url": "git://github.com/Qard/jsonfig.git"
+ },
+ "engines": {
+ "node": "~v0.4.11"
+ },
+ "dependencies": {},
+ "devDependencies": {}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.