cfg-manager
Advanced tools
Comparing version 0.0.2 to 1.0.0
65
index.js
@@ -1,64 +0,1 @@ | ||
'use strict'; | ||
var objectAssign = require('object-assign'); | ||
module.exports = { | ||
/** | ||
* Parameter prefix of environment variables | ||
* We only parse environment variables starting with this prefix | ||
* | ||
* @default {String} APP_DEBUG=1, APP_DIR_PATH=path/to/somewhere | ||
*/ | ||
prefix: 'APP_', | ||
_isNumeric: function(number) { | ||
return !isNaN(parseFloat(number)) && isFinite(number); | ||
}, | ||
/** | ||
* Merging default configuration with user provided environment variables | ||
* | ||
* @param {Object} defaultConfig The config object | ||
* @param {Object} options for setting arguments | ||
* @param {String} options.prefix Set prefix of environment variable | ||
* @return {Object} return a config object | ||
*/ | ||
init: function(defaultConfig, options) { | ||
options = options || {}; | ||
var prefix = options.prefix || this.prefix; | ||
var config = objectAssign({}, defaultConfig || {}); | ||
var env = process.env; | ||
for (var key in env) { | ||
var realKey = key.substr(prefix.length); | ||
if (key.indexOf(prefix) === 0 && config[realKey] !== undefined) { | ||
var value = env[key]; | ||
// Convert string into number if it's a number | ||
config[realKey] = this._isNumeric(value) ? parseFloat(value) : value; | ||
} | ||
} | ||
// Keep config object as an environment variable | ||
env.CONFIG_MANAGER_ENVS = JSON.stringify(config); | ||
return config; | ||
}, | ||
/** | ||
* Get config from CONFIG_MANAGER_ENVS environment variable | ||
* | ||
* @return {Object} config object | ||
*/ | ||
getConfig: function() { | ||
var envs = process.env.CONFIG_MANAGER_ENVS; | ||
if (envs) { | ||
return JSON.parse(envs); | ||
} else { | ||
throw new Error('Cannot get settings from environment variables, ' + | ||
'please try to invoke init() before getConfig()'); | ||
} | ||
} | ||
}; | ||
module.exports = require('./dist/cfg-manager'); |
{ | ||
"name": "cfg-manager", | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"author": "Ricky Chien <ricky060709@gmail.com>", | ||
@@ -15,11 +15,10 @@ "description": "Managing config and environment variables", | ||
"readme": "README.md", | ||
"dependencies": { | ||
"object-assign": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"babel": "^5.8.3", | ||
"mocha": "^2.2.5" | ||
}, | ||
"scripts": { | ||
"prepublish": "mkdir -p dist && ./node_modules/.bin/babel src --out-dir dist", | ||
"test": "./node_modules/.bin/mocha" | ||
} | ||
} |
@@ -16,3 +16,3 @@ # Cfg-manager | ||
``` | ||
$ myProgram DEBUG=1 FILE_PATH=path/to/somewhere | ||
$ execMyProgram DEBUG=1 FILE_PATH=path/to/somewhere | ||
``` | ||
@@ -24,53 +24,24 @@ | ||
You can simply initialize config from an entry module and access it from sub-modules without re-initializing again. | ||
Initialize and generate merged config which is also stored in environment varialbe with the name CONFIG_MANAGER_ENVS. | ||
In entry.js, simply use: | ||
```javascript | ||
var subModule = require('sub-modules'); | ||
var cfgManager = require('cfg-manager'); | ||
// Pass your default configuration as first argument | ||
// you could read your defaultConfig from a file or an object | ||
var config = cfgManager.init(defaultConfig); | ||
``` | ||
Simply access config with getConfig() once you already init() in your config from entry module. | ||
In sub-modules.js: | ||
```javascript | ||
var cfgManager = require('cfg-manager'); | ||
var config = cfgManager.getConfig(); | ||
``` | ||
### APIs | ||
#### init(defaultConfig, [options]) | ||
Initialize config object by merging given confg object and given environment variables. | ||
options: ```Object``` | ||
## Development | ||
return: ```Object``` | ||
### Build | ||
##### options.prefix | ||
This project built with ES6, so you should transpile it to ES5 through babel after modifying any codebase. | ||
Environment variable prefix. Cfg-manager only parse environment variables if they begin with given prefix. | ||
``` | ||
$ npm install // Trigger babel transpiler to convert source from ES6 to ES5. | ||
``` | ||
type: ```string``` | ||
### Running test | ||
default: ```'APP_'``` | ||
We use [mocha](http://mochajs.org/) for testing and it is able to transpile ES6 to ES5 automatically. | ||
#### getConfig() | ||
Return config object directly if config has been initialized and existed in environment variable CONFIG_MANAGER_ENVS. | ||
## Development | ||
### Running unit test | ||
``` | ||
npm test | ||
$ npm test | ||
``` |
@@ -1,53 +0,58 @@ | ||
var cfgManager = require('../index'); | ||
var assert = require('assert'); | ||
import assert from 'assert'; | ||
import CfgManager from '../index'; | ||
suite('Config Manager', function() { | ||
suite('Config Manager', () => { | ||
var cfg; | ||
var defaultConfig; | ||
var expect; | ||
var env = process.env; | ||
suiteSetup(function() { | ||
defaultConfig = { | ||
DEBUG: 0, | ||
PRODUCTION: 0, | ||
OPTIMIZE: 0, | ||
setup(() => { | ||
cfg = new CfgManager(); | ||
process.env = { | ||
DEBUG: '0', | ||
PRODUCTION: '0', | ||
DIR_PATH: 'path/to/dir' | ||
}; | ||
}); | ||
expect = { | ||
DEBUG: 1, | ||
PRODUCTION: 0, | ||
OPTIMIZE: 0, | ||
DIR_PATH: 'path/to/test' | ||
}; | ||
test('#file()', () => { | ||
cfg.file(`${__dirname}/config.json`); | ||
assert.strictEqual(cfg.get('FILE_PATH'), 'path/to/file'); | ||
assert.strictEqual(cfg.get('LOCALE'), 'en-US'); | ||
assert.strictEqual(cfg.get('ENABLE'), '1'); | ||
assert.strictEqual(cfg.get('VALUE'), '100'); | ||
}); | ||
test('#init()', function() { | ||
env.APP_DEBUG = '1'; | ||
env.APP_DIR_PATH = 'path/to/test'; | ||
env.APP_NON_EXIST_FLAG = 'path/to/somewhere'; | ||
let config = cfgManager.init(defaultConfig); | ||
test('#config()', () => { | ||
cfg.config({ DEBUG: '0', DIR_PATH: 'path/to/config_1' }) | ||
.config({ PRODUCTION: '0', DIR_PATH: 'path/to/config_2' }); | ||
assert.deepEqual(config, expect); | ||
assert.strictEqual(typeof(config.DEBUG), 'number'); | ||
assert.strictEqual(typeof(config.DIR_PATH), 'string'); | ||
assert.strictEqual(cfg.get('DEBUG'), '0'); | ||
assert.strictEqual(cfg.get('PRODUCTION'), '0'); | ||
assert.strictEqual(cfg.get('DIR_PATH'), 'path/to/config_2'); | ||
}); | ||
test('#init() with a specific prefix', function() { | ||
env.TEST_DEBUG = '1'; | ||
env.TEST_DIR_PATH = 'path/to/test'; | ||
env.TEST_NON_EXIST_FLAG = 'path/to/somewhere'; | ||
let config = cfgManager.init(defaultConfig, { prefix: 'TEST_' }); | ||
test('#env()', () => { | ||
cfg.env(); | ||
assert.deepEqual(config, expect); | ||
assert.strictEqual(typeof(config.DEBUG), 'number'); | ||
assert.strictEqual(typeof(config.DIR_PATH), 'string'); | ||
assert.strictEqual(cfg.get('DEBUG'), '0'); | ||
assert.strictEqual(cfg.get('PRODUCTION'), '0'); | ||
assert.strictEqual(cfg.get('DIR_PATH'), 'path/to/dir'); | ||
}); | ||
test('#getConfig()', function() { | ||
cfgManager.init(defaultConfig); | ||
test('#env(whitelist)', () => { | ||
cfg.env(['DEBUG', 'DIR_PATH']); | ||
assert.deepEqual(cfgManager.getConfig(), expect); | ||
assert.strictEqual(cfg.get('DEBUG'), '0'); | ||
assert.strictEqual(cfg.get('DIR_PATH'), 'path/to/dir'); | ||
}); | ||
test('#get(name)', () => { | ||
cfg.config({ DEBUG: '1', DIR_PATH: 'path/to/config' }) | ||
.env(); | ||
assert.strictEqual(cfg.get('DEBUG'), '0'); | ||
assert.strictEqual(cfg.get('PRODUCTION'), '0'); | ||
assert.strictEqual(cfg.get('DIR_PATH'), 'path/to/dir'); | ||
assert.strictEqual(cfg.get('INEXISTENT'), undefined); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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 v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8077
0
10
204
1
2
46
5
1
- Removedobject-assign@^3.0.0
- Removedobject-assign@3.0.0(transitive)