0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00

refactor: improve plugin system to load es6 modules

This commit is contained in:
Juan Picado @jotadeveloper 2017-12-25 11:51:47 +01:00 committed by juanpicado
parent dcbd1e9db8
commit 9a6adc62b6
2 changed files with 12 additions and 3 deletions

View file

@ -55,7 +55,7 @@ class LocalStorage implements IStorage {
if (_.isNil(Storage)) {
return new LocalDatabase(this.config, logger);
} else {
return new Storage(this.config, logger);
return Storage;
}
}

View file

@ -1,5 +1,6 @@
import Path from 'path';
import _ from 'lodash';
import logger from './logger';
/**
@ -18,6 +19,14 @@ function tryLoad(path) {
}
}
function isValid(plugin) {
return (_.isFunction(plugin) === false || _.isFunction(plugin.default) === false);
}
function isES6(plugin) {
return Object.keys(plugin).includes('default');
}
/**
* Load a plugin following the rules
* - First try to load from the internal directory plugins (which will disappear soon or later).
@ -59,12 +68,12 @@ function loadPlugin(config, plugin_configs, params, sanity_check) {
throw Error('"' + p + '" plugin not found\ntry "npm install verdaccio-' + p + '"');
}
if (typeof(plugin) !== 'function') {
if (isValid(plugin) === false) {
logger.logger.error({content: p}, '@{content} doesn\'t look like a valid plugin');
throw Error('"' + p + '" doesn\'t look like a valid plugin');
}
plugin = plugin(plugin_configs[p], params);
plugin = isES6(plugin) ? new plugin.default(plugin_configs[p], params) : plugin(plugin_configs[p], params);
if (plugin === null || !sanity_check(plugin)) {
logger.logger.error({content: p}, '@{content} doesn\'t look like a valid plugin');