From 0be8d9d5bce1357cff704b2ad2dc78d8d7c5c451 Mon Sep 17 00:00:00 2001 From: BluJ Date: Mon, 24 Apr 2023 13:24:57 -0600 Subject: [PATCH] chore: Update the types for autoconfig --- lib/autoconfig/AutoConfig.ts | 27 +++++++++++++++------- lib/autoconfig/setupAutoConfig.ts | 9 +++++--- lib/config/index.ts | 2 +- lib/config/setupConfig.ts | 37 ++++++++++++++++--------------- lib/types.ts | 3 ++- lib/util/propertiesMatcher.ts | 2 ++ 6 files changed, 49 insertions(+), 31 deletions(-) diff --git a/lib/autoconfig/AutoConfig.ts b/lib/autoconfig/AutoConfig.ts index 115a62a..70eff94 100644 --- a/lib/autoconfig/AutoConfig.ts +++ b/lib/autoconfig/AutoConfig.ts @@ -1,30 +1,36 @@ import { AutoConfigure, Effects, ExpectedExports } from "../types"; -import { deepEqual, deepMerge } from "../util"; +import { Utils, deepEqual, deepMerge, utils } from "../util"; -export type AutoConfigFrom = { +export type AutoConfigFrom = { [key: string]: (options: { effects: Effects; - localConfig: unknown; + localConfig: Input; remoteConfig: unknown; + utils: Utils; }) => Promise>; }; -export class AutoConfig { +export class AutoConfig { constructor( - readonly configs: AutoConfigFrom, - readonly path: keyof AutoConfigFrom, + readonly configs: AutoConfigFrom, + readonly path: keyof AutoConfigFrom, ) {} async check( options: Parameters[0], ): ReturnType { const origConfig = JSON.parse(JSON.stringify(options.localConfig)); + const newOptions = { + ...options, + utils: utils(options.effects), + localConfig: options.localConfig as Input, + }; if ( !deepEqual( origConfig, deepMerge( {}, options.localConfig, - await this.configs[this.path](options), + await this.configs[this.path](newOptions), ), ) ) @@ -33,10 +39,15 @@ export class AutoConfig { async autoConfigure( options: Parameters[0], ): ReturnType { + const newOptions = { + ...options, + utils: utils(options.effects), + localConfig: options.localConfig as Input, + }; return deepMerge( {}, options.localConfig, - await this.configs[this.path](options), + await this.configs[this.path](newOptions), ); } } diff --git a/lib/autoconfig/setupAutoConfig.ts b/lib/autoconfig/setupAutoConfig.ts index 55a459a..9b0e1c4 100644 --- a/lib/autoconfig/setupAutoConfig.ts +++ b/lib/autoconfig/setupAutoConfig.ts @@ -1,9 +1,12 @@ import { AutoConfig, AutoConfigFrom } from "./AutoConfig"; -export function setupAutoConfig(configs: C) { - const answer = { ...configs } as unknown as { [k in keyof C]: AutoConfig }; +export function setupAutoConfig(configs: AutoConfigFrom) { + type C = typeof configs; + const answer = { ...configs } as unknown as { + [k in keyof C]: AutoConfig; + }; for (const key in configs) { - answer[key] = new AutoConfig(configs, key); + answer[key] = new AutoConfig(configs, key); } return answer; } diff --git a/lib/config/index.ts b/lib/config/index.ts index 2d7184e..f52496b 100644 --- a/lib/config/index.ts +++ b/lib/config/index.ts @@ -1,5 +1,5 @@ export * as configBuilder from "./builder"; -export { setupConfig as setupConfigExports } from "./setupConfig"; +export { setupConfig } from "./setupConfig"; export { specToBuilder, specToBuilderFile } from "./specToBuilder"; export * as dependencies from "./dependencies"; diff --git a/lib/config/setupConfig.ts b/lib/config/setupConfig.ts index 74c466e..e4075d2 100644 --- a/lib/config/setupConfig.ts +++ b/lib/config/setupConfig.ts @@ -1,20 +1,23 @@ import { Config } from "./builder"; import { DeepPartial, Dependencies, Effects, ExpectedExports } from "../types"; import { InputSpec } from "./configTypes"; -import { nullIfEmpty } from "../util"; +import { Utils, nullIfEmpty, utils } from "../util"; import { TypeFromProps } from "../util/propertiesMatcher"; -export type Write = (options: { +declare const dependencyProof: unique symbol; +export type DependenciesReceipt = void & { + [dependencyProof]: never; +}; + +export type Save = (options: { effects: Effects; - input: TypeFromProps; -}) => Promise; -export type Read = (options: { + input: A; + utils: Utils; +}) => Promise; +export type Read = (options: { effects: Effects; -}) => Promise>>; -export type DependenciesFn = (options: { - effects: Effects; - input: TypeFromProps; -}) => Promise; + utils: Utils; +}) => Promise>; /** * We want to setup a config export with a get and set, this * is going to be the default helper to setup config, because it will help @@ -22,11 +25,10 @@ export type DependenciesFn = (options: { * @param options * @returns */ -export function setupConfig( - spec: Config, - write: Write, - read: Read, - dependencies: DependenciesFn, +export function setupConfig>( + spec: A, + write: Save>, + read: Read>, ) { const validator = spec.validator(); return { @@ -38,14 +40,13 @@ export function setupConfig( await write({ input: JSON.parse(JSON.stringify(input)), effects, + utils: utils(effects), }); - const dependenciesToSet = (await dependencies({ effects, input })) || []; - await effects.setDependencies(dependenciesToSet); }) as ExpectedExports.setConfig, getConfig: (async ({ effects, config }) => { return { spec: spec.build(), - config: nullIfEmpty(await read({ effects })), + config: nullIfEmpty(await read({ effects, utils: utils(effects) })), }; }) as ExpectedExports.getConfig, }; diff --git a/lib/types.ts b/lib/types.ts index 34fbe7e..993efb6 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -1,5 +1,6 @@ export * as configTypes from "./config/configTypes"; import { InputSpec } from "./config/configTypes"; +import { DependenciesReceipt } from "./config/setupConfig"; import { ActionReceipt } from "./init"; export type ExportedAction = (options: { @@ -382,7 +383,7 @@ export type Effects = { }): Promise; /** Set the dependencies of what the service needs, usually ran during the set config as a best practice */ - setDependencies(dependencies: Dependencies): Promise; + setDependencies(dependencies: Dependencies): Promise; /** Exists could be useful during the runtime to know if some service exists, option dep */ exists(packageId: PackageId): Promise; /** Exists could be useful during the runtime to know if some service is running, option dep */ diff --git a/lib/util/propertiesMatcher.ts b/lib/util/propertiesMatcher.ts index 3d8314d..17ebc4c 100644 --- a/lib/util/propertiesMatcher.ts +++ b/lib/util/propertiesMatcher.ts @@ -6,6 +6,7 @@ import { ValueSpec as ValueSpecAny, InputSpec, } from "../config/configTypes"; +import { Config } from "../config/builder/config"; const { string, @@ -110,6 +111,7 @@ export type GuardAll = GuardNumber & GuardDatetime; // prettier-ignore export type TypeFromProps = + A extends Config ? TypeFromProps : A extends Record ? { [K in keyof A & string]: _> } : unknown;