Skip to content

Commit

Permalink
Use DAG in config
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 16, 2022
1 parent 4dacc1c commit e06a0c6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 51 deletions.
17 changes: 0 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"p-map": "^5.3.0",
"p-map-series": "^3.0.0",
"p-props": "^5.0.0",
"p-reduce": "^3.0.0",
"path-exists": "^5.0.0",
"path-type": "^5.0.0",
"precise-now": "^0.3.1",
Expand Down
4 changes: 2 additions & 2 deletions src/config/dag/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { mapValues } from '../../utils/map.js'

import { createDag, addDagEdge } from './structure.js'

// Run several async tasks in parallel while still allowing them to use each
// other's return values.
// Run several async or async tasks in parallel while still allowing them to
// use each other's return values.
// This creates a DAG.
// - Unlike traditional DAGs which require each task to declaring its
// dependencies as a static array, tasks declare those in an imperative
Expand Down
59 changes: 30 additions & 29 deletions src/config/normalize.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,43 @@
import omit from 'omit.js'
import pReduce from 'p-reduce'
import { mapValues } from '../utils/map.js'

import { runNormalizer } from './check.js'
import { runDag } from './dag/run.js'
import { CONFIG_PROPS } from './properties.js'

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

const normalizePropConfig = async function (
{ config, name, command, configInfos },
configProp,
) {
const value = await normalizePropValue(
{ config, name, command, configInfos },
configProp,
)

if (value === undefined) {
return name in config ? omit.default(config, [name]) : config
}

return { ...config, [name]: value }
const configProps = await runDag(configPropsFuncs)
const configA = mergeConfigProps(configProps)
return configA
}

const normalizePropValue = async function (
{ config, name, command, configInfos },
{ commands, default: defaultValue, normalize },
{
configProp: { commands, default: defaultValue, normalize },
name,
config,
command,
configInfos,
},
configPromises,
) {
if (!commandHasProp(commands, command)) {
return
}

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

const valueA = await addDefaultValue(value, defaultValue, args)

Expand Down Expand Up @@ -91,3 +84,11 @@ const addDefaultValue = async function (value, defaultValue, args) {

return await defaultValue(...args)
}

const mergeConfigProps = function (configProps) {
return Object.entries(configProps).reduce(setConfigProp, {})
}

const setConfigProp = function (config, [name, value]) {
return value === undefined ? config : { ...config, [name]: value }
}
4 changes: 2 additions & 2 deletions src/config/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ export const CONFIG_PROPS = {
reporter: {
commands: 'report',
default: ['debug'],
normalize(value, name, { config: { force } }) {
if (force) {
async normalize(value, name, { config: { force } }) {
if (await force) {
return []
}

Expand Down

0 comments on commit e06a0c6

Please sign in to comment.