diff --git a/extraPlugin/extra-plugin.js b/extraPlugin/extra-plugin.js index bb7e0e227..349fc4091 100644 --- a/extraPlugin/extra-plugin.js +++ b/extraPlugin/extra-plugin.js @@ -1,9 +1,6 @@ +const {configValidator, routeSplit} = require('../scully/bin'); -const {configValidator} = require('../scully/bin'); - -console.log(__dirname) - -exports.extraRoutesPlugin = (route, options) => { +const extraRoutesPlugin = (route, options) => { const {createPath} = routeSplit(route); if (options.numberOfPages) { return Array.from({length: options.numberOfPages}, (_v, k) => k).map(n => ({ @@ -19,6 +16,31 @@ exports.extraRoutesPlugin = (route, options) => { } return []; }; -/** the validator is mandatory */ -exports.extraRoutesPlugin[configValidator] = async options => []; +extraRoutesPlugin[configValidator] = async options => { + const errors = []; + + if (options.numberOfPages && typeof options.numberOfPages !== 'number') { + errors.push( + `extraroutesPlugin plugin numberOfPages should be a number, not a ${typeof options.numberOfPages}` + ); + } + if (options.numberOfPages && options.data) { + errors.push(`extraroutesPlugin plugin can't have property 'numberOfPages' and 'data' at the same time`); + } + if (options.data) { + if (!Array.isArray(options.data)) { + errors.push(`extraroutesPlugin property 'data' needs to be an array`); + } else { + if (!options.data.every(item => typeof item.title === 'string' && typeof item.data === 'string')) { + errors.push( + `extraroutesPlugin property 'data' needs to have 'title' and 'data' strings on every tuple` + ); + } + } + } + return errors; +}; + +exports.extraRoutesPlugin = extraRoutesPlugin; +/** the validator is mandatory */ diff --git a/scully.config.js b/scully.config.js index b6eb03a1d..dd13232ee 100644 --- a/scully.config.js +++ b/scully.config.js @@ -11,7 +11,7 @@ exports.config = { routes: { '/demo/:id': { type: 'extra', - numberOfPages: 10, + numberOfPages: 5, }, '/home/:topLevel': { type: 'extra', diff --git a/scully/pluginManagement/pluginRepository.ts b/scully/pluginManagement/pluginRepository.ts index 56fdab248..d6e0c1479 100644 --- a/scully/pluginManagement/pluginRepository.ts +++ b/scully/pluginManagement/pluginRepository.ts @@ -10,7 +10,7 @@ export interface Plugin { } export type ErrorString = string; -export type ConfigValidator = (HandledRoute) => ErrorString[] +export type ConfigValidator = (HandledRoute) => ErrorString[]; export interface FilePlugin { alternateExtensions?: string[]; @@ -31,16 +31,24 @@ export const plugins: Plugins = { export type PluginTypes = keyof Plugins; -export const registerPlugin = (type: PluginTypes, name: string, plugin: any) => { - if (plugins[type][name]) { +export const registerPlugin = ( + type: PluginTypes, + name: string, + plugin: any, + {replaceExistingPlugin = false} = {} +) => { + if (replaceExistingPlugin === false && plugins[type][name]) { throw new Error(`Plugin ${name} already exists`); } if (type === 'router' && plugin[configValidator] === undefined) { - logError( - `Route plugin "${yellow( - name - )}" should have an config validator attached to the 'validate' symbol ` - ); + logError(` +--------------- + Route plugin "${yellow(name)}" should have an config validator attached to '${ + plugin.name + }[configValidator]' +--------------- +`); + plugin[configValidator] = async () => []; } plugins[type][name] = plugin; };