Skip to content

Commit

Permalink
Move default value logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 16, 2022
1 parent 7ca0daf commit 2935be6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
27 changes: 2 additions & 25 deletions src/config/default.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
import { cwd as getCwd } from 'process'

import isPlainObj from 'is-plain-obj'

import { getDefaultId } from '../history/merge/id.js'
import { isTtyInput } from '../report/tty.js'
import { cleanObject } from '../utils/clean.js'

// Add default configuration properties
export const addDefaultConfig = function (config, command) {
const defaultConfig = getDefaultConfig(config, command)
return cleanObject({ ...defaultConfig, ...config })
}

const getDefaultConfig = function (config, command) {
return {
cwd: getCwd(),
force: !isTtyInput(),
merge: getDefaultId(),
showMetadata: METADATA_COMMANDS.has(command),
showSystem: getDefaultShowSystem(config),
}
export const addDefaultConfig = function (config) {
return cleanObject(config)
}

const getDefaultShowSystem = function ({ system = {} }) {
return isPlainObj(system) && Object.keys(system).length !== 0
}

const METADATA_COMMANDS = new Set(['show', 'remove'])
2 changes: 1 addition & 1 deletion src/config/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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 configA = addDefaultConfig(config)
const configB = normalizeConfig(configA, command, configInfos)
const configC = await addPlugins(configB, command)
return configC
Expand Down
2 changes: 1 addition & 1 deletion src/config/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const normalizePropConfig = function (
}

const value = config[name]
const args = [name, { configInfos, config }]
const args = [name, { configInfos, config, command }]

const valueA = addDefaultValue(value, defaultValue, args)

Expand Down
25 changes: 24 additions & 1 deletion src/config/properties.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/* eslint-disable max-lines */
import { cwd as getCwd } from 'process'

import isPlainObj from 'is-plain-obj'

import { normalizeLimit } from '../history/compare/normalize.js'
import { normalizeDelta } from '../history/delta/normalize.js'
import { validateMerge } from '../history/merge/id.js'
import { getDefaultId, validateMerge } from '../history/merge/id.js'
import { isOutputPath } from '../report/output.js'
import { isTtyInput } from '../report/tty.js'
import { normalizePrecision } from '../run/precision.js'

import {
Expand All @@ -17,8 +22,11 @@ import {
checkJson,
} from './check.js'
import { normalizeConfigPath, normalizeConfigGlob } from './path.js'
// eslint-disable-next-line import/max-dependencies
import { recurseConfigSelectors } from './select/normalize.js'

const METADATA_COMMANDS = new Set(['show', 'remove'])

export const CONFIG_PROPS = {
colors: {
commands: 'report',
Expand All @@ -28,6 +36,9 @@ export const CONFIG_PROPS = {
},
cwd: {
commands: 'all',
default() {
return getCwd()
},
normalize(value, name, { configInfos }) {
checkDefinedString(value, name)
return normalizeConfigPath(value, name, configInfos)
Expand All @@ -42,6 +53,9 @@ export const CONFIG_PROPS = {
},
force: {
commands: 'remove',
default() {
return !isTtyInput()
},
normalize(value, name) {
checkBoolean(value, name)
},
Expand All @@ -66,6 +80,9 @@ export const CONFIG_PROPS = {
},
merge: {
commands: 'run',
default() {
return getDefaultId()
},
normalize(value, name) {
checkDefinedString(value, name)
validateMerge(value, name)
Expand Down Expand Up @@ -172,6 +189,9 @@ export const CONFIG_PROPS = {
},
showMetadata: {
commands: 'report',
default(name, { command }) {
return METADATA_COMMANDS.has(command)
},
normalize(value, name) {
checkBoolean(value, name)
},
Expand All @@ -187,6 +207,9 @@ export const CONFIG_PROPS = {
},
showSystem: {
commands: 'report',
default(name, { config: { system } }) {
return isPlainObj(system) && Object.keys(system).length !== 0
},
normalize(value, name) {
checkBoolean(value, name)
},
Expand Down

0 comments on commit 2935be6

Please sign in to comment.