plugin.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var fs = require('graceful-fs')
  2. var path = require('path')
  3. var helper = require('./helper')
  4. var log = require('./logger').create('plugin')
  5. var IGNORED_PACKAGES = ['karma-cli', 'karma-runner.github.com']
  6. exports.resolve = function (plugins, emitter) {
  7. var modules = []
  8. var requirePlugin = function (name) {
  9. log.debug('Loading plugin %s.', name)
  10. try {
  11. modules.push(require(name))
  12. } catch (e) {
  13. if (e.code === 'MODULE_NOT_FOUND' && e.message.indexOf(name) !== -1) {
  14. log.error('Cannot find plugin "%s".\n Did you forget to install it?\n' +
  15. ' npm install %s --save-dev', name, name)
  16. } else {
  17. log.error('Error during loading "%s" plugin:\n %s', name, e.message)
  18. }
  19. emitter.emit('load_error', 'plug_in', name)
  20. }
  21. }
  22. plugins.forEach(function (plugin) {
  23. if (helper.isString(plugin)) {
  24. if (plugin.indexOf('*') === -1) {
  25. requirePlugin(plugin)
  26. return
  27. }
  28. var pluginDirectory = path.normalize(path.join(__dirname, '/../..'))
  29. var regexp = new RegExp('^' + plugin.replace('*', '.*'))
  30. log.debug('Loading %s from %s', plugin, pluginDirectory)
  31. fs.readdirSync(pluginDirectory).filter(function (pluginName) {
  32. return IGNORED_PACKAGES.indexOf(pluginName) === -1 && regexp.test(pluginName)
  33. }).forEach(function (pluginName) {
  34. requirePlugin(pluginDirectory + '/' + pluginName)
  35. })
  36. return
  37. }
  38. if (helper.isObject(plugin)) {
  39. log.debug('Loading inlined plugin (defining %s).', Object.keys(plugin).join(', '))
  40. modules.push(plugin)
  41. return
  42. }
  43. log.error('Invalid plugin %s', plugin)
  44. emitter.emit('load_error', 'plug_in', plugin)
  45. })
  46. return modules
  47. }