From 405a5a62d2ecc47a46b2ff069bfeb624f0b06982 Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Sun, 5 May 2013 13:11:53 -0700 Subject: [PATCH] feat: simplify loading plugins using patterns like `karma-*` 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. --- lib/config.js | 9 +-------- lib/plugin.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/server.js | 22 +++------------------- 3 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 lib/plugin.js diff --git a/lib/config.js b/lib/config.js index f73937b52..2d34ac83d 100644 --- a/lib/config.js +++ b/lib/config.js @@ -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 { diff --git a/lib/plugin.js b/lib/plugin.js new file mode 100644 index 000000000..d9131014f --- /dev/null +++ b/lib/plugin.js @@ -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; +}; diff --git a/lib/server.js b/lib/server.js index d3dba8c7e..fcf3e9a56 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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'); @@ -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);