Skip to content

Commit

Permalink
feat(pluginrpository.ts): refactor registerplugin to take validator
Browse files Browse the repository at this point in the history
* feat(pluginrpository.ts): refactor registerplugin to take validator

To make writing plugins easier, the validator is now a parameter to the registerPlugin function.
Also the resiterPlugin functin now checks its types.

* fix(pluginrepsitory .ts): user the correct validator

* fix(pluginrepository): check type of valdidator is funciton
  • Loading branch information
SanderElias authored and Villanuevand committed Jan 20, 2020
1 parent 73cfa2b commit 72d3ae4
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions scully/pluginManagement/pluginRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,25 @@ export const registerPlugin = (
type: PluginTypes,
name: string,
plugin: any,
validator = async () => [],
{replaceExistingPlugin = false} = {}
) => {
if (!['router', 'render', 'fileHandler'].includes(type)) {
throw new Error(
`Type "${yellow(type)}" is not a known plugin type for registering plugin "${yellow(name)}"`
);
}
if (replaceExistingPlugin === false && plugins[type][name]) {
throw new Error(`Plugin ${name} already exists`);
}
if (type === 'router' && plugin[configValidator] === undefined) {
if (type === 'router' && typeof validator !== 'function') {
logError(`
---------------
Route plugin "${yellow(name)}" should have an config validator attached to '${
plugin.name
}[configValidator]'
Route plugin "${yellow(name)}" should have an config validator attached to '${plugin.name}'
---------------
`);
plugin[configValidator] = async () => [];
}
plugin[configValidator] = validator;
plugins[type][name] = plugin;
};

0 comments on commit 72d3ae4

Please sign in to comment.