wild-config
Advanced tools
Comparing version 1.0.0 to 1.1.0
69
index.js
@@ -1,2 +0,2 @@ | ||
/* eslint no-console: 0 */ | ||
/* eslint no-console: 0, global-require: 0 */ | ||
'use strict'; | ||
@@ -10,8 +10,7 @@ | ||
const deepExtend = require('deep-extend'); | ||
const defaultPath = path.join(process.cwd(), 'config', 'default.toml'); | ||
const envPath = path.join(process.cwd(), 'config', env + '.toml'); | ||
const configDirectory = path.join(process.cwd(), 'config'); | ||
const events = new EventEmitter(); | ||
const argv = require('minimist')(process.argv.slice(2)); | ||
const configPath = argv.config || argv.c; | ||
const configPath = argv.config || argv.c || false; | ||
@@ -29,2 +28,4 @@ module.exports = {}; | ||
try { | ||
let pathParts = path.parse(filePath); | ||
let ext = pathParts.ext.toLowerCase(); | ||
let stat = fs.statSync(filePath); | ||
@@ -34,3 +35,25 @@ if (!stat.isFile()) { | ||
} | ||
sources.push(toml.parse(fs.readFileSync(filePath, 'utf-8'))); | ||
let parsed; | ||
if (ext === '.js') { | ||
if (filePath.indexOf('/') !== '/') { | ||
filePath = path.join(process.cwd(), filePath); | ||
} | ||
parsed = require(filePath); | ||
} else { | ||
let contents = fs.readFileSync(filePath, 'utf-8'); | ||
switch (ext) { | ||
case '.toml': | ||
parsed = toml.parse(contents); | ||
break; | ||
case '.json': | ||
parsed = JSON.parse(contents); | ||
break; | ||
} | ||
} | ||
if (parsed) { | ||
sources.push(parsed); | ||
} | ||
} catch (E) { | ||
@@ -45,6 +68,38 @@ if (E.code !== 'ENOENT' || !ignoreMissing) { | ||
loadFromFile(defaultPath, true); | ||
loadFromFile(envPath, true); | ||
try { | ||
let listing = fs.readdirSync(configDirectory); | ||
listing | ||
.map(file => ({ | ||
name: file, | ||
isDefault: file.toLowerCase().indexOf('default.') === 0, | ||
path: path.join(configDirectory, file) | ||
})) | ||
.filter(file => { | ||
let parts = path.parse(file.name); | ||
if (!['.toml', '.json', '.js'].includes(parts.ext.toLowerCase())) { | ||
return false; | ||
} | ||
if (!['default', env].includes(parts.name.toLowerCase())) { | ||
return false; | ||
} | ||
return true; | ||
}) | ||
.sort((a, b) => { | ||
if (a.isDefault) { | ||
return -1; | ||
} | ||
if (b.isDefault) { | ||
return 1; | ||
} | ||
return a.path.localeCompare(b.path); | ||
}) | ||
.forEach(file => loadFromFile(file.path)); | ||
} catch (E) { | ||
// failed to list files | ||
} | ||
// try user specified file | ||
loadFromFile(configPath); | ||
// join found files | ||
let data = deepExtend(...sources); | ||
@@ -51,0 +106,0 @@ |
{ | ||
"name": "wild-config", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Configuration management module", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,10 +5,15 @@ # wild-config | ||
*wild-config* loads configuration from application config directory and from a config path if provided. All configuration files use [toml](https://github.com/toml-lang/toml) syntax. | ||
* Config files use either [toml](https://github.com/toml-lang/toml), JSON or JavaScript syntax | ||
* If the config file format is javascript, then the value must be exported with "module.exports = {...}" | ||
* The application can have a config file for default values in ./config/default.toml | ||
* Main config file path can be provided from a command line argument, eg. `--config=/etc/app.toml` | ||
* Additionally command line arguments can be used to override any existing config option | ||
* *wild-config* detects SIGHUP and reloads configuration files automatically | ||
### Loading order | ||
*wild-config* tries to load configuration in the following order (missing files are skipped, except the one provided by --config argument). Values are merged. | ||
*wild-config* tries to load configuration in the following order (missing files are skipped, except the one provided by `--config` argument). Values are merged. | ||
1. ./config/default.toml | ||
2. ./config/$NODE_ENV.toml | ||
1. ./config/default.* | ||
2. ./config/$NODE_ENV.* | ||
3. `--config` or `-c` argument value | ||
@@ -19,5 +24,5 @@ 4. command line arguments | ||
In case of command line arguments only such keys are merged that already exist in the configuration object. For subkeys use dot notation. Value type (numbers, booleans and strings are supported) is defined by existing value. | ||
When using command line arguments to provide config values only such keys are merged that already exist in the configuration object, unknown keys are ignored. For subkeys use dot notation. Value type (numbers, booleans and strings are supported) is defined by existing value. | ||
Example default.toml | ||
Example *config/default.toml*: | ||
@@ -29,13 +34,15 @@ ```toml | ||
Override server.enabled with the following command line: | ||
Override `server.enabled` value with the following command line argument: | ||
node app.js --server.enabled=true | ||
`server.enabled` is defined as a boolean in the config file, so the overriden value is also going to be `true` as a boolean and not `"true"` as a string. | ||
#### Application config file | ||
If you are running your app as a service daemon, then you can load configuration from a config file from a common config folder, eg. /etc by using the ``--config` argument. These values are loaded and merged with the default values. | ||
If you are running your app as a service daemon, then you can load configuration from a config file by using the `--config` argument. These values are loaded and merged with the default values. | ||
``` | ||
[Service] | ||
WorkingDirectory=/opt/application | ||
WorkingDirectory=/opt/app | ||
ExecStart=/usr/bin/node index.js --config=/etc/app.toml | ||
@@ -48,4 +55,3 @@ ``` | ||
const config = require('wild-config'); | ||
console.log(config.server.enabled); // false | ||
console.log(config.server.enabled); | ||
``` | ||
@@ -60,3 +66,2 @@ | ||
config.on('reload', ()=>{ | ||
console.log('Configuration was updated'); | ||
console.log('New "server.enabled" value: %s', config.server.enabled); | ||
@@ -68,3 +73,3 @@ }); | ||
* *'reload'* emitted when SIGHUP is received | ||
* *'reload'* emitted when SIGHUP is received and configuration is reloaded | ||
@@ -74,2 +79,3 @@ ### Limitations | ||
* You can not use "on" as a root key. If you do then it is ignored. This key is reserved for the event emitter handler. | ||
* When providing configuration options from command line then `--config` does not override `root.config` value (if it even exists). This argument is used only for defining the configuration file path. | ||
@@ -76,0 +82,0 @@ ## Licese |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
10101
11
176
78
3