Skip to content

Commit

Permalink
feat: simplify loading plugins using patterns like karma-*
Browse files Browse the repository at this point in the history
Now it's possible to load all sybling npm modules through:
plugins = ['karma-*'];

This is also the default configuration.

I hope in most of the cases, people should not have to worry about specifying plugins at all.

This commit also extracts the plugin loading logic into a separate module.
  • Loading branch information
vojtajina committed May 5, 2013
1 parent 856a731 commit 405a5a6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 27 deletions.
9 changes: 1 addition & 8 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,7 @@ var parseConfig = function(configFilePath, cliOptions) {
dir: 'coverage/'
},
loggers: [ constant.CONSOLE_APPENDER ],
plugins: [
'karma-jasmine',
'karma-requirejs',
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-phantomjs-launcher',
'karma-coffee-preprocessor'
]
plugins: [ 'karma-*' ]
};
var dsl = new KarmaDsl(config);
try {
Expand Down
49 changes: 49 additions & 0 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var fs = require('fs');
var path = require('path');

var helper = require('./helper');
var log = require('./logger').create('plugin');


exports.resolve = function(plugins) {
var modules = [];

var requirePlugin = function(name) {
log.debug('Loading plugin %s.', name);
try {
modules.push(require(name));
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND' && e.message.indexOf(name) !== -1) {
log.warn('Cannot find plugin "%s".\n Did you forget to install it ?\n' +
' npm install %s --save-dev', name, name);
} else {
log.warn('Error during loading "%s" plugin:\n %s', name, e.message);
}
}
};

plugins.forEach(function(plugin) {
if (helper.isString(plugin)) {
if (plugin.indexOf('*') !== -1) {
var pluginDirectory = path.normalize(__dirname + '/../..');
var regexp = new RegExp('^' + plugin.replace('*', '.*'));

log.debug('Loading %s from %s', plugin, pluginDirectory);
fs.readdirSync(pluginDirectory).filter(function(pluginName) {
return regexp.test(pluginName);
}).forEach(function(pluginName) {
requirePlugin(pluginDirectory + '/' + pluginName);
});
} else {
requirePlugin(plugin);
}
} else if (helper.isObject(plugin)) {
log.debug('Loading inlined plugin (defining %s).', Object.keys(plugin).join(', '));
modules.push(plugin);
} else {
log.warn('Invalid plugin %s', plugin);
}
});

return modules;
};
22 changes: 3 additions & 19 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var logger = require('./logger');
var browser = require('./browser');
var constant = require('./constants');
var watcher = require('./watcher');
var plugin = require('./plugin');

var ws = require('./web-server');
var preprocessor = require('./preprocessor');
Expand Down Expand Up @@ -247,25 +248,8 @@ exports.start = function(cliOptions, done) {
capturedBrowsers: ['type', browser.Collection]
}];

// register all plugins
config.plugins.forEach(function(plugin) {
if (helper.isString(plugin)) {
try {
modules.push(require(plugin));
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
log.warn('Cannot find plugin "%s".\n Did you forget to install it ?\n' +
' npm install %s --save-dev', plugin, plugin);
} else {
log.warn('Error during loading "%s" plugin:\n %s', plugin, e.message);
}
}
} else if (helper.isObject(plugin)) {
modules.push(plugin);
} else {
log.warn('Invalid plugin %s', plugin);
}
});
// load the plugins
modules = modules.concat(plugin.resolve(config.plugins));

var injector = new di.Injector(modules);

Expand Down

0 comments on commit 405a5a6

Please sign in to comment.