Permalink
Browse files

add idiff; change the style to object; add extend for common part

  • Loading branch information...
1 parent 9cf387a commit ead159a42cdc8fea0afd165ce2753fd2d05cd40d @yarcowang committed Feb 9, 2013
Showing with 173 additions and 10 deletions.
  1. +40 −2 README.md
  2. +60 −0 bin/idiff
  3. +32 −5 lib/icnf.js
  4. +5 −1 package.json
  5. +3 −0 test/etc/comm/comm.json
  6. +12 −1 test/etc/development.json
  7. +14 −1 test/etc/production.json
  8. +7 −0 test/test.js
View
@@ -1,7 +1,7 @@
-iConfig
+iconfig
========
-`iCnf` is a nodejs module for supporting configeration.
+`icnf` is a nodejs module for supporting configeration.
**License: This project is under GPL/BSD**
@@ -40,12 +40,50 @@ var config = icnf(); // default is process.env.NODE_ENV || 'production', so it w
var config = icnf('development'); // then, now it will choose etc/development.json
```
+### How to use _idiff_
+There is also a tool named _idiff_ under bin/ above v0.0.3. This tool only check the key in the config file. For example:
+
+```
+$ ../bin/idiff production
+ checking /Users/yarco/Sites/js/icnf/test/etc/development.json
+- database.port
+- database.more.name
+! database.more.port
+- keywords.1
+```
+
+Argument _production_ is the standard config file in this example. (That means you may treat that file as a standard.)
+This file should be located in _etc/_ (etc/production.json).
+
+* **-** means the key _database.port_ is lost in _development.json_ when comparing with _production.json_
+* **!** means type does't match
+
+**Notice:**
+
+* Array like _keywords_ here will always treat as an object.
+* Key exists in _development_ but not in _production_ will be totally ignored.
+* _extend_ part won't be checked. cause it is used for common part
+
+development to production (you could also add more files there, example: testing.json):
+
+```
+$ ../bin/idiff development
+ checking /Users/yarco/Sites/js/icnf/test/etc/production.json
+- gf
+- database.more.kk
+! database.more.port
+```
+
Extra notice
------------
* the config file is `<PROJECT>/etc/<ENV>.json`
+* files in subdirectories under etc/ will be totally ignored when using _idiff_
ChangeLog
----------
+* 0.0.1 to 0.0.3
+ * add _idiff_
+ * add _extend_ feature for common part, see example under tests/
Sugguestion
-----------
View
@@ -0,0 +1,60 @@
+#!/usr/bin/env node
+
+// vim: set tabstop=2 shiftwidth=2 softtabstop=2 noexpandtab ai si:
+var program = require('commander')
+ , path = require('path')
+ , fs = require('fs');
+
+program.version('0.0.3')
+ .usage('[options] <main>');
+
+program.on('--help', function() {
+ console.log(' Examples:\n' +
+ ' $ idiff production\n');
+});
+
+program.parse(process.argv);
+
+if (program.args.length != 1) {
+ console.log('see more help with --help');
+ process.exit(0);
+}
+
+// basic directory
+var dir = process.env.PWD;
+dir = path.join(dir, 'etc');
+
+// cache
+var cnfs = {};
+fs.readdirSync(dir).forEach(function(file) {
+ if (file.substr(-4) === 'json') {
+ var f = path.join(dir, file);
+ cnfs[f] = require(f);
+ }
+});
+
+var mainfile = path.join(dir, program.args[0] + '.json');
+var origin = cnfs[mainfile];
+
+for(var k in cnfs) {
+ if (k != mainfile) {
+ console.log(' checking ' + k);
+ idiff(origin, cnfs[k]);
+ }
+}
+
+function idiff(origin, target, prefix)
+{
+ if (typeof origin == 'object' && origin) {
+ for(var i in origin) {
+ var s = prefix ? prefix + '.' + i : i;
+ if (typeof target[i] == 'undefined') {
+ console.log('- ' + s);
+ } else if (typeof origin[i] != typeof target[i]) {
+ console.log('! ' + s);
+ } else if (typeof target[i] == 'object') {
+ idiff(origin[i], target[i], s);
+ }
+ }
+ }
+}
View
@@ -10,12 +10,39 @@
var path = require('path');
-module.exports = function(dir) { // project root
- var root = dir;
-
+module.exports = function(dir) {
+ var cnf = new iCnf;
+ cnf.rootDir = dir;
return function(env) {
- env = env || process.env.NODE_ENV || 'production';
- return require(path.join(root, 'etc', env + '.json')); // <PROJECT>/etc/<ENV>.json
+ return cnf.require(env);
}
+};
+
+/**
+ * @class iCnf
+ */
+function iCnf()
+{
+ this.rootDir = '';
}
+(function() {
+
+ /** require something {{{
+ */
+ this.require = function(env) {
+ env = env || process.env.NODE_ENV || 'production';
+ var config = require(path.join(this.rootDir, 'etc', env + '.json'));
+ if (config.extend) {
+ var comm = require(path.join(this.rootDir, 'etc', 'comm', config.extend + '.json'));
+ for(var k in comm) {
+ if (!config[k]) {
+ config[k] = comm[k];
+ }
+ }
+ }
+ return config;
+ };/*}}}*/
+
+}).call(iCnf.prototype);
+
View
@@ -1,12 +1,16 @@
{
"name": "icnf",
- "version": "0.0.1",
+ "version": "0.0.3",
"description": "a nodejs module for supporting configeration",
"main": "index.js",
"scripts": {
"test": "mocha"
},
+ "bin": {
+ "icnfsync": "./bin/idiff"
+ },
"dependencies": {
+ "commander": "*"
},
"devDependencies": {
"mocha": "*",
@@ -0,0 +1,3 @@
+{
+ "host":"127.0.0.1"
+}
@@ -1,5 +1,16 @@
{
+ "extend":"comm",
"name":"yarco",
"sex":"male",
- "gf":"shunza"
+ "gf":"shunza",
+ "database": {
+ "host": "localhost",
+ "more": {
+ "kk": "ok",
+ "port":77
+ }
+ },
+ "keywords": [
+ "abc"
+ ]
}
@@ -1,4 +1,17 @@
{
+ "extend":"comm",
"name": "yarco",
- "sex": "male"
+ "sex": "male",
+ "database": {
+ "host": "localhost",
+ "port": 1111,
+ "more": {
+ "name":"love",
+ "port":"77"
+ }
+ },
+ "keywords": [
+ "abc",
+ "efg"
+ ]
}
View
@@ -12,3 +12,10 @@ describe('development env should using development.json', function() {
});
});
+describe('they should also have field host', function() {
+ it('should equal 127.0.0.1', function() {
+ icnf().host.should.equal('127.0.0.1');
+ icnf('development').host.should.equal('127.0.0.1');
+ });
+});
+

0 comments on commit ead159a

Please sign in to comment.