mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-26 18:31:52 +00:00
* update registry upload to take id for new admin permissions (#2605) * wip * wip: Get the get dependencies * wip check_dependencies * wip: Get the build working to the vm * wip: Add in the last of the things that where needed for the new sdk * Add fix * wip: implement the changes * wip: Fix the naming --------- Co-authored-by: Lucy <12953208+elvece@users.noreply.github.com>
86 lines
2.5 KiB
TypeScript
86 lines
2.5 KiB
TypeScript
import { Effects, ExpectedExports } from "../types"
|
|
import { SDKManifest } from "../manifest/ManifestTypes"
|
|
import * as D from "./configDependencies"
|
|
import { Config, ExtractConfigType } from "./builder/config"
|
|
import nullIfEmpty from "../util/nullIfEmpty"
|
|
import { InterfacesReceipt as InterfacesReceipt } from "../interfaces/setupInterfaces"
|
|
|
|
declare const dependencyProof: unique symbol
|
|
export type DependenciesReceipt = void & {
|
|
[dependencyProof]: never
|
|
}
|
|
|
|
export type Save<
|
|
A extends
|
|
| Record<string, any>
|
|
| Config<Record<string, any>, any>
|
|
| Config<Record<string, never>, never>,
|
|
> = (options: {
|
|
effects: Effects
|
|
input: ExtractConfigType<A> & Record<string, any>
|
|
}) => Promise<{
|
|
dependenciesReceipt: DependenciesReceipt
|
|
interfacesReceipt: InterfacesReceipt
|
|
restart: boolean
|
|
}>
|
|
export type Read<
|
|
Manifest extends SDKManifest,
|
|
Store,
|
|
A extends
|
|
| Record<string, any>
|
|
| Config<Record<string, any>, any>
|
|
| Config<Record<string, any>, never>,
|
|
> = (options: {
|
|
effects: Effects
|
|
}) => Promise<void | (ExtractConfigType<A> & Record<string, any>)>
|
|
/**
|
|
* 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
|
|
* enforce that we have a spec, write, and reading.
|
|
* @param options
|
|
* @returns
|
|
*/
|
|
export function setupConfig<
|
|
Store,
|
|
ConfigType extends
|
|
| Record<string, any>
|
|
| Config<any, any>
|
|
| Config<any, never>,
|
|
Manifest extends SDKManifest,
|
|
Type extends Record<string, any> = ExtractConfigType<ConfigType>,
|
|
>(
|
|
spec: Config<Type, Store> | Config<Type, never>,
|
|
write: Save<Type>,
|
|
read: Read<Manifest, Store, Type>,
|
|
) {
|
|
const validator = spec.validator
|
|
return {
|
|
setConfig: (async ({ effects, input }) => {
|
|
if (!validator.test(input)) {
|
|
await console.error(String(validator.errorMessage(input)))
|
|
return { error: "Set config type error for config" }
|
|
}
|
|
await effects.clearBindings()
|
|
await effects.clearServiceInterfaces()
|
|
const { restart } = await write({
|
|
input: JSON.parse(JSON.stringify(input)) as any,
|
|
effects,
|
|
})
|
|
if (restart) {
|
|
await effects.restart()
|
|
}
|
|
}) as ExpectedExports.setConfig,
|
|
getConfig: (async ({ effects }) => {
|
|
const configValue = nullIfEmpty((await read({ effects })) || null)
|
|
return {
|
|
spec: await spec.build({
|
|
effects,
|
|
}),
|
|
config: configValue,
|
|
}
|
|
}) as ExpectedExports.getConfig,
|
|
}
|
|
}
|
|
|
|
export default setupConfig
|