mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-26 02:11:56 +00:00
feat: Making the auto config
This commit is contained in:
@@ -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<Manifest extends SDKManifest, Store, Vault> {
|
||||
isReady: AnyNeverCond<[Manifest, Store, Vault], "Build not ready", true>,
|
||||
) {
|
||||
return {
|
||||
AutoConfig: <Input, NestedConfigs extends Record<string, any>>(
|
||||
configs: AutoConfigFrom<Store, Vault, Input, NestedConfigs>,
|
||||
path: keyof AutoConfigFrom<Store, Vault, Input, NestedConfigs>,
|
||||
) => new AutoConfig<Store, Vault, Input, NestedConfigs>(configs, path),
|
||||
AutoConfig: {
|
||||
of<
|
||||
LocalConfig extends Record<string, any>,
|
||||
RemoteConfig extends Record<string, any>,
|
||||
>({
|
||||
localConfig,
|
||||
remoteConfig,
|
||||
autoconfig,
|
||||
}: {
|
||||
localConfig: Config<LocalConfig, Store, Vault>
|
||||
remoteConfig: Config<RemoteConfig, any, any>
|
||||
autoconfig: (options: {
|
||||
effects: Effects
|
||||
localConfig: LocalConfig
|
||||
remoteConfig: RemoteConfig
|
||||
utils: Utils<Store, Vault>
|
||||
}) => Promise<void | DeepPartial<RemoteConfig>>
|
||||
}) {
|
||||
return new AutoConfig<Store, Vault, LocalConfig, RemoteConfig>(
|
||||
autoconfig,
|
||||
)
|
||||
},
|
||||
},
|
||||
Backups: {
|
||||
volumes: (...volumeNames: Array<keyof Manifest["volumes"] & string>) =>
|
||||
Backups.volumes<Manifest>(...volumeNames),
|
||||
@@ -209,19 +229,17 @@ export class StartSdk<Manifest extends SDKManifest, Store, Vault> {
|
||||
},
|
||||
setupActions: (...createdActions: CreatedAction<any, any, any>[]) =>
|
||||
setupActions<Store, Vault>(...createdActions),
|
||||
setupAutoConfig: <
|
||||
Input extends Record<string, any>,
|
||||
NestedConfigs extends {
|
||||
[key in keyof Manifest["dependencies"]]: unknown
|
||||
},
|
||||
>(
|
||||
setupAutoConfig: <Input extends Record<string, any>>(
|
||||
config: Config<Input, Store, Vault>,
|
||||
autoConfigs: AutoConfigFrom<Store, Vault, Input, NestedConfigs>,
|
||||
) =>
|
||||
setupAutoConfig<Store, Vault, Input, Manifest, NestedConfigs>(
|
||||
config,
|
||||
autoConfigs,
|
||||
),
|
||||
autoConfigs: {
|
||||
[K in keyof Manifest["dependencies"]]: AutoConfig<
|
||||
Store,
|
||||
Vault,
|
||||
Input,
|
||||
any
|
||||
>
|
||||
},
|
||||
) => setupAutoConfig<Store, Vault, Input, Manifest>(config, autoConfigs),
|
||||
setupBackups: (...args: SetupBackupsParams<Manifest>) =>
|
||||
setupBackups<Manifest>(...args),
|
||||
setupConfig: <
|
||||
|
||||
@@ -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<string, any>,
|
||||
> = {
|
||||
[key in keyof NestedConfigs & string]: {
|
||||
serviceConfig: Config<NestedConfigs[key], Store, Vault>
|
||||
autoConfig: (options: {
|
||||
effects: Effects
|
||||
localConfig: Input
|
||||
remoteConfig: NestedConfigs[key]
|
||||
utils: Utils<Store, Vault>
|
||||
}) => Promise<void | DeepPartial<NestedConfigs[key]>>
|
||||
}
|
||||
}
|
||||
export class AutoConfig<
|
||||
Store,
|
||||
Vault,
|
||||
Input,
|
||||
NestedConfigs extends Record<string, any>,
|
||||
Input extends Record<string, any>,
|
||||
RemoteConfig extends Record<string, any>,
|
||||
> {
|
||||
constructor(
|
||||
readonly configs: AutoConfigFrom<Store, Vault, Input, NestedConfigs>,
|
||||
readonly path: keyof AutoConfigFrom<Store, Vault, Input, NestedConfigs>,
|
||||
readonly autoconfig: (options: {
|
||||
effects: Effects
|
||||
localConfig: Input
|
||||
remoteConfig: RemoteConfig
|
||||
utils: Utils<Store, Vault>
|
||||
}) => Promise<void | DeepPartial<RemoteConfig>>,
|
||||
) {}
|
||||
|
||||
async check(
|
||||
@@ -39,19 +27,15 @@ export class AutoConfig<
|
||||
...options,
|
||||
utils: utils<Store, Vault>(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<AutoConfigure["autoConfigure"]>[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),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<string, any>,
|
||||
Manifest extends SDKManifest,
|
||||
NestedConfigs extends {
|
||||
[key in keyof Manifest["dependencies"]]: unknown
|
||||
},
|
||||
>(
|
||||
_config: Config<Input, Store, Vault>,
|
||||
autoConfigs: AutoConfigFrom<Store, Vault, Input, NestedConfigs>,
|
||||
) {
|
||||
type C = typeof autoConfigs
|
||||
const answer = { ...autoConfigs } as unknown as {
|
||||
[k in keyof C]: AutoConfig<Store, Vault, Input, NestedConfigs>
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
27
lib/test/setupAutoConfig.test.ts
Normal file
27
lib/test/setupAutoConfig.test.ts
Normal file
@@ -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,
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -27,11 +27,6 @@ import {
|
||||
Path,
|
||||
} from "../dependency/setupDependencyMounts"
|
||||
|
||||
// prettier-ignore
|
||||
type skipFirstParam<A> =
|
||||
A extends [any, ...infer B] ? B :
|
||||
A extends [] ? [] :
|
||||
never
|
||||
export type Utils<Store, Vault, WrapperOverWrite = { const: never }> = {
|
||||
createOrUpdateVault: (opts: {
|
||||
key: string
|
||||
|
||||
2
package-lock.json
generated
2
package-lock.json
generated
@@ -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"
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user