Skip to content

Commit

Permalink
refactor: update lib/plugin.js to ES6 (#3036)
Browse files Browse the repository at this point in the history
  • Loading branch information
lusarz authored and johnjbarton committed Jun 11, 2018
1 parent e8ca4ec commit 558e2f0
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
var fs = require('graceful-fs')
var path = require('path')
'use strict'

var helper = require('./helper')
var log = require('./logger').create('plugin')
const fs = require('graceful-fs')
const path = require('path')
const helper = require('./helper')

var IGNORED_PACKAGES = ['karma-cli', 'karma-runner.github.com']
const log = require('./logger').create('plugin')

exports.resolve = function (plugins, emitter) {
var modules = []
const IGNORED_PACKAGES = ['karma-cli', 'karma-runner.github.com']

var requirePlugin = function (name) {
log.debug('Loading plugin %s.', name)
function resolve (plugins, emitter) {
const modules = []

function requirePlugin (name) {
log.debug(`Loading plugin ${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)
log.error(`Cannot find plugin "${name}".\n Did you forget to install it?\n npm install ${name} --save-dev`)
} else {
log.error('Error during loading "%s" plugin:\n %s', name, e.message)
log.error(`Error during loading "${name}" plugin:\n ${e.message}`)
}
emitter.emit('load_error', 'plug_in', name)
}
Expand All @@ -30,25 +31,23 @@ exports.resolve = function (plugins, emitter) {
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(', '))
const pluginDirectory = path.normalize(path.join(__dirname, '/../..'))
const regexp = new RegExp(`^${plugin.replace('*', '.*')}`)

log.debug(`Loading ${plugin} from ${pluginDirectory}`)
fs.readdirSync(pluginDirectory)
.filter((pluginName) => IGNORED_PACKAGES.indexOf(pluginName) === -1 && regexp.test(pluginName))
.forEach((pluginName) => requirePlugin(pluginDirectory + '/' + pluginName))
} else if (helper.isObject(plugin)) {
log.debug(`Loading inlined plugin (defining ${Object.keys(plugin).join(', ')}).`)
modules.push(plugin)
return
} else {
log.error(`Invalid plugin ${plugin}`)
emitter.emit('load_error', 'plug_in', plugin)
}
log.error('Invalid plugin %s', plugin)
emitter.emit('load_error', 'plug_in', plugin)
})

return modules
}

exports.resolve = resolve

0 comments on commit 558e2f0

Please sign in to comment.