Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 30, 2022
1 parent 6dc3e4e commit ecec039
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 66 deletions.
15 changes: 10 additions & 5 deletions src/config/plugin/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,24 @@ import { normalizeConfig } from '../normalize/main.js'
// - Which enables using - and _ in user-defined identifiers
// - Works unescaped with YAML, JSON and JavaScript
export const getPluginConfig = async function ({
id,
config,
context,
configProp,
topProps,
pluginConfigDefinitions,
plugin,
plugin: { id },
topDefinitions,
}) {
const pluginConfig = config[configProp][id]
const pluginConfigA = await normalizePluginConfig({
pluginConfig,
configProp,
id,
context,
topProps,
pluginConfigDefinitions,
plugin,
topDefinitions,
})
const pluginConfigB = mergeTopProps(config, pluginConfigA, topProps)
return pluginConfigB
Expand All @@ -66,17 +69,19 @@ export const getPluginConfig = async function ({
const normalizePluginConfig = async function ({
pluginConfig = {},
configProp,
id,
context,
topProps,
pluginConfigDefinitions = [],
plugin,
plugin: { id },
topDefinitions: pluginTopDefinitions,
}) {
const topDefinitions = getTopDefinitions(topProps)
const prefix = `${configProp}.${id}`
return await normalizeConfig(
pluginConfig,
[...topDefinitions, ...pluginConfigDefinitions],
{ context, prefix },
[...topDefinitions, ...pluginTopDefinitions, ...pluginConfigDefinitions],
{ context: { ...context, plugin }, prefix },
)
}

Expand Down
7 changes: 5 additions & 2 deletions src/config/plugin/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const loadPlugin = async function (
topProps,
isCombinationDimension,
mainDefinitions,
topDefinitions,
},
) {
const moduleId = getModuleId(id, type, isCombinationDimension)
Expand All @@ -38,15 +39,17 @@ const loadPlugin = async function (
mainDefinitions,
topProps,
)
const pluginC = { ...pluginB, id }
const pluginConfig = await getPluginConfig({
id,
config,
context,
configProp,
topProps,
pluginConfigDefinitions,
plugin: pluginC,
topDefinitions,
})
return { ...pluginB, id, config: pluginConfig }
return { ...pluginC, config: pluginConfig }
}

// Builtin modules are lazy loaded for performance reasons
Expand Down
50 changes: 3 additions & 47 deletions src/report/config/main.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,19 @@
import { detectFormat } from '../formats/detect.js'
import { isTtyOutput } from '../tty.js'

import { addProgrammaticReporter } from './programmatic.js'
import { validateOutputGroups } from './validate.js'

// Normalize reporters configuration
export const normalizeReporters = function (config, command) {
const reporters = config.reporters
.map(addOutput)
.filter((reporter) => shouldUseReporter(reporter, command))
.map(addDefaultReporterConfig)
const reporters = config.reporters.filter((reporter) =>
shouldUseReporter(reporter, command),
)
validateOutputGroups(reporters)
const reportersA = addProgrammaticReporter(reporters)
return { ...config, reporters: reportersA }
}

const addOutput = function (reporter) {
const output = getOutput(reporter)
const format = detectFormat(reporter, output)
const tty = getTty(output)
return { ...reporter, config: { ...reporter.config, format, tty, output } }
}

// The reporter's output is decided by (in priority order):
// - `config.reporterConfig.{reporterId}.output`
// (user-defined, reporter-specific)
// - `config.output` (user-defined, any reporters): merged in a previous step
// - `reporter.defaultOutput` (reporter-defined, reporter-specific)
// - "stdout" (system-defined, any reporters)
// `reporter.defaultOutput` is meant for reporters to define the default format
// and filename
const getOutput = function ({
defaultOutput,
config: { output = defaultOutput },
}) {
return output
}

// `reporter.config.tty` is `true` when output is interactive terminal
const getTty = function (output) {
return output === 'stdout' && isTtyOutput()
}

// Reporting in the `remove` command is shown so the user can be clear about
// which result was removed, and provide with confirmation.
// So we only need to print in the terminal, not output|insert files.
const shouldUseReporter = function ({ config: { tty } }, command) {
return command !== 'remove' || tty
}

// Add default values for each reporter's configuration
// Several reporter's configuration properties default to `true` only when the
// output is an interactive terminal.
const addDefaultReporterConfig = function ({
config: { tty, quiet = !tty, showDiff = tty, colors = tty, ...config },
...reporter
}) {
return {
...reporter,
config: { ...config, tty, quiet, showDiff, colors },
}
}
25 changes: 13 additions & 12 deletions src/report/formats/detect.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { UserError } from '../../error/main.js'

import { FORMATS } from './list.js'

// Retrieve the reporter's format, based on its `output`.
Expand All @@ -21,28 +19,31 @@ import { FORMATS } from './list.js'
// `spyd-reporter-{reporter}-{format}` because this:
// - Requires uninstalling/installing to change format
// - Is harder for publisher
export const detectFormat = function (reporter, output) {
export const computeFormat = function ({ config: { output } }) {
const { name: format } = Object.values(FORMATS).find(({ detect }) =>
detect(output),
)
validateFormat({ format, reporter, output })
return format
}

// Validate that a reporter supports the format specified by a given `output`
const validateFormat = function ({
export const validateFormat = function (
format,
reporter,
reporter: { id },
output,
}) {
{
config: { output },
context: {
plugin,
plugin: { id },
},
},
) {
const hasMethod = FORMATS[format].methods.some(
(method) => reporter[method] !== undefined,
(method) => plugin[method] !== undefined,
)

if (!hasMethod) {
throw new UserError(
`The reporter "${id}" does not support "output": "${output}"`,
throw new Error(
`must not use unsupported "output: ${output}" with reporter "${id}".`,
)
}
}
2 changes: 2 additions & 0 deletions src/report/reporters/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { DEFAULT_REPORTER_OUTPUT } from '../contents/output.js'
import { getReportMethods } from '../formats/list.js'

import { BUILTIN_REPORTERS, DEFAULT_REPORTERS } from './main.js'
import { reportersTopDefinitions } from './top_definitions.js'

export const REPORTER_PLUGIN_TYPE = {
type: 'reporter',
Expand Down Expand Up @@ -63,4 +64,5 @@ export const REPORTER_PLUGIN_TYPE = {
validate: validateDefinedString,
},
],
topDefinitions: reportersTopDefinitions,
}
57 changes: 57 additions & 0 deletions src/report/reporters/top_definitions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { computeFormat, validateFormat } from '../formats/detect.js'
import { isTtyOutput } from '../tty.js'

// Reporter-specific definitions of top-level properties
export const reportersTopDefinitions = [
// The reporter's output is decided by (in priority order):
// - `config.reporterConfig.{reporterId}.output`
// (user-defined, reporter-specific)
// - `config.output` (user-defined, any reporters): merged in a previous
// step
// - `reporter.defaultOutput` (reporter-defined, reporter-specific)
// - "stdout" (system-defined, any reporters)
// `reporter.defaultOutput` is meant for reporters to define the default
// format and filename
{
name: 'output',
default({
context: {
plugin: { defaultOutput },
},
}) {
return defaultOutput
},
},
{
name: 'format',
compute: computeFormat,
validate: validateFormat,
},
// `reporter.config.tty` is `true` when output is interactive terminal.
// Several reporter's configuration properties default to `true` only when
// the output is an interactive terminal.
{
name: 'tty',
compute({ config: { output } }) {
return output === 'stdout' && isTtyOutput()
},
},
{
name: 'quiet',
default({ config: { tty } }) {
return !tty
},
},
{
name: 'showDiff',
default({ config: { tty } }) {
return tty
},
},
{
name: 'colors',
default({ config: { tty } }) {
return tty
},
},
]
2 changes: 2 additions & 0 deletions src/runners/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ export const RUNNER_PLUGIN_TYPE = {
validate: validateFunction,
},
],
// Plugin-specific definitions for top-properties
topDefinitions: [],
}

0 comments on commit ecec039

Please sign in to comment.