-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test and proper check for ignoring non javascript files
- Loading branch information
1 parent
0e1be56
commit 6ab36b4
Showing
5 changed files
with
58 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,52 @@ | ||
(function() { | ||
var fs = require('fs'), | ||
path = require('path'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
|
||
module.exports = function(dir, basenames) { | ||
var requires = {}; | ||
module.exports = function (dir, basenames) { | ||
var requires = {}; | ||
|
||
if (arguments.length === 1) { | ||
var files = fs.readdirSync(dir); | ||
|
||
// sort files in lowercase alpha for linux | ||
files.sort(function(a,b) { | ||
a = a.toLowerCase(); | ||
b = b.toLowerCase(); | ||
|
||
if (a < b) { | ||
return -1; | ||
} else if (b < a) { | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
}); | ||
|
||
files.forEach(function(filename) { | ||
|
||
if ((filename === 'index.js') || (filename[0] === '_')) { return; } | ||
|
||
var ext = path.extname(filename); | ||
if (!(ext in require.extensions)) { return; } | ||
if (arguments.length === 1) { | ||
// if basenames arguments isn't passed, require all javascript | ||
// files (except for those prefixed with _) and all directories | ||
|
||
filename = path.basename(filename, ext); | ||
var filepath = path.join(dir, filename); | ||
var files = fs.readdirSync(dir); | ||
|
||
// sort files in lowercase alpha for linux | ||
files.sort(function (a,b) { | ||
a = a.toLowerCase(); | ||
b = b.toLowerCase(); | ||
|
||
if (a < b) { | ||
return -1; | ||
} else if (b < a) { | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
}); | ||
|
||
requires[filename] = require(path.resolve(filepath)); | ||
}); | ||
files.forEach(function (filename) { | ||
// ignore index.js and files prefixed with underscore | ||
if ((filename === 'index.js') || (filename[0] === '_')) { return; } | ||
|
||
var filepath = path.resolve(path.join(dir, filename)); | ||
var ext = path.extname(filename); | ||
var stats = fs.statSync(filepath); | ||
|
||
// don't require non-javascript files (.txt .md etc.) | ||
if (stats.isFile() && !(ext in require.extensions)) { return; } | ||
|
||
var basename = path.basename(filename, ext); | ||
|
||
requires[basename] = require(filepath); | ||
}); | ||
|
||
} else { | ||
basenames.forEach(function(basename) { | ||
var filepath = path.join(dir, basename); | ||
requires[filename] = require(path.resolve(filepath)); | ||
}); | ||
} | ||
} else { | ||
// if basenames argument is passed, explicitly include those files | ||
basenames.forEach(function (basename) { | ||
var filepath = path.resolve(path.join(dir, basename)); | ||
requires[basename] = require(filepath); | ||
}); | ||
} | ||
|
||
return requires; | ||
}; | ||
})(); | ||
return requires; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
asdf 1 2 / @ 123 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters