Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #8 from dscape/defaults

Adds sane defaults
  • Loading branch information...
commit de3060dcaa011075897522125d291e751d428962 2 parents f6d98d3 + 0660a5f
@dscape dscape authored
Showing with 54 additions and 21 deletions.
  1. +1 −0  .gitignore
  2. +8 −0 Readme.md
  3. +14 −7 index.js
  4. +4 −2 package.json
  5. +27 −12 test/test.js
View
1  .gitignore
@@ -1 +1,2 @@
*.un~
+node_modules/
View
8 Readme.md
@@ -15,3 +15,11 @@ var controllers = require('require-all')({
// for example:
// { HomeController: function HomeController() {...}, ...}
```
+
+## 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');
+```
View
21 index.js
@@ -1,23 +1,31 @@
-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 = {};
function excludeDirectory(dirname) {
return options.excludeDirs && dirname.match(options.excludeDirs);
}
- files.forEach(function(file) {
+ files.forEach(function (file) {
var filepath = options.dirname + '/' + file;
if (fs.statSync(filepath).isDirectory()) {
if (excludeDirectory(file)) return;
modules[file] = requireAll({
- dirname : filepath,
- filter : options.filter,
- excludeDirs : options.excludeDirs
+ dirname: filepath,
+ filter: options.filter,
+ excludeDirs: options.excludeDirs
});
} else {
@@ -30,4 +38,3 @@ module.exports = function requireAll(options) {
return modules;
};
-
View
6 package.json
@@ -2,7 +2,7 @@
"author": "Felix Geisendörfer <felix@debuggable.com> (http://debuggable.com/)",
"name": "require-all",
"description": "An easy way to require all files within a directory.",
- "version": "0.0.6",
+ "version": "0.0.7",
"scripts": {
"test": "node test/test.js"
},
@@ -21,6 +21,8 @@
"node": "*"
},
"dependencies": {},
- "devDependencies": {},
+ "devDependencies": {
+ "semver": "~2.1.0"
+ },
"optionalDependencies": {}
}
View
39 test/test.js
@@ -1,4 +1,5 @@
var assert = require('assert');
+var semver = require('semver');
var requireAll = require('..');
var controllers = requireAll({
@@ -20,21 +21,35 @@ assert.deepEqual(controllers, {
}
});
-
-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({
dirname: __dirname + '/mydir',
filter: /(.+)\.(js|json)$/
});
- 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);
}
var unfiltered = requireAll({
@@ -43,8 +58,8 @@ var unfiltered = requireAll({
});
assert(unfiltered['.svn']);
-assert(unfiltered['root']);
-assert(unfiltered['sub']);
+assert(unfiltered.root);
+assert(unfiltered.sub);
var excludedSvn = requireAll({
dirname: __dirname + '/filterdir',
@@ -53,8 +68,8 @@ var excludedSvn = requireAll({
});
assert.equal(excludedSvn['.svn'], undefined);
-assert.ok(excludedSvn['root']);
-assert.ok(excludedSvn['sub']);
+assert.ok(excludedSvn.root);
+assert.ok(excludedSvn.sub);
var excludedSvnAndSub = requireAll({
dirname: __dirname + '/filterdir',
@@ -63,5 +78,5 @@ 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);
Please sign in to comment.
Something went wrong with that request. Please try again.