Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Feb 6, 2022
1 parent 9a79b10 commit 2078ce8
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
38 changes: 38 additions & 0 deletions src/config/plugin/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Add default values of plugin types
export const addPluginTypeDefault = function ({
name,
multiple = false,
shape = [],
builtins = {},
selectProp: {
name: selectPropName = name,
default: selectPropDefault = [],
...selectProp
} = {},
configProp: {
name: configPropName = `${name}Config`,
default: configPropDefault = {},
...configProp
} = {},
sharedProps = [],
...pluginType
}) {
return {
...pluginType,
name,
multiple,
shape,
builtins,
selectProp: {
...selectProp,
name: selectPropName,
default: selectPropDefault,
},
configProp: {
...configProp,
name: configPropName,
default: configPropDefault,
},
sharedProps,
}
}
9 changes: 5 additions & 4 deletions src/config/plugin/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ import { normalizeOptionalArray } from '../normalize/transform.js'
import { validateObject, validateJson } from '../normalize/validate/complex.js'
import { validateDefinedString } from '../normalize/validate/simple.js'

import { addPluginTypeDefault } from './default.js'
import { PLUGIN_TYPES_ARRAY } from './types.js'

// Retrieve the definition for plugins, both the selection property
// (like `reporter`) and the configuration one (like `reporterConfig`).
// This enforces consistency across plugins for selection and configuration.
export const getPluginsDefinitions = function () {
return PLUGIN_TYPES_ARRAY.flatMap(getPluginDefinitions)
return PLUGIN_TYPES_ARRAY.map(addPluginTypeDefault).flatMap(
getPluginDefinitions,
)
}

const getPluginDefinitions = function ({
configProp,
selectProp: { default: defaultValue = [], ...selectProp },
selectProp,
sharedProps,
}) {
return [
...getDummyDefinitions(sharedProps),
{
default: defaultValue,
transform: normalizeOptionalArray,
...selectProp,
},
Expand All @@ -29,7 +31,6 @@ const getPluginDefinitions = function ({
validate: validateDefinedString,
},
{
default: {},
validate: validateObject,
...configProp,
},
Expand Down
5 changes: 4 additions & 1 deletion src/config/plugin/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { list } from '../normalize/lib/prop_path/get.js'
import { isParent } from '../normalize/lib/prop_path/parse.js'
import { set, remove } from '../normalize/lib/prop_path/set.js'

import { addPluginTypeDefault } from './default.js'
import { PLUGIN_TYPES_ARRAY } from './types.js'

// Retrieve all plugin types, normalized
export const getPluginTypes = function () {
return PLUGIN_TYPES_ARRAY.map(addSharedConfigPropNames)
return PLUGIN_TYPES_ARRAY.map(addPluginTypeDefault).map(
addSharedConfigPropNames,
)
}

// Retrieve all unique `name` of shared config properties, excluding their
Expand Down
3 changes: 1 addition & 2 deletions src/report/reporters/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { sharedProps } from './shared_props.js'
const pick = amongCommands(['remove', 'run', 'show'])

export const REPORTER_PLUGIN_TYPE = {
name: 'reporter',
modulePrefix: 'spyd-reporter-',
multiple: true,
shape: [
Expand Down Expand Up @@ -47,15 +48,13 @@ export const REPORTER_PLUGIN_TYPE = {
],
builtins: BUILTIN_REPORTERS,
selectProp: {
name: 'reporter',
pick,
default: DEFAULT_REPORTERS,
transform(value, { config }) {
return config.force ? [] : normalizeOptionalArray(value)
},
},
configProp: {
name: 'reporterConfig',
pick,
},
sharedProps,
Expand Down
14 changes: 11 additions & 3 deletions src/runners/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ import { sharedProps } from './shared_props.js'
const pick = amongCommands(['dev', 'run'])

export const RUNNER_PLUGIN_TYPE = {
// Name used:
// - As a default value for the selection property
// - As a default value for the configuration property appended with `Config`
// - In error messages
name: 'runner',
// Prefix of the npm package
modulePrefix: 'spyd-runner-',
// When `true`, the plugin can be selected twice but with different configs
// Default: `false`
multiple: false,
// Configuration definition for the plugin itself
// Default: empty array
shape: [
{
name: 'launch',
Expand All @@ -23,20 +30,21 @@ export const RUNNER_PLUGIN_TYPE = {
},
],
// Builtin plugins
// Default: empty object
builtins: BUILTIN_RUNNERS,
// Configuration property selecting the plugin
// Default: empty object
selectProp: {
// This `name` is also shown in error messages
name: 'runner',
pick,
default: DEFAULT_RUNNERS,
validate: validateEmptyArray,
},
// Configuration property configuring the plugin
// Default: empty object
configProp: {
name: 'runnerConfig',
pick,
},
// Plugin properties shared by all plugins
// Default: empty array
sharedProps,
}

0 comments on commit 2078ce8

Please sign in to comment.