chore: Update the lazy config

This commit is contained in:
BluJ
2023-05-01 13:04:48 -06:00
parent cb89f3a65f
commit a30ed1f0ab
19 changed files with 505 additions and 667 deletions

View File

@@ -2,7 +2,6 @@ import { Config } from "./builder"
import { DeepPartial, Dependencies, Effects, ExpectedExports } from "../types"
import { InputSpec } from "./configTypes"
import { Utils, nullIfEmpty, once, utils } from "../util"
import { TypeFromProps } from "../util/propertiesMatcher"
import { GenericManifest } from "../manifest/ManifestTypes"
import * as D from "./dependencies"
@@ -30,18 +29,18 @@ export type Read<WD, A> = (options: {
*/
export function setupConfig<
WD,
A extends Config<InputSpec>,
Type extends Record<string, any>,
Manifest extends GenericManifest,
>(
spec: A,
write: Save<WD, TypeFromProps<A>, Manifest>,
read: Read<WD, TypeFromProps<A>>,
spec: Config<Type, WD, Type>,
write: Save<WD, Type, Manifest>,
read: Read<WD, Type>,
) {
const validator = once(() => spec.validator())
const validator = spec.validator
return {
setConfig: (async ({ effects, input }) => {
if (!validator().test(input)) {
await effects.console.error(String(validator().errorMessage(input)))
if (!validator.test(input)) {
await effects.console.error(String(validator.errorMessage(input)))
return { error: "Set config type error for config" }
}
await write({
@@ -51,12 +50,18 @@ export function setupConfig<
dependencies: D.dependenciesSet<Manifest>(),
})
}) as ExpectedExports.setConfig,
getConfig: (async ({ effects, config }) => {
getConfig: (async ({ effects }) => {
const myUtils = utils<WD>(effects)
const configValue = nullIfEmpty(
(await read({ effects, utils: myUtils })) || null,
)
return {
spec: spec.build(),
config: nullIfEmpty(
(await read({ effects, utils: utils<WD>(effects) })) || null,
),
spec: await spec.build({
effects,
utils: myUtils,
config: configValue,
}),
config: configValue,
}
}) as ExpectedExports.getConfig,
}