Skip to content

Commit

Permalink
Merge command logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 16, 2022
1 parent 1b9ee8c commit babadac
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 74 deletions.
8 changes: 3 additions & 5 deletions src/config/main.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { addDefaultConfig } from './default.js'
import { loadConfig } from './load/main.js'
import { normalizeConfig } from './normalize.js'
import { pickCommandConfig } from './pick.js'
import { addPlugins } from './plugin/add.js'

// Retrieve configuration
export const getConfig = async function (command, configFlags = {}) {
const { config, configInfos } = await loadConfig(configFlags)
const configA = addDefaultConfig(config, command)
const configB = pickCommandConfig(configA, command)
const configC = normalizeConfig(configB, configInfos)
const configD = await addPlugins(configC, command)
return configD
const configB = normalizeConfig(configA, command, configInfos)
const configC = await addPlugins(configB, command)
return configC
}
41 changes: 35 additions & 6 deletions src/config/normalize.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import omit from 'omit.js'

import { runNormalizer } from '../utils/functional.js'

import { CONFIG_PROPS } from './properties.js'

// Normalize configuration shape and do custom validation
export const normalizeConfig = function (config, configInfos) {
export const normalizeConfig = function (config, command, configInfos) {
// eslint-disable-next-line fp/no-mutating-methods
const configInfosA = [...configInfos].reverse()
return Object.entries(CONFIG_PROPS).reduce(
(configA, [name, configProp]) =>
normalizePropConfig(
{ config: configA, name, configInfos: configInfosA },
{ config: configA, name, command, configInfos: configInfosA },
configProp,
),
config,
)
}

const normalizePropConfig = function (
{ config, name, configInfos },
{ normalize },
{ config, name, command, configInfos },
{ commands, normalize },
) {
const value = config[name]

if (value === undefined) {
return config
if (value === undefined || !commandHasProp(commands, command)) {
return omit.default(config, [name])
}

const newValue =
Expand All @@ -32,3 +34,30 @@ const normalizePropConfig = function (
: runNormalizer(normalize, value, name, configInfos)
return { ...config, [name]: newValue }
}

// All config properties can be specified in `spyd.yml` (unlike CLI flags), for
// any commands.
// Therefore, we need to filter them out depending on the current command.
const commandHasProp = function (commands, command) {
return COMMAND_GROUPS[commands].includes(command)
}

// Every group of commands
const COMMAND_GROUPS = {
// All commands
all: ['dev', 'remove', 'run', 'show'],
// Commands that can run combinations
combinations: ['dev', 'run'],
// Commands that use main deltas
delta: ['remove', 'show'],
// Commands that use history
history: ['remove', 'run', 'show'],
// `remove` command
remove: ['remove'],
// Commands that report results
report: ['remove', 'run', 'show'],
// `run` command
run: ['run'],
// Commands that can select combinations
select: ['dev', 'remove', 'run', 'show'],
}
63 changes: 0 additions & 63 deletions src/config/pick.js

This file was deleted.

30 changes: 30 additions & 0 deletions src/config/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,89 +22,118 @@ import { cRecurseConfigSelectors } from './select/normalize.js'

export const CONFIG_PROPS = {
colors: {
commands: 'report',
normalize: checkBoolean,
},
cwd: {
commands: 'all',
normalize: composeNormalizers(checkDefinedString, normalizeConfigPath),
},
delta: {
commands: 'delta',
normalize: normalizeDelta,
},
force: {
commands: 'remove',
normalize: checkBoolean,
},
inputs: {
commands: 'combinations',
normalize: cCheckObjectProps(checkJson),
},
limit: {
commands: 'report',
normalize: cRecurseConfigSelectors(
composeNormalizers(checkInteger, normalizeLimit),
),
},
merge: {
commands: 'run',
normalize: composeNormalizers(checkDefinedString, validateMerge),
},
output: {
commands: 'report',
normalize: composeNormalizers(
checkDefinedString,
condition(normalizeConfigPath, isOutputPath),
),
},
outliers: {
commands: 'run',
normalize: cRecurseConfigSelectors(checkBoolean),
},
precision: {
commands: 'run',
normalize: cRecurseConfigSelectors(
composeNormalizers(checkInteger, normalizePrecision),
),
},
quiet: {
commands: 'run',
normalize: checkBoolean,
},
reporter: {
commands: 'report',
normalize: composeNormalizers(
normalizeOptionalArray,
cCheckArrayItems(checkDefinedString),
),
},
reporterConfig: {
commands: 'report',
},
runner: {
commands: 'combinations',
normalize: composeNormalizers(
normalizeOptionalArray,
checkArrayLength,
cCheckArrayItems(checkDefinedString),
),
},
runnerConfig: {
commands: 'combinations',
},
save: {
commands: 'run',
normalize: checkBoolean,
},
select: {
commands: 'select',
normalize: composeNormalizers(
normalizeOptionalArray,
cCheckArrayItems(checkString),
),
},
showDiff: {
commands: 'report',
normalize: cRecurseConfigSelectors(checkBoolean),
},
showMetadata: {
commands: 'report',
normalize: checkBoolean,
},
showPrecision: {
commands: 'report',
normalize: cRecurseConfigSelectors(checkBoolean),
},
showSystem: {
commands: 'report',
normalize: checkBoolean,
},
showTitles: {
commands: 'report',
normalize: cRecurseConfigSelectors(checkBoolean),
},
since: {
commands: 'history',
normalize: normalizeDelta,
},
system: {
commands: 'combinations',
normalize: cCheckObjectProps(checkDefinedString),
},
tasks: {
commands: 'combinations',
normalize: composeNormalizers(
normalizeOptionalArray,
cCheckArrayItems(
Expand All @@ -113,6 +142,7 @@ export const CONFIG_PROPS = {
),
},
titles: {
commands: 'report',
normalize: cCheckObjectProps(checkDefinedString),
},
}
Expand Down

0 comments on commit babadac

Please sign in to comment.