Skip to content

Commit

Permalink
make use of module pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
mboughaba committed May 31, 2015
1 parent 5929b1f commit 8d5f550
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 147 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ For example, if NODE_ENV=development then it will require 'root/dir/of/config/fi
Finally, the callback function is called once all done.

```javascript
var config = require(__dirname + '/../lib/config');
config({dir: 'root/dir/of/config/files/', defaultFile: 'path/to/default/config/file'}, function () {
var etcjs = require('etcjs');
etcjs.load({dir: 'root/dir/of/config/files/', defaultFile: 'path/to/default/config/file'}, function () {
console.info('configuration has been initialized');
});
```

## Running tests

Install dependencies & run tests:
```shell
$ npm install
$ npm test
```

Expand Down
8 changes: 3 additions & 5 deletions bin/init
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#!/usr/bin/env node

var config = require(__dirname + '/../lib/config');
console.dir(process.argv);
console.log(process.argv[2]);
console.log(process.argv[3]);
config({
var etcjs = require(__dirname + '/../lib/etcjs');

etcjs.load({
dir: process.argv[2],
defaultFile: process.argv[3]
}, function() {
Expand Down
File renamed without changes.
51 changes: 0 additions & 51 deletions lib/config.js

This file was deleted.

48 changes: 48 additions & 0 deletions lib/etcjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*global module:false,require:false*/
var debug = require('debug')('config'),
path = require('path'),
etcjs = (function() {

var _getEnvironmentVariable = function(env) {
'use strict';
var regEx = /^[a-zA-Z0-9_]*$/;

if (!env) {
debug('missing node environment variable');
throw new Error('missing node environment');
}

if (!regEx.test(env)) {
debug('invalid node environment variable');
throw new Error('invalid node environment');
}
return env;
};

return {
load: function(options, callback) {
'use strict';
var globalEnv,
localEnv,
env = _getEnvironmentVariable(process.env.NODE_ENV),
globalPath,
localPath;
if (!options || !options.dir || !options.defaultFile) {
throw new Error('Missing required option `file`');
}
globalPath = path.normalize(options.defaultFile);
localPath = path.normalize(options.dir + '/' + env);
debug('initializing configuration for %s', env);
// NOTE: by loading the global env first we ensure that env. var. can be
// overwritten by the local configuration.
globalEnv = require(globalPath);
env = localEnv = require(localPath);
// NOTE: inform that loading the configuration is completed.
if (callback && typeof callback === 'function') {
callback();
}
}
};
})();

module.exports = etcjs;
100 changes: 49 additions & 51 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,54 +1,52 @@
{
"name": "etcjs",
"version": "0.11.0",
"private": false,
"description": "Simple stupid node.js configuration module",
"main": "lib/config",
"scripts": {
"start": "node bin/init",
"test": "mocha",
"test-watch": "mocha --opts ./test/mocha-watch.opts",
"cover": "istanbul cover node_modules/mocha/bin/_mocha",
"test-travis": "istanbul cover node_modules/.bin/_mocha --report lcovonly",
"lint": "jshint ."
},
"engines": {
"node": ">=0.8.x"
},
"author": "Mohamed Boughaba <[email protected]>",
"contributors": [
{
"name": "Mohamed Boughaba",
"email": "[email protected]"
"name": "etcjs",
"version": "0.11.0",
"private": false,
"description": "Simple stupid node.js configuration module",
"main": "lib/etcjs",
"scripts": {
"start": "node bin/init",
"test": "mocha",
"test-watch": "mocha --opts ./test/mocha-watch.opts",
"cover": "istanbul cover node_modules/mocha/bin/_mocha",
"test-travis": "istanbul cover node_modules/.bin/_mocha --report lcovonly",
"lint": "jshint ."
},
"engines": {
"node": ">=0.8.x"
},
"author": "Mohamed Boughaba <[email protected]>",
"contributors": [{
"name": "Mohamed Boughaba",
"email": "[email protected]"
}],
"license": "MIT",
"dependencies": {
"debug": "^2.2.0"
},
"devDependencies": {
"growl": "^1.8.1",
"istanbul": "^0.3.14",
"mocha": "^2.2.5",
"precommit-hook": "latest",
"should": "^6.0.3"
},
"keywords": [
"etc",
"js",
"configuration",
"node",
"simple"
],
"pre-commit": [
"lint",
"test"
],
"repository": {
"type": "git",
"url": "https://github.com/mboughaba/etcjs.git"
},
"bugs": {
"url": "https://github.com/mboughaba/etcjs/issues"
}
],
"license": "MIT",
"dependencies": {
"debug": "^2.2.0"
},
"devDependencies": {
"growl": "^1.8.1",
"istanbul": "^0.3.14",
"mocha": "^2.2.5",
"precommit-hook": "latest",
"should": "^6.0.3"
},
"keywords": [
"etc",
"js",
"configuration",
"node",
"simple"
],
"pre-commit": [
"lint",
"test"
],
"repository": {
"type": "git",
"url": "https://github.com/mboughaba/etcjs.git"
},
"bugs": {
"url": "https://github.com/mboughaba/etcjs/issues"
}
}
Loading

0 comments on commit 8d5f550

Please sign in to comment.