From cdf42ca3b66bcfa5a44fd3850ee2df8363df88ba Mon Sep 17 00:00:00 2001 From: BluJ Date: Wed, 10 May 2023 16:30:42 -0600 Subject: [PATCH] feat: Making the auto config --- lib/StartSdk.ts | 52 +++++++++++++++++++++---------- lib/autoconfig/AutoConfig.ts | 42 ++++++++----------------- lib/autoconfig/setupAutoConfig.ts | 24 +++++--------- lib/test/output.sdk.ts | 43 ++++++++++++++++++++++++- lib/test/setupAutoConfig.test.ts | 27 ++++++++++++++++ lib/util/utils.ts | 5 --- package-lock.json | 2 +- 7 files changed, 126 insertions(+), 69 deletions(-) create mode 100644 lib/test/setupAutoConfig.test.ts diff --git a/lib/StartSdk.ts b/lib/StartSdk.ts index 40cbb70..388d93b 100644 --- a/lib/StartSdk.ts +++ b/lib/StartSdk.ts @@ -18,9 +18,10 @@ import { ActionResult, Metadata, BackupOptions, + DeepPartial, } from "./types" import { Utils } from "./util/utils" -import { AutoConfig, AutoConfigFrom } from "./autoconfig/AutoConfig" +import { AutoConfig } from "./autoconfig/AutoConfig" import { BackupSet, Backups } from "./backup/Backups" import { smtpConfig } from "./config/configConstants" import { Daemons } from "./mainFn/Daemons" @@ -75,10 +76,29 @@ export class StartSdk { isReady: AnyNeverCond<[Manifest, Store, Vault], "Build not ready", true>, ) { return { - AutoConfig: >( - configs: AutoConfigFrom, - path: keyof AutoConfigFrom, - ) => new AutoConfig(configs, path), + AutoConfig: { + of< + LocalConfig extends Record, + RemoteConfig extends Record, + >({ + localConfig, + remoteConfig, + autoconfig, + }: { + localConfig: Config + remoteConfig: Config + autoconfig: (options: { + effects: Effects + localConfig: LocalConfig + remoteConfig: RemoteConfig + utils: Utils + }) => Promise> + }) { + return new AutoConfig( + autoconfig, + ) + }, + }, Backups: { volumes: (...volumeNames: Array) => Backups.volumes(...volumeNames), @@ -209,19 +229,17 @@ export class StartSdk { }, setupActions: (...createdActions: CreatedAction[]) => setupActions(...createdActions), - setupAutoConfig: < - Input extends Record, - NestedConfigs extends { - [key in keyof Manifest["dependencies"]]: unknown - }, - >( + setupAutoConfig: >( config: Config, - autoConfigs: AutoConfigFrom, - ) => - setupAutoConfig( - config, - autoConfigs, - ), + autoConfigs: { + [K in keyof Manifest["dependencies"]]: AutoConfig< + Store, + Vault, + Input, + any + > + }, + ) => setupAutoConfig(config, autoConfigs), setupBackups: (...args: SetupBackupsParams) => setupBackups(...args), setupConfig: < diff --git a/lib/autoconfig/AutoConfig.ts b/lib/autoconfig/AutoConfig.ts index 484f3bb..ba4c25a 100644 --- a/lib/autoconfig/AutoConfig.ts +++ b/lib/autoconfig/AutoConfig.ts @@ -4,31 +4,19 @@ import { deepEqual } from "../util/deepEqual" import { deepMerge } from "../util/deepMerge" import { Config } from "../config/builder/config" -export type AutoConfigFrom< - Store, - Vault, - Input, - NestedConfigs extends Record, -> = { - [key in keyof NestedConfigs & string]: { - serviceConfig: Config - autoConfig: (options: { - effects: Effects - localConfig: Input - remoteConfig: NestedConfigs[key] - utils: Utils - }) => Promise> - } -} export class AutoConfig< Store, Vault, - Input, - NestedConfigs extends Record, + Input extends Record, + RemoteConfig extends Record, > { constructor( - readonly configs: AutoConfigFrom, - readonly path: keyof AutoConfigFrom, + readonly autoconfig: (options: { + effects: Effects + localConfig: Input + remoteConfig: RemoteConfig + utils: Utils + }) => Promise>, ) {} async check( @@ -39,19 +27,15 @@ export class AutoConfig< ...options, utils: utils(options.effects), localConfig: options.localConfig as Input, - remoteConfig: options.remoteConfig as any, + remoteConfig: options.remoteConfig as RemoteConfig, } if ( !deepEqual( origConfig, - deepMerge( - {}, - options.localConfig, - await this.configs[this.path].autoConfig(newOptions), - ), + deepMerge({}, options.localConfig, await this.autoconfig(newOptions)), ) ) - throw new Error(`Check failed for ${this.path}`) + throw new Error(`Check failed`) } async autoConfigure( options: Parameters[0], @@ -64,8 +48,8 @@ export class AutoConfig< } return deepMerge( {}, - options.localConfig, - await this.configs[this.path].autoConfig(newOptions), + options.remoteConfig, + await this.autoconfig(newOptions), ) } } diff --git a/lib/autoconfig/setupAutoConfig.ts b/lib/autoconfig/setupAutoConfig.ts index 6055929..00ed044 100644 --- a/lib/autoconfig/setupAutoConfig.ts +++ b/lib/autoconfig/setupAutoConfig.ts @@ -1,30 +1,22 @@ import { Config } from "../config/builder/config" import { SDKManifest } from "../manifest/ManifestTypes" -import { AutoConfig, AutoConfigFrom } from "./AutoConfig" +import { AutoConfig } from "./AutoConfig" export function setupAutoConfig< Store, Vault, Input extends Record, Manifest extends SDKManifest, - NestedConfigs extends { - [key in keyof Manifest["dependencies"]]: unknown - }, >( _config: Config, - autoConfigs: AutoConfigFrom, -) { - type C = typeof autoConfigs - const answer = { ...autoConfigs } as unknown as { - [k in keyof C]: AutoConfig - } - for (const key in autoConfigs) { - answer[key as keyof typeof autoConfigs] = new AutoConfig< + autoConfigs: { + [key in keyof Manifest["dependencies"] & string]: AutoConfig< Store, Vault, Input, - NestedConfigs - >(autoConfigs, key as keyof typeof autoConfigs) - } - return answer + any + > + }, +) { + return autoConfigs } diff --git a/lib/test/output.sdk.ts b/lib/test/output.sdk.ts index 453b896..4802317 100644 --- a/lib/test/output.sdk.ts +++ b/lib/test/output.sdk.ts @@ -1,8 +1,49 @@ import { StartSdk } from "../StartSdk" +import { setupManifest } from "../manifest/setupManifest" export type Manifest = any export const sdk = StartSdk.of() - .withManifest({} as any) + .withManifest( + setupManifest({ + id: "testOutput", + title: "", + version: "1.0", + releaseNotes: "", + license: "", + replaces: [], + wrapperRepo: "", + upstreamRepo: "", + supportSite: "", + marketingSite: "", + donationUrl: null, + description: { + short: "", + long: "", + }, + assets: { + icon: "", + instructions: "", + license: "", + }, + containers: {}, + volumes: {}, + alerts: { + install: null, + update: null, + uninstall: null, + restore: null, + start: null, + stop: null, + }, + dependencies: { + remoteTest: { + description: "", + requirement: { how: "", type: "opt-in" }, + version: "1.0", + }, + }, + }), + ) .withStore<{ storeRoot: { storeLeaf: "value" } }>() .withVault<{ vaultRoot: "value" }>() .build(true) diff --git a/lib/test/setupAutoConfig.test.ts b/lib/test/setupAutoConfig.test.ts new file mode 100644 index 0000000..8af417d --- /dev/null +++ b/lib/test/setupAutoConfig.test.ts @@ -0,0 +1,27 @@ +import { sdk } from "./output.sdk" + +describe("setupAutoConfig", () => { + test("test", () => { + const testConfig = sdk.Config.of({ + test: sdk.Value.text({ + name: "testValue", + required: false, + }), + }) + + const testConfig2 = sdk.Config.of({ + test2: sdk.Value.text({ + name: "testValue2", + required: false, + }), + }) + const remoteTest = sdk.AutoConfig.of({ + localConfig: testConfig, + remoteConfig: testConfig2, + autoconfig: async ({}) => {}, + }) + sdk.setupAutoConfig(testConfig, { + remoteTest, + }) + }) +}) diff --git a/lib/util/utils.ts b/lib/util/utils.ts index 95cd1f0..696a80c 100644 --- a/lib/util/utils.ts +++ b/lib/util/utils.ts @@ -27,11 +27,6 @@ import { Path, } from "../dependency/setupDependencyMounts" -// prettier-ignore -type skipFirstParam = - A extends [any, ...infer B] ? B : - A extends [] ? [] : - never export type Utils = { createOrUpdateVault: (opts: { key: string diff --git a/package-lock.json b/package-lock.json index 3575f69..2788e93 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,7 +20,7 @@ "ts-node": "^10.9.1", "tsc-multi": "^0.6.1", "tsconfig-paths": "^3.14.2", - "typescript": "^5.0.0", + "typescript": "^5.0.4", "vitest": "^0.29.2" } },