Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions extraPlugin/extra-plugin.js
Original file line number Diff line number Diff line change
@@ -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 => ({
Expand All @@ -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 */
2 changes: 1 addition & 1 deletion scully.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports.config = {
routes: {
'/demo/:id': {
type: 'extra',
numberOfPages: 10,
numberOfPages: 5,
},
'/home/:topLevel': {
type: 'extra',
Expand Down
24 changes: 16 additions & 8 deletions scully/pluginManagement/pluginRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand All @@ -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;
};