-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: simplify loading plugins using patterns like
karma-*
Now it's possible to load all sybling npm modules through: plugins = ['karma-*']; This is also the default configuration. I hope in most of the cases, people should not have to worry about specifying plugins at all. This commit also extracts the plugin loading logic into a separate module.
- Loading branch information
Showing
3 changed files
with
53 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
|
||
var helper = require('./helper'); | ||
var log = require('./logger').create('plugin'); | ||
|
||
|
||
exports.resolve = function(plugins) { | ||
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.warn('Cannot find plugin "%s".\n Did you forget to install it ?\n' + | ||
' npm install %s --save-dev', name, name); | ||
} else { | ||
log.warn('Error during loading "%s" plugin:\n %s', name, e.message); | ||
} | ||
} | ||
}; | ||
|
||
plugins.forEach(function(plugin) { | ||
if (helper.isString(plugin)) { | ||
if (plugin.indexOf('*') !== -1) { | ||
var pluginDirectory = path.normalize(__dirname + '/../..'); | ||
var regexp = new RegExp('^' + plugin.replace('*', '.*')); | ||
|
||
log.debug('Loading %s from %s', plugin, pluginDirectory); | ||
fs.readdirSync(pluginDirectory).filter(function(pluginName) { | ||
return regexp.test(pluginName); | ||
}).forEach(function(pluginName) { | ||
requirePlugin(pluginDirectory + '/' + pluginName); | ||
}); | ||
} else { | ||
requirePlugin(plugin); | ||
} | ||
} else if (helper.isObject(plugin)) { | ||
log.debug('Loading inlined plugin (defining %s).', Object.keys(plugin).join(', ')); | ||
modules.push(plugin); | ||
} else { | ||
log.warn('Invalid plugin %s', plugin); | ||
} | ||
}); | ||
|
||
return modules; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters