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