Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jun 5, 2022
1 parent 4b514b8 commit 66f5b4f
Showing 1 changed file with 146 additions and 186 deletions.
332 changes: 146 additions & 186 deletions src/config/normalize/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,193 +43,153 @@ import { normalizeArray } from './transform.js'
// eslint-disable-next-line import/max-dependencies
import { validateJson } from './validate/json.js'

const configProps = getDummyRules(CONFIG_RULES)

const cwd = {
name: 'cwd',
pick: amongCommands(['dev', 'remove', 'run', 'show']),
default: getCwd,
path: ['exist', 'directory'],
}

const delta = {
name: 'delta',
pick: amongCommands(['remove', 'show']),
default: DEFAULT_MAIN_DELTA,
transform: transformDelta,
}

const force = {
name: 'force',
pick: amongCommands(['remove']),
default() {
return !isTtyInput()
},
schema: { type: 'boolean' },
}

const inputs = {
name: 'inputs',
pick: amongCommands(['dev', 'run']),
default: DEFAULT_INPUTS,
schema: { type: 'object' },
example: EXAMPLE_INPUTS,
}

const inputsAny = {
name: 'inputs.*',
pick: amongCommands(['dev', 'run']),
validate: validateJson,
example: EXAMPLE_INPUT_VALUE,
}

const limit = {
name: 'limit',
pick: amongCommands(['remove', 'run', 'show']),
schema: { type: 'integer' },
transform: transformLimit,
example: EXAMPLE_LIMIT,
}

const id = {
name: 'id',
pick: amongCommands(['run']),
default: getDefaultId,
schema: {
type: 'string',
minLength: 1,
oneOf: [{ format: 'uuid' }, { const: LAST_ID }],
errorMessage: {
minLength: 'must not be an empty string',
_: `must be "${LAST_ID}" or a UUID`,
export const RULES = [
...getDummyRules(CONFIG_RULES),
{
name: 'cwd',
pick: amongCommands(['dev', 'remove', 'run', 'show']),
default: getCwd,
path: ['exist', 'directory'],
},
{
name: 'delta',
pick: amongCommands(['remove', 'show']),
default: DEFAULT_MAIN_DELTA,
transform: transformDelta,
},
{
name: 'force',
pick: amongCommands(['remove']),
default() {
return !isTtyInput()
},
schema: { type: 'boolean' },
},
{
name: 'inputs',
pick: amongCommands(['dev', 'run']),
default: DEFAULT_INPUTS,
schema: { type: 'object' },
example: EXAMPLE_INPUTS,
},
{
name: 'inputs.*',
pick: amongCommands(['dev', 'run']),
validate: validateJson,
example: EXAMPLE_INPUT_VALUE,
},
{
name: 'limit',
pick: amongCommands(['remove', 'run', 'show']),
schema: { type: 'integer' },
transform: transformLimit,
example: EXAMPLE_LIMIT,
},
{
name: 'id',
pick: amongCommands(['run']),
default: getDefaultId,
schema: {
type: 'string',
minLength: 1,
oneOf: [{ format: 'uuid' }, { const: LAST_ID }],
errorMessage: {
minLength: 'must not be an empty string',
_: `must be "${LAST_ID}" or a UUID`,
},
},
},
{
name: 'outliers',
pick: amongCommands(['run']),
default: DEFAULT_OUTLIERS,
schema: { type: 'boolean' },
},
{
name: 'precision',
pick: amongCommands(['run']),
default: DEFAULT_PRECISION,
schema: { type: 'integer', minimum: MIN_PRECISION, maximum: MAX_PRECISION },
transform: transformPrecision,
},
{
name: 'save',
pick: amongCommands(['run']),
default: DEFAULT_SAVE,
schema: { type: 'boolean' },
},
{
name: 'select',
pick: amongCommands(['dev', 'remove', 'run', 'show']),
default: DEFAULT_SELECT,
transform: normalizeArray,
example: EXAMPLE_SELECT,
},
{
name: 'select.*',
required: true,
pick: amongCommands(['dev', 'remove', 'run', 'show']),
schema: { type: 'string' },
example: EXAMPLE_SELECT,
},
{
name: 'since',
pick: amongCommands(['remove', 'run', 'show']),
default: DEFAULT_SINCE_DELTA,
transform: transformDelta,
},
{
name: 'system',
pick: amongCommands(['dev', 'run']),
default: {},
schema: { type: 'object' },
example: EXAMPLE_SYSTEMS,
},
{
name: 'system.*',
pick: amongCommands(['dev', 'run']),
schema: {
type: 'string',
minLength: 1,
errorMessage: { minLength: 'must not be an empty string' },
},
example: EXAMPLE_SYSTEM,
},
{
name: 'titles',
pick: amongCommands(['remove', 'run', 'show']),
default: DEFAULT_TITLES,
schema: { type: 'object' },
example: EXAMPLE_TITLES,
},
{
name: 'titles.*',
pick: amongCommands(['remove', 'run', 'show']),
schema: {
type: 'string',
minLength: 1,
errorMessage: { minLength: 'must not be an empty string' },
},
example: EXAMPLE_TITLE,
},
{
name: 'runner',
pick: amongCommands(['dev', 'run']),
default: DEFAULT_RUNNERS,
schema: {
if: { type: 'array' },
// eslint-disable-next-line unicorn/no-thenable
then: { type: 'array', minItems: 1 },
errorMessage: 'must not be an empty array',
},
},
{
name: 'reporter',
pick: amongCommands(['remove', 'run', 'show']),
default: DEFAULT_REPORTERS,
transform(value, { inputs: { force: forceProp } }) {
return forceProp ? [] : value
},
},
}

const outliers = {
name: 'outliers',
pick: amongCommands(['run']),
default: DEFAULT_OUTLIERS,
schema: { type: 'boolean' },
}

const precision = {
name: 'precision',
pick: amongCommands(['run']),
default: DEFAULT_PRECISION,
schema: { type: 'integer', minimum: MIN_PRECISION, maximum: MAX_PRECISION },
transform: transformPrecision,
}

const save = {
name: 'save',
pick: amongCommands(['run']),
default: DEFAULT_SAVE,
schema: { type: 'boolean' },
}

const select = {
name: 'select',
pick: amongCommands(['dev', 'remove', 'run', 'show']),
default: DEFAULT_SELECT,
transform: normalizeArray,
example: EXAMPLE_SELECT,
}

const selectAny = {
name: 'select.*',
required: true,
pick: amongCommands(['dev', 'remove', 'run', 'show']),
schema: { type: 'string' },
example: EXAMPLE_SELECT,
}

const since = {
name: 'since',
pick: amongCommands(['remove', 'run', 'show']),
default: DEFAULT_SINCE_DELTA,
transform: transformDelta,
}

const system = {
name: 'system',
pick: amongCommands(['dev', 'run']),
default: {},
schema: { type: 'object' },
example: EXAMPLE_SYSTEMS,
}

const systemAny = {
name: 'system.*',
pick: amongCommands(['dev', 'run']),
schema: {
type: 'string',
minLength: 1,
errorMessage: { minLength: 'must not be an empty string' },
},
example: EXAMPLE_SYSTEM,
}

const titles = {
name: 'titles',
pick: amongCommands(['remove', 'run', 'show']),
default: DEFAULT_TITLES,
schema: { type: 'object' },
example: EXAMPLE_TITLES,
}

const titlesAny = {
name: 'titles.*',
pick: amongCommands(['remove', 'run', 'show']),
schema: {
type: 'string',
minLength: 1,
errorMessage: { minLength: 'must not be an empty string' },
},
example: EXAMPLE_TITLE,
}

const runner = {
name: 'runner',
pick: amongCommands(['dev', 'run']),
default: DEFAULT_RUNNERS,
schema: {
if: { type: 'array' },
// eslint-disable-next-line unicorn/no-thenable
then: { type: 'array', minItems: 1 },
errorMessage: 'must not be an empty array',
},
}

const reporter = {
name: 'reporter',
pick: amongCommands(['remove', 'run', 'show']),
default: DEFAULT_REPORTERS,
transform(value, { inputs: { force: forceProp } }) {
return forceProp ? [] : value
},
}

export const RULES = [
...configProps,
cwd,
delta,
force,
inputs,
inputsAny,
limit,
id,
outliers,
precision,
save,
select,
selectAny,
since,
system,
systemAny,
titles,
titlesAny,
runner,
reporter,
].flatMap(normalizeConfigSelectors)
/* eslint-enable max-lines */

0 comments on commit 66f5b4f

Please sign in to comment.