Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional filtering functionality #4549

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions lib/core/importer.js
Original file line number Diff line number Diff line change
@@ -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).
Expand All @@ -19,17 +19,22 @@ var path = require('path');
* @api public
*/

function dispatchImporter (rel__dirname) {
function dispatchImporter (rel__dirname, filters) {

function importer (from) {
debug('importing ', from);
var imported = {};
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()) {
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down