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