Comparing version 1.0.2 to 2.0.0
72
conf.js
@@ -31,11 +31,17 @@ /** | ||
function _initFiles(basePath) { | ||
if (!basePath) { | ||
basePath = '.'; | ||
function _checkInitialized() { | ||
if(!initialized) { | ||
throw new Error("e2e-conf hasn't been initialized."); | ||
} | ||
} | ||
this.localDirectory = path.resolve(basePath, 'config/local'); | ||
this.localFileName = path.resolve(this.localDirectory, 'config.json'); | ||
this.defaultFileName = path.resolve(basePath, 'config/default/config.json'); | ||
function _initFiles(configPath, localConfig) { | ||
if (!configPath) { | ||
throw new Error("Cannot initialize e2e-conf without config path"); | ||
} | ||
this.defaultFileName = localConfig ? configPath : path.resolve(configPath, 'config/default/config.json'); | ||
this.localDirectory = localConfig ? path.dirname(localConfig) : path.resolve(configPath, 'config/local'); | ||
this.localFileName = localConfig ? localConfig : path.resolve(this.localDirectory, 'config.json'); | ||
nconf.file('local', {file: this.localFileName}) | ||
@@ -46,7 +52,9 @@ .file('default', {file: this.defaultFileName}); | ||
/** | ||
* Initialize the configuration with custom path. | ||
* Initialize the configuration with custom path(s). | ||
* | ||
* @param {String?} basePath Directory where the configuration files are looked up. | ||
* @param {String} configPath Directory where the configuration files are looked up or path to default config file. | ||
* @param {String?} localConfig Path to local config file. If given, configPath is interpreted as path to default config file. | ||
* | ||
*/ | ||
exports.init = function init(basePath) { | ||
exports.init = function init(configPath, localConfig) { | ||
if (!initialized) { | ||
@@ -57,3 +65,3 @@ | ||
_initFiles.apply(this, [basePath]); | ||
_initFiles.apply(this, arguments); | ||
@@ -67,11 +75,12 @@ initialized = true; | ||
/** | ||
* Initialize the configuration with custom path and only values from files and not from program arguments | ||
* Initialize the configuration with custom path(s) and only values from files and not from program arguments | ||
* or environment. | ||
* | ||
* @param {String} basePath Directory where the configuration files are looked up. | ||
* @param {String} configPath Directory where the configuration files are looked up or path to default config file. | ||
* @param {String?} localConfig Path to local config file. If given, configPath is interpreted as path to default config file. | ||
*/ | ||
exports.initOnlyFiles = function init(basePath) { | ||
exports.initOnlyFiles = function init(configPath, localConfig) { | ||
if (!initialized) { | ||
_initFiles.apply(this, [basePath]); | ||
_initFiles.apply(this, arguments); | ||
@@ -104,5 +113,3 @@ initialized = true; | ||
exports.get = function get(key) { | ||
if (!initialized) { | ||
this.init(); | ||
} | ||
_checkInitialized() | ||
@@ -119,5 +126,3 @@ return nconf.get(key); | ||
exports.set = function set(key, value) { | ||
if (!initialized) { | ||
this.init(); | ||
} | ||
_checkInitialized() | ||
@@ -133,5 +138,3 @@ return nconf.set(key, value); | ||
exports.setObject = function setObject(object) { | ||
if (!initialized) { | ||
this.init(); | ||
} | ||
_checkInitialized() | ||
@@ -150,11 +153,18 @@ Object.keys(object).forEach(function (prop) { | ||
var self = this; | ||
if (!initialized) { | ||
self.init(); | ||
} | ||
if (typeof actualConf === 'function') { | ||
callback = actualConf; | ||
actualConf = nconf.stores.local.store; | ||
actualConf = nconf.stores.local && nconf.stores.local.store; | ||
} | ||
try { | ||
_checkInitialized(); | ||
} catch(e) { | ||
if(typeof callback === 'function') { | ||
return callback(e); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
async.waterfall([ | ||
@@ -185,5 +195,3 @@ function (callback) { | ||
exports.localFile = function localFile() { | ||
if (!initialized) { | ||
this.init(); | ||
} | ||
_checkInitialized() | ||
@@ -199,5 +207,3 @@ return this.localFileName; | ||
exports.defaultFile = function defaultFile() { | ||
if (!initialized) { | ||
this.init(); | ||
} | ||
_checkInitialized() | ||
@@ -204,0 +210,0 @@ return this.defaultFileName; |
{ | ||
"name": "e2e-conf", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"main": "conf.js", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
@@ -5,4 +5,4 @@ # e2e-conf - Easy Configuration for E2E Bridge Node.js Services | ||
If you use this module to access your configuration values then you can edit your configuration with a nice UI inside | ||
the E2E Bridge. At each deployment for development, testing or production you can change the configuration to match | ||
If you use this module to access your configuration values then you can edit your configuration with a nice UI inside | ||
the E2E Bridge. At each deployment for development, testing or production you can change the configuration to match | ||
the environment and your needs. | ||
@@ -23,2 +23,3 @@ Technically it is a wrapper to [nconf](https://github.com/flatiron/nconf). | ||
var conf = require('e2e-conf'); | ||
conf.init(__dirname); | ||
@@ -31,3 +32,3 @@ var port = conf.get('host:port'); | ||
the default values. | ||
You can also use command-line arguments or environment variables to change the configuration values. The order is: | ||
@@ -55,8 +56,8 @@ 1. command-line arguments | ||
.env('__') | ||
.file('local', { file: './config/local/config.json') }) | ||
.file('default', { file: './config/default/config.json') }); | ||
.file('local', { file: __dirname + '/config/local/config.json') }) | ||
.file('default', { file: __dirname + '/config/default/config.json') }); | ||
``` | ||
## License | ||
## License | ||
@@ -63,0 +64,0 @@ (The MIT License) |
@@ -37,3 +37,3 @@ /** | ||
subprocess.on('close', function (code) { | ||
test.equals(1, code); | ||
test.equals(code, 1); | ||
test.done(); | ||
@@ -62,3 +62,3 @@ }); | ||
subprocess.on('close', function (code) { | ||
test.equals(0, code); | ||
test.equals(code, 0); | ||
test.done(); | ||
@@ -65,0 +65,0 @@ }); |
@@ -34,5 +34,9 @@ /** | ||
var conf = this.conf; | ||
test.equal(conf.get('hello'), 'from env'); | ||
test.equal(conf.get('version'), undefined); | ||
test.throws( | ||
function() { | ||
conf.get('hello'); | ||
}, | ||
Error, | ||
"e2e-conf hasn't been initialized." | ||
); | ||
test.done(); | ||
@@ -55,5 +59,5 @@ }, | ||
conf.init(); | ||
conf.init(__dirname); | ||
test.throws(function () { | ||
conf.init(); | ||
conf.init(__dirname); | ||
}, Error); | ||
@@ -60,0 +64,0 @@ test.done(); |
@@ -115,3 +115,3 @@ /** | ||
conf.save(function (err) { | ||
test.equals(err.code, "ENOENT"); | ||
test.equals(err.message, "e2e-conf hasn't been initialized."); | ||
@@ -118,0 +118,0 @@ test.done(); |
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
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
30427
816
82
2