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