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

76 lines
2.0 KiB
JavaScript
Raw Normal View History

'use strict';
2017-04-27 05:52:46 +02:00
const Path = require('path');
2015-04-08 22:54:59 +02:00
2017-04-27 05:52:46 +02:00
/**
* Requires a module.
* @param {*} path the module's path
* @return {Object}
*/
2015-04-08 22:54:59 +02:00
function try_load(path) {
try {
return require(path);
2015-04-08 22:54:59 +02:00
} catch(err) {
if (err.code === 'MODULE_NOT_FOUND') {
return null;
2015-04-08 22:54:59 +02:00
}
throw err;
2015-04-08 22:54:59 +02:00
}
}
2017-04-27 05:52:46 +02:00
/**
* Load a plugin following the rules
* - First try to load from the internal directory plugins (which will disappear soon or later).
* - A seccond attempt from node_modules, in case to have multiple match as for instance verdaccio-ldap
* and sinopia-ldap. All verdaccio prefix will have preferences.
* @param {*} config a reference of the configuration settings
* @param {*} plugin_configs
* @param {*} params a set of params to initialise the plugin
* @param {*} sanity_check callback that check the shape that should fulfill the plugin
* @return {Array} list of plugins
*/
2015-04-08 22:54:59 +02:00
function load_plugins(config, plugin_configs, params, sanity_check) {
let plugins = Object.keys(plugin_configs || {}).map(function(p) {
let plugin;
2015-04-08 22:54:59 +02:00
// 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
2017-04-22 09:31:12 +02:00
if (!plugin) {
plugin = try_load(`sinopia-${p}`);
}
2015-04-08 22:54:59 +02:00
}
if (plugin === null) {
plugin = try_load(p);
2015-04-08 22:54:59 +02:00
}
// relative to config path
if (plugin === null && p.match(/^\.\.?($|\/)/)) {
plugin = try_load(Path.resolve(Path.dirname(config.self_path), p));
}
if (plugin === null) {
throw Error('"' + p + '" plugin not found\ntry "npm install verdaccio-' + p + '"');
}
if (typeof(plugin) !== 'function')
throw Error('"' + p + '" doesn\'t look like a valid plugin');
plugin = plugin(plugin_configs[p], params);
if (plugin === null || !sanity_check(plugin))
throw Error('"' + p + '" doesn\'t look like a valid plugin');
2017-04-22 09:31:12 +02:00
return plugin;
});
return plugins;
}
exports.load_plugins = load_plugins;