Comparing version 1.0.0 to 1.1.0
@@ -18,8 +18,17 @@ 'use strict'; | ||
return Promise.all(realFiles.map(function(file) { | ||
return fsHelpers.read(file).then(function(content) { | ||
var parsedConfig = nodeEval('(' + content + ')'); | ||
var parsedConfig; | ||
try { | ||
parsedConfig = require(file); | ||
parsedConfig.__source = file; | ||
return parsedConfig; | ||
}); | ||
return Promise.resolve(parsedConfig); | ||
} catch (err) { | ||
return fsHelpers.read(file).then(function(content) { | ||
parsedConfig = nodeEval('(' + content + ')'); | ||
parsedConfig.__source = file; | ||
return parsedConfig; | ||
}); | ||
} | ||
})); | ||
@@ -26,0 +35,0 @@ }); |
@@ -24,3 +24,3 @@ 'use strict'; | ||
try { | ||
parsedConfig = nodeEval('(' + fs.readFileSync(file, 'utf8') + ')'); | ||
parsedConfig = require(file); | ||
} catch (err) { | ||
@@ -30,2 +30,10 @@ // | ||
if (!parsedConfig) { | ||
try { | ||
parsedConfig = nodeEval('(' + fs.readFileSync(file, 'utf8') + ')'); | ||
} catch (err) { | ||
// | ||
} | ||
} | ||
if (!parsedConfig) { return configs; } | ||
@@ -32,0 +40,0 @@ |
{ | ||
"name": "betterc", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Like rc but better", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# betterc | ||
Like `rc` but better ;) | ||
Like [rc](https://github.com/dominictarr/rc) but better ;) | ||
Searches for configs. | ||
Searches for configs and returns an array of all configs found. | ||
@@ -17,4 +17,92 @@ ## Installation | ||
var betterc = require('betterc'); | ||
betterc({ name: 'something' }); // for async version | ||
betterc.sync({ name: 'something' }); // for sync version | ||
betterc({ name: 'appname' }); | ||
betterc.sync({ name: 'appname' }); | ||
``` | ||
Given your application name (`appname`), `betterc` will look in all the obvious places for configuration: | ||
* the defaults object you passed in | ||
* `$HOME/.${appname}rc` | ||
* `$HOME/.${appname}/config` | ||
* `$HOME/.config/${appname}` | ||
* `$HOME/.config/${appname}/config` | ||
* a local `.${appname}rc` and all found looking in `./ ../ ../../ ../../../` etc. | ||
* if you passed environment variable `${appname}_config` then from that file | ||
* if you passed an option `--config file` then from that file | ||
* environment variables prefixed with `${appname}_` | ||
* or use "\_\_" to indicate nested properties <br/> _(e.g. `appname_foo__bar__baz` => `foo.bar.baz`)_ | ||
All configuration sources that were found will be added to result array. | ||
## Advanced usage | ||
```js | ||
var betterc = require('betterc'); | ||
betterc({ // or betterc.sync for sync version | ||
name: 'appname', | ||
cwd: 'current-working-directory', | ||
argv: { | ||
config: 'path-to-config' | ||
}, | ||
fsRoot: '/my-root', // custom '/' directory, used in tests | ||
fsHome: '/my-home', // custom $HOME directory, used in tests | ||
defaults: { | ||
foo: 'baz' | ||
} | ||
}); | ||
``` | ||
## Configuration file format | ||
Configuration files (e.g. `.appnamerc`) may be in [json](http://json.org/example) or CommonJS module format: | ||
### JSON | ||
```js | ||
{ | ||
"dependsOn": "0.10.0", | ||
"commands": { | ||
"www": "./commands/www", | ||
"console": "./commands/repl" | ||
}, | ||
"generators": { | ||
"options": { | ||
"engine": "ejs" | ||
}, | ||
"modules": { | ||
"new": "generate-new", | ||
"backend": "generate-backend" | ||
} | ||
} | ||
} | ||
``` | ||
### CommonJS | ||
```js | ||
module.exports = { | ||
"dependsOn": "0.10.0", | ||
"commands": { | ||
"www": "./commands/www", | ||
"console": "./commands/repl" | ||
}, | ||
"generators": { | ||
"options": { | ||
"engine": "ejs" | ||
}, | ||
"modules": { | ||
"new": "generate-new", | ||
"backend": "generate-backend" | ||
} | ||
} | ||
}; | ||
``` | ||
> Since env variables do not have a standard for types, your application needs be prepared for strings. | ||
## License | ||
Licensed under the MIT License. |
@@ -6,5 +6,3 @@ 'use strict'; | ||
const mock = require('mock-fs'); | ||
const mockFsHelper = require(path.join(__dirname, 'lib', 'mock-fs-helper')); | ||
const rc = require('../'); | ||
const nodeModules = mockFsHelper.duplicateFSInMemory(path.resolve('..', 'node_modules')); | ||
const osHomedir = require('os-homedir')(); | ||
@@ -20,5 +18,3 @@ | ||
test('should return empty config if no config found', async t => { | ||
mock({ | ||
node_modules: nodeModules | ||
}); | ||
mock({}); | ||
@@ -32,5 +28,3 @@ const actual = await rc(); | ||
test('should return config defaults', async t => { | ||
mock({ | ||
node_modules: nodeModules | ||
}); | ||
mock({}); | ||
@@ -44,5 +38,3 @@ const expected = [{ test: 1 }]; | ||
test('should find configs in home dir', async t => { | ||
const mockOpts = { | ||
node_modules: nodeModules | ||
}; | ||
const mockOpts = {}; | ||
@@ -72,5 +64,3 @@ const sources = [ | ||
test('should find configs with custom name in home dir', async t => { | ||
const mockOpts = { | ||
node_modules: nodeModules | ||
}; | ||
const mockOpts = {}; | ||
@@ -102,3 +92,2 @@ const sources = [ | ||
mock({ | ||
node_modules: nodeModules, | ||
'/test/.bemrc': '{"test": 1}' | ||
@@ -119,3 +108,2 @@ }); | ||
mock({ | ||
node_modules: nodeModules, | ||
'/test/.bemrc': '{"test": 1}' | ||
@@ -135,3 +123,2 @@ }); | ||
mock({ | ||
node_modules: nodeModules, | ||
'/test/.bemrc': '{"test": 1}' | ||
@@ -155,5 +142,3 @@ }); | ||
test('should use config field passed via ENV', async t => { | ||
mock({ | ||
node_modules: nodeModules, | ||
}); | ||
mock({}); | ||
@@ -174,3 +159,2 @@ process.env.bem_test = 1; | ||
mock({ | ||
node_modules: nodeModules, | ||
'/test/.bemrc': '{"test": 1}' | ||
@@ -195,3 +179,2 @@ }); | ||
mock({ | ||
node_modules: nodeModules, | ||
'.bemrc': '{"test": 1}' | ||
@@ -208,3 +191,2 @@ }); | ||
mock({ | ||
node_modules: nodeModules, | ||
'.ololorc': '{"test": 1}' | ||
@@ -221,3 +203,2 @@ }); | ||
mock({ | ||
node_modules: nodeModules, | ||
grandparent: { | ||
@@ -249,3 +230,2 @@ parent: { | ||
mock({ | ||
node_modules: nodeModules, | ||
grandparent: { | ||
@@ -275,3 +255,2 @@ parent: { | ||
mock({ | ||
node_modules: nodeModules, | ||
'/.bemrc': '{"test": "root"}', | ||
@@ -291,3 +270,2 @@ }); | ||
mock({ | ||
node_modules: nodeModules, | ||
'/.bemrc': '{"test": "root"}', | ||
@@ -308,3 +286,2 @@ '.bemrc': '{"test": 1}' | ||
const mockOpts = { | ||
node_modules: nodeModules, | ||
home: { | ||
@@ -331,3 +308,2 @@ '.bemrc': '{"test": 1}' | ||
'/.bemrc': '{"test": "root"}', | ||
node_modules: nodeModules | ||
}; | ||
@@ -349,3 +325,2 @@ | ||
const mockOpts = { | ||
node_modules: nodeModules, | ||
'/.bemrc': '{"test": "root"}', | ||
@@ -352,0 +327,0 @@ '/argv/.bemrc': '{"test": "argv"}', |
@@ -6,5 +6,3 @@ 'use strict'; | ||
const mock = require('mock-fs'); | ||
const mockFsHelper = require(path.join(__dirname, 'lib', 'mock-fs-helper')); | ||
const rc = require('../').sync; | ||
const nodeModules = mockFsHelper.duplicateFSInMemory(path.resolve('..', 'node_modules')); | ||
const osHomedir = require('os-homedir')(); | ||
@@ -20,5 +18,3 @@ | ||
test('should return empty config if no config found', t => { | ||
mock({ | ||
node_modules: nodeModules | ||
}); | ||
mock({}); | ||
@@ -29,5 +25,3 @@ t.deepEqual(rc(), [{}]); | ||
test('should return config defaults', t => { | ||
mock({ | ||
node_modules: nodeModules | ||
}); | ||
mock({}); | ||
@@ -38,5 +32,3 @@ t.deepEqual(rc({ defaults: { test: 1 } }), [{ test: 1 }]); | ||
test('should find configs in home dir', t => { | ||
const mockOpts = { | ||
node_modules: nodeModules | ||
}; | ||
const mockOpts = {}; | ||
@@ -66,5 +58,3 @@ const sources = [ | ||
test('should find configs with custom name in home dir', t => { | ||
const mockOpts = { | ||
node_modules: nodeModules | ||
}; | ||
const mockOpts = {}; | ||
@@ -95,3 +85,2 @@ const sources = [ | ||
mock({ | ||
node_modules: nodeModules, | ||
'/test/.bemrc': '{"test": 1}' | ||
@@ -109,3 +98,2 @@ }); | ||
mock({ | ||
node_modules: nodeModules, | ||
'/test/.bemrc': '{"test": 1}' | ||
@@ -125,3 +113,2 @@ }); | ||
mock({ | ||
node_modules: nodeModules, | ||
'/test/.bemrc': '{"test": 1}' | ||
@@ -142,5 +129,3 @@ }); | ||
test('should use config field passed via ENV', t => { | ||
mock({ | ||
node_modules: nodeModules, | ||
}); | ||
mock({}); | ||
@@ -158,3 +143,2 @@ process.env.bem_test = 1; | ||
mock({ | ||
node_modules: nodeModules, | ||
'/test/.bemrc': '{"test": 1}' | ||
@@ -176,3 +160,2 @@ }); | ||
mock({ | ||
node_modules: nodeModules, | ||
'.bemrc': '{"test": 1}' | ||
@@ -186,3 +169,2 @@ }); | ||
mock({ | ||
node_modules: nodeModules, | ||
'.ololorc': '{"test": 1}' | ||
@@ -196,3 +178,2 @@ }); | ||
mock({ | ||
node_modules: nodeModules, | ||
grandparent: { | ||
@@ -221,3 +202,2 @@ parent: { | ||
mock({ | ||
node_modules: nodeModules, | ||
grandparent: { | ||
@@ -244,3 +224,2 @@ parent: { | ||
mock({ | ||
node_modules: nodeModules, | ||
'/.bemrc': '{"test": "root"}', | ||
@@ -257,3 +236,2 @@ }); | ||
mock({ | ||
node_modules: nodeModules, | ||
'/.bemrc': '{"test": "root"}', | ||
@@ -271,3 +249,2 @@ '.bemrc': '{"test": 1}' | ||
const mockOpts = { | ||
node_modules: nodeModules, | ||
home: { | ||
@@ -291,3 +268,2 @@ '.bemrc': '{"test": 1}' | ||
'/.bemrc': '{"test": "root"}', | ||
node_modules: nodeModules | ||
}; | ||
@@ -310,3 +286,2 @@ | ||
const mockOpts = { | ||
node_modules: nodeModules, | ||
'/.bemrc': '{"test": "root"}', | ||
@@ -313,0 +288,0 @@ '/argv/.bemrc': '{"test": "argv"}', |
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
108
28
29586
16
693