Comparing version 0.3.3 to 0.3.4
@@ -39,25 +39,25 @@ 'use strict'; | ||
// Configuration of the current project (local to the file | ||
// hierarchy). | ||
// Default vendor configuration. | ||
{ | ||
name: 'local', | ||
name: 'vendor', | ||
read: function () { | ||
// It is assumed that app-conf is in the `node_modules` | ||
// directory of the owner package. | ||
return Bluebird.map( | ||
glob(j(__dirname, '..', '..', 'config.*')), | ||
readFile | ||
); | ||
}, | ||
}, | ||
// Configuration for the whole system. | ||
{ | ||
name: 'system', | ||
read: function (opts) { | ||
var name = opts.name; | ||
// Compute the list of paths from the current directory to the | ||
// root directory. | ||
var paths = []; | ||
var dir, prev; | ||
dir = process.cwd(); | ||
while (dir !== prev) { | ||
paths.push(j(dir, '.' + name + '.*')); | ||
prev = dir; | ||
dir = resolvePath(dir, '..'); | ||
} | ||
return Bluebird.map(paths, function (path) { | ||
return glob(path, { | ||
silent: true, | ||
}).catch(ignoreAccessErrors); | ||
}).then(flatten).map(readFile); | ||
return Bluebird.map( | ||
glob(j('/etc', name, 'config.*')), | ||
readFile | ||
); | ||
} | ||
@@ -84,27 +84,27 @@ }, | ||
// Configuration for the whole system. | ||
// Configuration of the current project (local to the file | ||
// hierarchy). | ||
{ | ||
name: 'system', | ||
name: 'local', | ||
read: function (opts) { | ||
var name = opts.name; | ||
return Bluebird.map( | ||
glob(j('/etc', name, 'config.*')), | ||
readFile | ||
); | ||
// Compute the list of paths from the current directory to the | ||
// root directory. | ||
var paths = []; | ||
var dir, prev; | ||
dir = process.cwd(); | ||
while (dir !== prev) { | ||
paths.push(j(dir, '.' + name + '.*')); | ||
prev = dir; | ||
dir = resolvePath(dir, '..'); | ||
} | ||
return Bluebird.map(paths.reverse(), function (path) { | ||
return glob(path, { | ||
silent: true, | ||
}).catch(ignoreAccessErrors); | ||
}).then(flatten).map(readFile); | ||
} | ||
}, | ||
// Default vendor configuration. | ||
{ | ||
name: 'vendor', | ||
read: function () { | ||
// It is assumed that app-conf is in the `node_modules` | ||
// directory of the owner package. | ||
return Bluebird.map( | ||
glob(j(__dirname, '..', '..', 'config.*')), | ||
readFile | ||
); | ||
}, | ||
}, | ||
]; |
35
index.js
@@ -12,10 +12,11 @@ 'use strict'; | ||
var merge = require('lodash.merge'); | ||
var flatten = require('lodash.flatten'); | ||
var isObject = require('lodash.isobject'); | ||
var isString = require('lodash.isstring'); | ||
var map = require('lodash.map'); | ||
var merge = require('lodash.merge'); | ||
var entries = require('./entries'); | ||
var serializers = require('./serializers'); | ||
var UnknownFormatError = require('./unknown-format-error'); | ||
var unserialize = require('./serializers').unserialize; | ||
@@ -32,3 +33,3 @@ //==================================================================== | ||
function fixPath(value, base) { | ||
function fixPaths(value, base) { | ||
var path; | ||
@@ -49,3 +50,3 @@ | ||
var promises = map(value, function (item, key) { | ||
return fixPath(item, base).then(function (item) { | ||
return fixPaths(item, base).then(function (item) { | ||
value[key] = item; | ||
@@ -72,19 +73,15 @@ }); | ||
var defaults = merge({}, opts.defaults || {}); | ||
var ignoreUnknownFormats = opts.ignoreUnknownFormats; | ||
var unknownFormatHandler = ignoreUnknownFormats ? noop : rethrow; | ||
var unknownFormatHandler = opts.ignoreUnknownFormats ? noop : rethrow; | ||
return Bluebird.each(entries, function (entry) { | ||
return entry.read({ | ||
name: name, | ||
}).each(function (file) { | ||
return Bluebird.try( | ||
serializers.unserialize, | ||
[file] | ||
).then(function (value) { | ||
return fixPath(value, dirname(file.path)); | ||
}).then(function (value) { | ||
merge(defaults, value); | ||
}).catch(UnknownFormatError, unknownFormatHandler); | ||
}); | ||
return Bluebird.map(entries, function (entry) { | ||
return entry.read({ name: name }); | ||
}).then(flatten).map(function (file) { | ||
return Bluebird.try(unserialize, [file]).then(function (value) { | ||
return fixPaths(value, dirname(file.path)); | ||
}).catch(UnknownFormatError, unknownFormatHandler); | ||
}).each(function (value) { | ||
if (value) { | ||
merge(defaults, value); | ||
} | ||
}).return(defaults); | ||
@@ -91,0 +88,0 @@ } |
{ | ||
"name": "app-conf", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"description": "", | ||
"keywords": [], | ||
"scripts": { | ||
"test": "mocha index.spec.js" | ||
"test": "mocha --require must index.spec.js" | ||
}, | ||
@@ -17,7 +17,11 @@ "repository": { | ||
"devDependencies": { | ||
"chai": "^1.9.1", | ||
"mocha": "^1.20.1" | ||
"mocha": "^2.1.0", | ||
"mock-fs": "^2.5.0", | ||
"must": "^0.12.0" | ||
}, | ||
"files": [ | ||
"*.js" | ||
"index.js", | ||
"entries.js", | ||
"serializers.js", | ||
"unknown-format-error.js" | ||
], | ||
@@ -24,0 +28,0 @@ "dependencies": { |
@@ -1,5 +0,42 @@ | ||
Those packages can be installed to provided some features: | ||
# app-conf [![Build Status](https://travis-ci.org/julien-f/nodejs-app-conf.png?branch=master)](https://travis-ci.org/julien-f/nodejs-app-conf) | ||
- [ini](https://www.npmjs.org/package/ini): to support INI files | ||
- [js-yaml](https://www.npmjs.org/package/js-yaml): to support YAML files | ||
- [strip-json-comments](https://www.npmjs.org/package/strip-json-comments): to support comments in JSON files | ||
## Usage | ||
```javascript | ||
var loadConfig = require('app-conf').load; | ||
loadConfig('my-application').then(function (config) { | ||
console.log(config); | ||
}); | ||
``` | ||
The following files are looked up and merged (the latest take | ||
precedence): | ||
- `config.*` in the project directory; | ||
- `/etc/my-application/config.*`; | ||
- `~/.config/my-application/config.*`; | ||
- `/.my-application.*` down to `./.my-application.*` in the current | ||
working directory. | ||
JSON format is supported natively but you may install the following | ||
packages to have additional features: | ||
- [ini](https://www.npmjs.org/package/ini): to support INI files; | ||
- [js-yaml](https://www.npmjs.org/package/js-yaml): to support YAML files; | ||
- [strip-json-comments](https://www.npmjs.org/package/strip-json-comments): to support comments in JSON files. | ||
## Contributing | ||
Contributions are *very* welcome, either on the documentation or on | ||
the code. | ||
You may: | ||
- report any [issue](https://github.com/julien-f/human-format/issues) | ||
you've encountered; | ||
- fork and create a pull request. | ||
## License | ||
ISC © [Julien Fontanet](http://julien.isonoe.net) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9747
43
3
6
249