diff --git a/lib/core/importer.js b/lib/core/importer.js index 8e482e8db2..3c2b9030e1 100644 --- a/lib/core/importer.js +++ b/lib/core/importer.js @@ -1,7 +1,7 @@ var fs = require('fs'); var debug = require('debug')('keystone:core:importer'); var path = require('path'); - +var minimatch = require('minimatch'); /** * Returns a function that looks in a specified path relative to the current * directory, and returns all .js modules in it (recursively). @@ -19,7 +19,7 @@ var path = require('path'); * @api public */ -function dispatchImporter (rel__dirname) { +function dispatchImporter (rel__dirname, filters) { function importer (from) { debug('importing ', from); @@ -27,9 +27,14 @@ function dispatchImporter (rel__dirname) { var joinPath = function () { return '.' + path.sep + path.join.apply(path, arguments); }; - var fsPath = joinPath(path.relative(process.cwd(), rel__dirname), from); fs.readdirSync(fsPath).forEach(function (name) { + var matchingVar = 'match'; + var excludingVar = 'exclude'; + var isMatch = true; + if (this.filters) { + isMatch = filters[matchingVar] ? true : false; + } var info = fs.statSync(path.join(fsPath, name)); debug('recur'); if (info.isDirectory()) { @@ -39,12 +44,27 @@ function dispatchImporter (rel__dirname) { var ext = path.extname(name); var base = path.basename(name, ext); if (require.extensions[ext]) { + if (this.filters) + { + if (isMatch || (!isMatch && !minimatch(name, this.filters[excludingVar]))) + { + imported[base] = require(path.join(rel__dirname, from, name)); + } + } + else + { + imported[base] = require(path.join(rel__dirname, from, name)); + } + } + else if (this.filters && isMatch && minimatch(name, this.filters[matchingVar])) + { imported[base] = require(path.join(rel__dirname, from, name)); - } else { + } + else { debug('cannot require ', ext); } } - }); + }, { filters: filters }); return imported; } diff --git a/package.json b/package.json index 53cd5124db..2ef4057161 100644 --- a/package.json +++ b/package.json @@ -59,6 +59,7 @@ "marked": "0.3.6", "method-override": "2.3.10", "mime-types": "2.1.15", + "minimatch": "^3.0.4", "moment": "2.18.1", "mongoose": "4.9.2", "morgan": "1.9.0",