Skip to content

Commit

Permalink
fix(util): filter .js files before requiring them
Browse files Browse the repository at this point in the history
use require-all instead of auto-loader, since autoloader select all files before filtering them,

which causes an error when a non-js file is loaded

Closes #96
  • Loading branch information
thetutlage committed Feb 25, 2017
1 parent 5aebc5b commit a21d6fc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
24 changes: 13 additions & 11 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

const i = require('inflect')
const _ = require('lodash')
const autoLoader = require('auto-loader')
const requireAll = require('require-all')
const prettyHrtime = require('pretty-hrtime')
const util = exports = module.exports = {}
const isolatedLodash = _.runInContext()
Expand Down Expand Up @@ -283,17 +283,19 @@ util.timeDiff = function (start) {
* @public
*/
util.loadJsFiles = function (fromPath, onlyFiles) {
return _(autoLoader.load(fromPath))
.map(function (file, name) {
if (name.endsWith('.js') && !_.size(onlyFiles)) {
return [name.replace('.js', ''), file]
} else if (name.endsWith('.js') && onlyFiles.indexOf(name) > -1) {
return [name.replace('.js', ''), file]
}
return requireAll({
dirname: fromPath,
filter: function (name) {
if (!name.endsWith('.js')) {
return false
}

if (!_.size(onlyFiles) || onlyFiles.indexOf(name) > -1) {
return name.replace('.js', '')
}
},
recursive: true
})
.compact()
.fromPairs()
.value()
}

/**
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
},
"dependencies": {
"adonis-binding-resolver": "^1.0.1",
"auto-loader": "git+https://github.com/thetutlage/node-auto-loader.git",
"cat-log": "^1.0.2",
"chance": "^1.0.6",
"co": "^4.6.0",
Expand All @@ -69,7 +68,8 @@
"lodash": "^4.17.4",
"moment": "^2.17.1",
"node-exceptions": "^1.0.3",
"pretty-hrtime": "^1.0.3"
"pretty-hrtime": "^1.0.3",
"require-all": "^2.2.0"
},
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions test/unit/autoload/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Hello Dude

0 comments on commit a21d6fc

Please sign in to comment.