chore: Update to the new dependencyConfig

This commit is contained in:
BluJ
2023-05-10 16:49:10 -06:00
parent cdf42ca3b6
commit 9521865c59
6 changed files with 140 additions and 132 deletions

View File

@@ -0,0 +1,58 @@
import { AutoConfigure, DeepPartial, Effects, ExpectedExports } from "../types"
import { Utils, utils } from "../util/utils"
import { deepEqual } from "../util/deepEqual"
import { deepMerge } from "../util/deepMerge"
export class DependencyConfig<
Store,
Vault,
Input extends Record<string, any>,
RemoteConfig extends Record<string, any>,
> {
constructor(
readonly dependencyConfig: (options: {
effects: Effects
localConfig: Input
remoteConfig: RemoteConfig
utils: Utils<Store, Vault>
}) => Promise<void | DeepPartial<RemoteConfig>>,
) {}
async check(
options: Parameters<AutoConfigure["check"]>[0],
): ReturnType<AutoConfigure["check"]> {
const origConfig = JSON.parse(JSON.stringify(options.localConfig))
const newOptions = {
...options,
utils: utils<Store, Vault>(options.effects),
localConfig: options.localConfig as Input,
remoteConfig: options.remoteConfig as RemoteConfig,
}
if (
!deepEqual(
origConfig,
deepMerge(
{},
options.localConfig,
await this.dependencyConfig(newOptions),
),
)
)
throw new Error(`Check failed`)
}
async autoConfigure(
options: Parameters<AutoConfigure["autoConfigure"]>[0],
): ReturnType<AutoConfigure["autoConfigure"]> {
const newOptions = {
...options,
utils: utils<Store, Vault>(options.effects),
localConfig: options.localConfig as Input,
remoteConfig: options.remoteConfig as any,
}
return deepMerge(
{},
options.remoteConfig,
await this.dependencyConfig(newOptions),
)
}
}

View File

@@ -0,0 +1,9 @@
// prettier-ignore
export type ReadonlyDeep<A> =
A extends Function ? A :
A extends {} ? { readonly [K in keyof A]: ReadonlyDeep<A[K]> } : A;
export type MaybePromise<A> = Promise<A> | A
export type Message = string
import "./DependencyConfig"
import "./setupDependencyConfig"

View File

@@ -0,0 +1,22 @@
import { Config } from "../config/builder/config"
import { SDKManifest } from "../manifest/ManifestTypes"
import { DependencyConfig } from "./DependencyConfig"
export function setupDependencyConfig<
Store,
Vault,
Input extends Record<string, any>,
Manifest extends SDKManifest,
>(
_config: Config<Input, Store, Vault>,
autoConfigs: {
[key in keyof Manifest["dependencies"] & string]: DependencyConfig<
Store,
Vault,
Input,
any
>
},
) {
return autoConfigs
}