Comparing version 0.2.5 to 0.2.7
@@ -10,2 +10,4 @@ /******************************************************************************* | ||
var ext = require('./extensions'); | ||
var File = require('fs'); | ||
var Yaml = require('yaml'); | ||
@@ -15,2 +17,5 @@ // Saved configurations key=moduleName, value=configObj | ||
// Saved configuration files. key=filename, value=configObj | ||
var savedConfigFiles = {}; | ||
/******************************************************************************* | ||
@@ -45,11 +50,52 @@ * config() - Build a module configuration object | ||
if (argName == arg && ++pos < process.argv.length) { | ||
// If the filename starts with "./", then make it relative to | ||
// the process CWD vs. this modules file location | ||
var confFile = process.argv[pos]; | ||
if (confFile.indexOf('./') == 0) { | ||
confFile = process.cwd() + confFile.substr(1); | ||
// If the filename is relative, make it relative to the process cwd | ||
var configFile = process.argv[pos]; | ||
if (configFile.indexOf('./') == 0 || configFile.indexOf('../') == 0) { | ||
configFile = process.cwd() + '/' + configFile; | ||
} | ||
// This will fail if it can't load the file | ||
_.extendDeep(mixedConfig, require(confFile)[modName]); | ||
// Get the configuration object from the file | ||
var configObject = savedConfigFiles[configFile]; | ||
if (!configObject) { | ||
// Determine the file type | ||
var fileParts = configFile.split('.'); | ||
lastPart = fileParts[fileParts.length - 1].toLowerCase(); | ||
var isYAML = (lastPart == 'yaml'); | ||
var isJSON = (lastPart == 'json'); | ||
var isJS = (!isYAML && !isJSON); | ||
// Load and parse the file into a javascript object | ||
try { | ||
if (isYAML) { | ||
var text = File.readFileSync(configFile).toString(); | ||
// Yaml library doesn't like strings that have newlines but don't end in | ||
// a newline: https://github.com/visionmedia/js-yaml/issues/issue/13 | ||
text += '\n'; | ||
configObject = Yaml.eval(text); | ||
} | ||
else if (isJSON) { | ||
var text = File.readFileSync(configFile).toString(); | ||
configObject = JSON.parse(text); | ||
} | ||
else { | ||
configObject = require(configFile); | ||
} | ||
// Remember the object so the file doesn't have to be parsed | ||
// again for the next module. | ||
savedConfigFiles[configFile] = configObject; | ||
} | ||
catch (e) | ||
{ | ||
console.log("\nError parsing config file: " + configFile); | ||
console.log(e.message); | ||
process.exit(1); | ||
} | ||
} | ||
// Mixin the module from the configuration file object | ||
_.extendDeep(mixedConfig, configObject[modName]); | ||
} | ||
@@ -56,0 +102,0 @@ }); |
{ | ||
"name": "config", | ||
"version": "0.2.5", | ||
"version": "0.2.7", | ||
"main": "./lib/config.js", | ||
@@ -10,5 +10,6 @@ "description": "Runtime configuration for node.js deployment", | ||
"dependencies": { | ||
"underscore" : ">=1.1.3", | ||
"vows" : ">=0.5.2", | ||
"eyes" : ">=0.1.6" | ||
"underscore" : "1.1.4", | ||
"vows" : "0.5.3", | ||
"eyes" : "0.1.6", | ||
"yaml" : "0.1.1" | ||
}, | ||
@@ -15,0 +16,0 @@ "engines": {"node": ">=0.2.4"}, |
@@ -65,3 +65,3 @@ /******************************************************************************* | ||
'Alpha configuration was mixed in': function() { | ||
process.argv = ['arg1', '-config', '../config/alpha.js']; | ||
process.argv = ['arg1', '-config', './config/alpha.js']; | ||
var conf = config('Customers', defaultParms); | ||
@@ -76,4 +76,4 @@ var shouldBe = _.extendDeep({}, defaultParms, { | ||
'Multiple configurations can be mixed in': function() { | ||
process.argv = ['-config', '../config/base.js', 'arg1', | ||
'-config', '../config/alpha.js', 'arg2']; | ||
process.argv = ['-config', './config/base.js', 'arg1', | ||
'-config', './config/alpha.js', 'arg2']; | ||
var conf = config('Customers', defaultParms); | ||
@@ -100,3 +100,3 @@ var shouldBe = _.extendDeep({}, defaultParms, { | ||
'Command line configurations override file configurations': function() { | ||
process.argv = ['-config', '../config/base.js', 'arg1', | ||
process.argv = ['-config', './config/base.js', 'arg1', | ||
'-Customers.dbName', 'cmdLineName']; | ||
@@ -112,3 +112,3 @@ var conf = config('Customers', defaultParms); | ||
'Configurations can be programmatically extended': function() { | ||
process.argv = ['arg1', '-config', '../config/production']; | ||
process.argv = ['arg1', '-config', './config/production']; | ||
var conf = config('Customers', defaultParms); | ||
@@ -125,3 +125,3 @@ var shouldBe = _.extendDeep({}, defaultParms, { | ||
'Configurations can be programmatically extended': function() { | ||
process.argv = ['arg1', '-config', '../config/production']; | ||
process.argv = ['arg1', '-config', './config/production']; | ||
var conf = config('Customers', defaultParms); | ||
@@ -172,11 +172,26 @@ var shouldBe = _.extendDeep({}, defaultParms, { | ||
/* | ||
'Configuration can be retrieved later': function() { | ||
assert.deepEqual(config('test'), module.shouldBe); | ||
'JSON configuration files can be loaded': function() { | ||
process.argv = ['-config', './config/base.json', 'arg1', | ||
'-config', './config/alpha.js', 'arg2']; | ||
var conf = config('Customers', defaultParms); | ||
var shouldBe = _.extendDeep({}, defaultParms, { | ||
dbName:'base_customers', | ||
dbHost:"alpha", | ||
dbPort:5999 | ||
}); | ||
assert.deepEqual(conf, shouldBe); | ||
}, | ||
'All configurations can be retrieved': function() { | ||
assert.isObject(config()); | ||
'YAML configuration files can be loaded': function() { | ||
process.argv = ['-config', './config/base.yaml', 'arg1', | ||
'-config', './config/alpha.js', 'arg2']; | ||
var conf = config('Customers', defaultParms); | ||
var shouldBe = _.extendDeep({}, defaultParms, { | ||
dbName:'base_customers', | ||
dbHost:"alpha", | ||
dbPort:5999 | ||
}); | ||
assert.deepEqual(conf, shouldBe); | ||
}, | ||
*/ | ||
'Resetting command line args': function(orig) { | ||
@@ -183,0 +198,0 @@ process.argv = orig.argv; |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
30381
13
644
4
3
+ Addedyaml@0.1.1
+ Addedeyes@0.1.6(transitive)
+ Addedunderscore@1.1.4(transitive)
+ Addedyaml@0.1.1(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removeddiff@4.0.2(transitive)
- Removedeyes@0.1.8(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@7.2.3(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedunderscore@1.13.7(transitive)
- Removedvows@0.8.3(transitive)
- Removedwrappy@1.0.2(transitive)
Updatedeyes@0.1.6
Updatedunderscore@1.1.4
Updatedvows@0.5.3