plugin.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict'
  2. const fs = require('graceful-fs')
  3. const path = require('path')
  4. const helper = require('./helper')
  5. const log = require('./logger').create('plugin')
  6. const IGNORED_PACKAGES = ['karma-cli', 'karma-runner.github.com']
  7. function resolve (plugins, emitter) {
  8. const modules = []
  9. function requirePlugin (name) {
  10. log.debug(`Loading plugin ${name}.`)
  11. try {
  12. modules.push(require(name))
  13. } catch (e) {
  14. if (e.code === 'MODULE_NOT_FOUND' && e.message.includes(name)) {
  15. log.error(`Cannot find plugin "${name}".\n Did you forget to install it?\n npm install ${name} --save-dev`)
  16. } else {
  17. log.error(`Error during loading "${name}" plugin:\n ${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.includes('*')) {
  25. requirePlugin(plugin)
  26. return
  27. }
  28. const pluginDirectory = path.normalize(path.join(__dirname, '/../..'))
  29. const regexp = new RegExp(`^${plugin.replace(/\*/g, '.*').replace(/\//g, '[/\\\\]')}`)
  30. log.debug(`Loading ${plugin} from ${pluginDirectory}`)
  31. fs.readdirSync(pluginDirectory)
  32. .map((e) => {
  33. const modulePath = path.join(pluginDirectory, e)
  34. if (e[0] === '@') {
  35. return fs.readdirSync(modulePath).map((e) => path.join(modulePath, e))
  36. }
  37. return modulePath
  38. })
  39. .reduce((a, x) => a.concat(x), [])
  40. .map((modulePath) => path.relative(pluginDirectory, modulePath))
  41. .filter((moduleName) => !IGNORED_PACKAGES.includes(moduleName) && regexp.test(moduleName))
  42. .forEach((pluginName) => requirePlugin(path.join(pluginDirectory, pluginName)))
  43. } else if (helper.isObject(plugin)) {
  44. log.debug(`Loading inline plugin defining ${Object.keys(plugin).join(', ')}.`)
  45. modules.push(plugin)
  46. } else {
  47. log.error(`Invalid plugin ${plugin}`)
  48. emitter.emit('load_error', 'plug_in', plugin)
  49. }
  50. })
  51. return modules
  52. }
  53. /**
  54. Create a function to handle errors in plugin loading.
  55. @param {Object} injector, the dict of dependency injection objects.
  56. @return function closed over injector, which reports errors.
  57. */
  58. function createInstantiatePlugin (injector) {
  59. const emitter = injector.get('emitter')
  60. // Cache to avoid report errors multiple times per plugin.
  61. const pluginInstances = new Map()
  62. return function instantiatePlugin (kind, name) {
  63. if (pluginInstances.has(name)) {
  64. return pluginInstances.get(name)
  65. }
  66. let p
  67. try {
  68. p = injector.get(`${kind}:${name}`)
  69. if (!p) {
  70. log.error(`Failed to instantiate ${kind} ${name}`)
  71. emitter.emit('load_error', kind, name)
  72. }
  73. } catch (e) {
  74. if (e.message.includes(`No provider for "${kind}:${name}"`)) {
  75. log.error(`Cannot load "${name}", it is not registered!\n Perhaps you are missing some plugin?`)
  76. } else {
  77. log.error(`Cannot load "${name}"!\n ` + e.stack)
  78. }
  79. emitter.emit('load_error', kind, name)
  80. }
  81. pluginInstances.set(name, p, `${kind}:${name}`)
  82. return p
  83. }
  84. }
  85. createInstantiatePlugin.$inject = ['injector']
  86. module.exports = { resolve, createInstantiatePlugin }