Comparing version 0.0.4 to 0.0.5
{ | ||
"name": "lconf", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "parse local configuration files of various types", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -20,7 +20,24 @@ var yaml = require('js-yaml'), | ||
self.parse = function(path) { | ||
// Note: currently this means we only support absolute paths | ||
// no regex, must include ./ if same dir, no directory names | ||
self._paths.push(path); | ||
self.parse = function(fpath, regex) { | ||
var paths; | ||
if (fs.existsSync(fpath) && fs.statSync(fpath).isDirectory()) { | ||
paths = fs.readdirSync(fpath); | ||
for (var i = 0 ; i < paths.length; i++) { | ||
paths[i] = fpath+path.sep+paths[i]; | ||
} | ||
} else { | ||
paths = [fpath]; | ||
} | ||
for (var i = 0 ; i < paths.length; i++) { | ||
if (regex instanceof RegExp) { | ||
if (regex.test(paths[i])) { | ||
self._paths.push(paths[i]); | ||
} | ||
} else { | ||
self._paths.push(paths[i]); | ||
} | ||
} | ||
return self; | ||
@@ -75,3 +92,7 @@ }; | ||
if ((path = pathResolver(path)) !== false) { | ||
res[path] = load(path); | ||
if (!(path instanceof Array)) path = [path]; | ||
for (var j = 0; j < path.length; j++) { | ||
res[path[j]] = load(path[j]); | ||
} | ||
} else { | ||
@@ -78,0 +99,0 @@ throw new Error("path "+self._paths[i]+" is invalid"); |
@@ -8,4 +8,6 @@ # lconf [![Build Status](https://travis-ci.org/bengreenier/lconf.svg)](https://travis-ci.org/bengreenier/lconf) | ||
+ __parse(file:string)__: | ||
parses a configuration file located at `file`. note: `file` must be a path `fs` recognizes, so __be sure to use `./`__ when needed. | ||
+ __parse(file:string, [regex:RegExp])__: | ||
parses a configuration file located at `file`. note: if `file` is relative, it will be resolved from `process.cwd()`. if `file` is a directory, all | ||
files inside the directory will be included. if `regex` is given, `file` (if actual file) or all files in directory (if `file` is a directory) will | ||
be compared against regex, and added if `regex.test(filePath)` returns `true`. | ||
@@ -12,0 +14,0 @@ + __suppress(bool:boolean[default=true])__: |
@@ -21,2 +21,6 @@ var assert = require("assert"); | ||
fs.unlinkSync('_test.js'); | ||
fs.unlinkSync('./_dir/_test.js'); | ||
fs.unlinkSync('./_dir/_test.json'); | ||
fs.rmdirSync('./_dir'); | ||
} catch (e){} | ||
@@ -27,2 +31,6 @@ | ||
fs.writeFileSync('_test.js', 'module.exports = {"invoice": 83, "date":false,"bill-to":"ben"};'); | ||
fs.mkdirSync('./_dir'); | ||
fs.writeFileSync('./_dir/_test.json', '{"invoice": 34843, "date":false,"bill-to":"greenier", "bool":true}'); | ||
fs.writeFileSync('./_dir/_test.js', 'module.exports = {"invoice": 83, "date":false,"bill-to":"ben","bool":true};'); | ||
}); | ||
@@ -56,2 +64,11 @@ | ||
it('shouldn\'t parse json config if regex /.?\\.js$/', function() { | ||
var conf = lconf(); | ||
var opts = conf.parse('./_test.json', /.?\.js$/) | ||
.opts(); | ||
assert.deepEqual(opts, {}, "opts shouldn't be "+JSON.stringify(opts)); | ||
}); | ||
it('should parse js config', function() { | ||
@@ -70,2 +87,15 @@ var conf = lconf(); | ||
it('should parse js config if regex /.?\\.js$/', function() { | ||
var conf = lconf(); | ||
var opts = conf.parse('./_test.js', /.?\.js$/) | ||
.opts(); | ||
var op3 = {}; | ||
op3[pathResolver("./_test.js")] = {invoice: 83, date: false, "bill-to": "ben"}; | ||
assert.equal(typeof(opts), "object", "opts shouldn't be type: "+typeof(opts)); | ||
assert.deepEqual(opts, op3, "opts shouldn't be "+JSON.stringify(opts)); | ||
}); | ||
it('should parse multiple config', function() { | ||
@@ -160,3 +190,3 @@ var conf = lconf(); | ||
it('shouldn\'t throw if file has unsupported extension, and suppress() is used', function() { | ||
it('shouldn\'t throw if file has unsupported extension, and suppress is used', function() { | ||
var conf = lconf(); | ||
@@ -168,2 +198,31 @@ assert.doesNotThrow(function() { | ||
it('should support directories', function() { | ||
var conf = lconf(); | ||
var opts = conf.parse('./_dir/') | ||
.opts(); | ||
var op3 = {}; | ||
op3[pathResolver("./_dir/_test.js")] = {invoice: 83, date: false, "bill-to": "ben", "bool":true}; | ||
op3[pathResolver("./_dir/_test.json")] = {invoice: 34843, date: false, "bill-to": "greenier", "bool":true}; | ||
assert.equal(typeof(opts), "object", "opts shouldn't be type: "+typeof(opts)); | ||
assert.deepEqual(opts, op3, "opts shouldn't be "+JSON.stringify(opts)); | ||
}); | ||
it('should support directories with regex', function() { | ||
var conf = lconf(); | ||
var opts = conf.parse('./_dir/', /.?\.js$/) | ||
.opts(); | ||
var op3 = {}; | ||
op3[pathResolver("./_dir/_test.js")] = {invoice: 83, date: false, "bill-to": "ben", "bool":true}; | ||
assert.equal(typeof(opts), "object", "opts shouldn't be type: "+typeof(opts)); | ||
assert.deepEqual(opts, op3, "opts shouldn't be "+JSON.stringify(opts)); | ||
}); | ||
after(function() { | ||
@@ -173,3 +232,7 @@ fs.unlinkSync('_test.yaml'); | ||
fs.unlinkSync('_test.js'); | ||
fs.unlinkSync('./_dir/_test.js'); | ||
fs.unlinkSync('./_dir/_test.json'); | ||
fs.rmdirSync('./_dir'); | ||
}); | ||
}); |
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
Possible typosquat attack
Supply chain riskThere is a package with a similar name that is downloaded much more often.
Did you mean |
---|
conf |
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
15970
270
82
0