config-node
Advanced tools
Comparing version 0.0.1 to 1.0.0
47
index.js
@@ -1,12 +0,37 @@ | ||
module.exports = function(defaultconfig) { | ||
// Check for config file, synchronously, default to config-dev.js | ||
defaultconfig = defaultconfig.default || 'config-dev.js'; | ||
var tryconfigfile = process.cwd()+'/'+process.argv[2]+'.js'; | ||
if( !require('fs').existsSync(tryconfigfile) ) | ||
tryconfigfile = process.cwd()+'/'+defaultconfig; | ||
var path = require('path'), | ||
fs = require('fs'); | ||
// Load config file | ||
config = require(tryconfigfile); | ||
} | ||
var config = module.exports = function(opts) { | ||
opts = opts || {}; | ||
var dir = opts.dir || 'config'; | ||
if (dir.indexOf('.') === -1) { | ||
dir = path.join(process.cwd(), dir); | ||
} | ||
var env = opts.env || process.env.NODE_ENV || 'development'; | ||
// You can specify the extension on opts.ext to improve performance | ||
var ext = 'ext' in opts ? opts.ext : extension(dir, env); | ||
var file = path.join(dir, env) + (ext ? '.'+ext : ''); | ||
// You can pass an option named as the extension with a function that parses the file | ||
// else, directory(''), js and json are handled by native require | ||
var data = ext in opts ? opts[ext](fs.readFileSync(file, 'utf8')) : require(file); | ||
for (var key in data) { | ||
config[key] = data[key]; | ||
} | ||
return config; | ||
}; | ||
function extension(dir, env) { | ||
var file = fs.readdirSync(dir).filter(function(filename) { | ||
return filename.indexOf(env) === 0; | ||
})[0]; | ||
if (!file) { | ||
throw new Error('No file found for environment '+env); | ||
} | ||
return file.slice(env.length+1); | ||
} |
{ | ||
"name": "config-node", | ||
"description": "Super simple config mgmt for Node.js", | ||
"url": "http://github.com/dpweb/config-node/", | ||
"author": "Ariel Flesler <aflesler@gmail.com>", | ||
"version": "1.0.0", | ||
"description": "Flexible lightweight configuration loader for Node", | ||
"keywords": [ | ||
"util", | ||
"nodejs", | ||
"config", | ||
"configuration", | ||
"config" | ||
"file", | ||
"require", | ||
"json", | ||
"environment", | ||
"variable", | ||
"node", | ||
"yaml", | ||
"coffee", | ||
"coffee-script" | ||
], | ||
"author": { | ||
"name": "C Borkert", | ||
"email": "ILIKECHEESE@dpsw.info" | ||
"license": "MIT", | ||
"homepage": "https://github.com/flesler/config-node", | ||
"bugs": "https://github.com/flesler/config-node/issues", | ||
"repository": "git://github.com/flesler/config-node", | ||
"main": "./index.js", | ||
"engines": { | ||
"node": "*", | ||
"npm": "*" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/dpweb/config-node.git" | ||
}, | ||
"version": "0.0.1", | ||
"readme": "configurator\n============\n\nSuper simple configuration mgmt for Node.js apps", | ||
"_id": "config-node@0.0.1", | ||
"dist": { | ||
"shasum": "def2f2da4e1642f52669113f022b5b61f40b319e" | ||
}, | ||
"_from": "./c2" | ||
"dependencies": {} | ||
} |
107
README.md
@@ -1,4 +0,105 @@ | ||
configurator | ||
============ | ||
# Flexible lightweight configuration loader for Node | ||
Super simple configuration mgmt for Node.js apps | ||
## Installation | ||
$ npm install config-node | ||
## Basic usage | ||
```js | ||
var config = require('config-node')(); | ||
console.log(config.server.port); | ||
``` | ||
<code>config</code> will contain the contents of the configuration file named <code>process.env.NODE_ENV</code> or <code>'development'</code>, on the folder <code>config/</code>.<br /> | ||
It can be a JSON, a JS that fills <code>module.exports</code> or a directory with an index.js inside. | ||
You probably noticed there's a function call in there. It needs to be added once to load the data. This is where you can put your options. | ||
You'll probably do that on your main js file, in all the other ones you only require it: | ||
```js | ||
var config = require('config-node'); | ||
console.log(config.db.port); | ||
``` | ||
## Options | ||
These are the defaults: | ||
```js | ||
var config = require('config-node')({ | ||
dir: 'config', // where to look for files | ||
ext: null, // spoil the fun, tell me which one it is ('' for directory). Improves performance. | ||
env: process.env.NODE_ENV || 'development' // set which one instead of smart defaults | ||
}); | ||
``` | ||
In order to support more formats beyond json, js and directories. You add a file with a different extension to the config folder | ||
and pass an option named as the extension with a function that takes the file string data and returns an object, synchronously! | ||
```js | ||
var config = require('config-node')({ | ||
png: function(data) { return convertPNGtoObjectSomehow(data); } | ||
}); | ||
``` | ||
Check the examples to see more use cases. | ||
## Examples | ||
- [How to use JSON files](examples/json) | ||
- [How to use JS files](examples/js) | ||
- [How to use entire folders](examples/folder) | ||
- [How to use YAML files](examples/yaml) | ||
- [How to use Coffee-Script files](examples/coffee) | ||
- [How to use INI files](examples/ini) | ||
- [How to use Properties files](examples/properties) | ||
- [How to load from a different directory](examples/custom-dir) | ||
- [How to keep local configuration unversioned](examples/unversioned) | ||
- [How to override the environment from NODE_ENV](examples/environment) | ||
- [How to skip extension detection](examples/extension) | ||
- [How to encapsulate config logic](examples/encapsulate) | ||
## Similar Projects | ||
- [lorenwest/node-config](https://github.com/lorenwest/node-config) - It's good, but too complex for me. I'd never need things like file watching (just use nodemon). | ||
- [dominictarr/config-chain](https://github.com/dominictarr/config-chain) - Very cool one, too complex for most simple cases | ||
- Feel free to suggest others, these are the ones I found and used for inspiration | ||
## Why use this project over others | ||
- It's simple, the code is short and clean | ||
- It's extensible, it can support coffee, yaml, ini or anything else you want, just DIY. | ||
- It has no dependencies. If you need yaml, just include the one you prefer and pass it over. | ||
- It's fast. Loading configuration needs to be fast, if you pass the `ext` setting, this module is mostly a smart require. | ||
## Some concepts taken into account | ||
- [Convention over configuration](http://en.wikipedia.org/wiki/Convention_over_configuration), tuned for the mayority. | ||
- [Pareto principle](http://en.wikipedia.org/wiki/Pareto_principle). I aim for that 80% that needs only the 20% of the features. | ||
- [KISS principle](http://en.wikipedia.org/wiki/KISS_principle). It's really simple but it does the job. | ||
- [YAGNI](http://en.wikipedia.org/wiki/YAGNI). I prefer to add features based on requests. | ||
## LICENSE | ||
The MIT License (MIT) | ||
Copyright (c) 2014 Ariel Flesler | ||
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. |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
7502
4
29
0
105
0
4