1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-13 03:35:52 +01:00
verdaccio/lib/plugin-loader.js

58 lines
1.3 KiB
JavaScript
Raw Normal View History

2015-04-08 22:54:59 +02:00
var Path = require('path')
function try_load(path) {
try {
return require(path)
} catch(err) {
if (err.code === 'MODULE_NOT_FOUND') {
return null
}
throw err
}
}
2015-04-08 22:54:59 +02:00
function load_plugins(config, plugin_configs, params, sanity_check) {
var plugins = Object.keys(plugin_configs || {}).map(function(p) {
2015-04-08 22:54:59 +02:00
var plugin
// try local plugins first
plugin = try_load(Path.resolve(__dirname + '/plugins', p))
2015-04-08 22:54:59 +02:00
// npm package
if (plugin === null && p.match(/^[^\.\/]/)) {
plugin = try_load(`verdaccio-${p}`)
2016-11-09 19:12:15 +01:00
// compatibility for old sinopia plugins
if(!plugin) {
plugin = try_load(`sinopia-${p}`)
}
2015-04-08 22:54:59 +02:00
}
if (plugin === null) {
2015-04-08 22:54:59 +02:00
plugin = try_load(p)
}
// relative to config path
if (plugin === null && p.match(/^\.\.?($|\/)/)) {
2015-04-08 22:54:59 +02:00
plugin = try_load(Path.resolve(Path.dirname(config.self_path), p))
}
if (plugin === null) {
2016-11-07 18:15:38 +01:00
throw Error('"' + p + '" plugin not found\ntry "npm install verdaccio-' + p + '"')
}
if (typeof(plugin) !== 'function')
2015-04-08 22:54:59 +02:00
throw Error('"' + p + '" doesn\'t look like a valid plugin')
plugin = plugin(plugin_configs[p], params)
if (plugin === null || !sanity_check(plugin))
2015-04-08 22:54:59 +02:00
throw Error('"' + p + '" doesn\'t look like a valid plugin')
return plugin
})
return plugins
}
exports.load_plugins = load_plugins;