require-all
Advanced tools
Comparing version 0.0.6 to 0.0.8
21
index.js
@@ -1,5 +0,13 @@ | ||
var fs = require('fs'); | ||
var fs = require('fs'); | ||
module.exports = function requireAll(options) { | ||
var files = fs.readdirSync(options.dirname); | ||
if (typeof options === 'string') { | ||
options = { | ||
dirname: options, | ||
filter: /(.+)\.js(on)?$/, | ||
excludeDirs: /^\.(git|svn)$/ | ||
}; | ||
} | ||
var files = fs.readdirSync(options.dirname); | ||
var modules = {}; | ||
@@ -11,3 +19,3 @@ | ||
files.forEach(function(file) { | ||
files.forEach(function (file) { | ||
var filepath = options.dirname + '/' + file; | ||
@@ -19,5 +27,5 @@ if (fs.statSync(filepath).isDirectory()) { | ||
modules[file] = requireAll({ | ||
dirname : filepath, | ||
filter : options.filter, | ||
excludeDirs : options.excludeDirs | ||
dirname: filepath, | ||
filter: options.filter, | ||
excludeDirs: options.excludeDirs | ||
}); | ||
@@ -35,2 +43,1 @@ | ||
}; | ||
@@ -5,3 +5,3 @@ { | ||
"description": "An easy way to require all files within a directory.", | ||
"version": "0.0.6", | ||
"version": "0.0.8", | ||
"scripts": { | ||
@@ -25,4 +25,6 @@ "test": "node test/test.js" | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"devDependencies": { | ||
"semver": "~2.1.0" | ||
}, | ||
"optionalDependencies": {} | ||
} |
@@ -18,1 +18,9 @@ # require-all | ||
``` | ||
## Advanced usage | ||
If your objective is to simply require all .js and .json files in a directory you can just pass a string to require-all: | ||
``` js | ||
var libs = require('require-all')(__dirname + '/lib'); | ||
``` |
var assert = require('assert'); | ||
var semver = require('semver'); | ||
var requireAll = require('..'); | ||
@@ -23,4 +24,6 @@ | ||
if (process.version > 'v0.6.0') { | ||
// | ||
// requiring json only became an option in 0.6+ | ||
// | ||
if (semver.gt(process.version, 'v0.6.0')) { | ||
var mydir = requireAll({ | ||
@@ -31,10 +34,22 @@ dirname: __dirname + '/mydir', | ||
assert.deepEqual(mydir, { | ||
var mydir_contents = { | ||
foo: 'bar', | ||
hello: { world: true, universe: 42 }, | ||
hello: { | ||
world: true, | ||
universe: 42 | ||
}, | ||
sub: { | ||
config: { settingA: 'A', settingB: 'B' }, | ||
config: { | ||
settingA: 'A', | ||
settingB: 'B' | ||
}, | ||
yes: true | ||
} | ||
}); | ||
}; | ||
assert.deepEqual(mydir, mydir_contents); | ||
var defaults = requireAll(__dirname + '/mydir'); | ||
assert.deepEqual(defaults, mydir_contents); | ||
} | ||
@@ -48,4 +63,4 @@ | ||
assert(unfiltered['.svn']); | ||
assert(unfiltered['root']); | ||
assert(unfiltered['sub']); | ||
assert(unfiltered.root); | ||
assert(unfiltered.sub); | ||
@@ -59,4 +74,4 @@ var excludedSvn = requireAll({ | ||
assert.equal(excludedSvn['.svn'], undefined); | ||
assert.ok(excludedSvn['root']); | ||
assert.ok(excludedSvn['sub']); | ||
assert.ok(excludedSvn.root); | ||
assert.ok(excludedSvn.sub); | ||
@@ -70,3 +85,3 @@ var excludedSvnAndSub = requireAll({ | ||
assert.equal(excludedSvnAndSub['.svn'], undefined); | ||
assert.ok(excludedSvnAndSub['root']); | ||
assert.equal(excludedSvnAndSub['sub'], undefined); | ||
assert.ok(excludedSvnAndSub.root); | ||
assert.equal(excludedSvnAndSub.sub, undefined); |
Sorry, the diff of this file is not supported yet
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
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
5272
116
26
1
1